Problem:
Score: 500 points

Problem Statement

N programmers are going to participate in the preliminary stage of DDCC 20XX. Due to the size of the venue, however, at most 9 contestants can participate in the finals.

The preliminary stage consists of several rounds, which will take place as follows:

All the N contestants will participate in the first round.

When X contestants participate in some round, the number of contestants advancing to the next round will be decided as follows:

The organizer will choose two consecutive digits in the decimal notation of X, and replace them with the sum of these digits. The number resulted will be the number of contestants advancing to the next round.

For example, when X = 2378, the number of contestants advancing to the next round will be 578 (if 2 and 3 are chosen), 2108 (if 3 and 7 are chosen), or 2315 (if 7 and 8 are chosen).

When X = 100, the number of contestants advancing to the next round will be 10, no matter which two digits are chosen.

The preliminary stage ends when 9 or fewer contestants remain.

Ringo, the chief organizer, wants to hold as many rounds as possible.
Find the maximum possible number of rounds in the preliminary stage.

Since the number of contestants, N, can be enormous, it is given to you as two integer sequences d_1, \ldots, d_M and c_1, \ldots, c_M, which means the following: the decimal notation of N consists of c_1 + c_2 + \ldots + c_M digits, whose first c_1 digits are all d_1, the following c_2 digits are all d_2, \ldots, and the last c_M digits are all d_M.

Constraints

1 \leq M \leq 200000

0 \leq d_i \leq 9

d_1 \neq 0

d_i \neq d_{i+1}

c_i \geq 1

2 \leq c_1 + \ldots + c_M \leq 10^{15}

Input

Input is given from Standard Input in the following format:

M
d_1 c_1
d_2 c_2
:
d_M c_M

Output

Print the maximum possible number of rounds in the preliminary stage.

Sample Input 1

2
2 2
9 1

Sample Output 1

3

In this case, N = 229 contestants will participate in the first round. One possible progression of the preliminary stage is as follows:

229 contestants participate in Round 1, 49 contestants participate in Round 2, 13 contestants participate in Round 3, and 4 contestants advance to the finals.

Here, three rounds take place in the preliminary stage, which is the maximum possible number.

Sample Input 2

3
1 1
0 8
7 1

Sample Output 2

9

In this case, 1000000007 will participate in the first round.

Solution in Rust (1.15.1) (judge verdict: Wrong Answer)

#[allow(unused_imports)]
use std::cmp::Ordering;

#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};

#[allow(unused_imports)]
use std::io::{stdin, stdout, BufWriter, Write};

#[allow(unused_imports)]
use std::iter::FromIterator;

#[allow(unused_macros)]
macro_rules! min {
    ($x: expr) => ($x);
    ($x: expr, $($z: expr),+) => {{
        std::cmp::min($x, min!($($z),*))
    }}
}

#[allow(unused_macros)]
macro_rules! max {
    ($x: expr) => ($x);
    ($x: expr, $($z: expr),+) => {{
        std::cmp::max($x, max!($($z),*))
    }}
}

#[allow(unused_macros)]
macro_rules! get {
    ([$t: ty]) => {
        {
            let mut line = String::new();
            stdin().read_line(&mut line).unwrap();
            line.split_whitespace().map(|t|t.parse::<$t>().unwrap()).collect::<Vec<_>>()
        }
    };
    ([$t: ty];$n: expr) => {
        (0..$n).map(|_|get!([$t])).collect::<Vec<_>>()
    };
    ($t: ty) => {
        {
            let mut line = String::new();
            stdin().read_line(&mut line).unwrap();
            line.trim().parse::<$t>().unwrap()
        }
    };
    ($($t: ty),*) => {
        {
            let mut line = String::new();
            stdin().read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            ($(iter.next().unwrap().parse::<$t>().unwrap(),)*)
        }
    };
    ($t: ty; $n: expr) => {
        (0..$n).map(|_|get!($t)).collect::<Vec<_>>()
    };
    ($($t: ty),*; $n: expr) => {
        (0..$n).map(|_|get!($($t),*)).collect::<Vec<_>>()
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        #[cfg(debug_assertions)]
        writeln!(&mut std::io::stderr(), concat!("[DEBUG] ", $(stringify!($a), "={:?} "),*), $($a),*).unwrap();
    }
}

const BIG_STACK_SIZE: bool = true;

#[allow(dead_code)]
fn main() {
    use std::thread;
    if BIG_STACK_SIZE {
        thread::Builder::new()
            .stack_size(32 * 1024 * 1024)
            .name("solve".into())
            .spawn(solve)
            .unwrap()
            .join()
            .unwrap();
    } else {
        solve();
    }
}

// fn dfs(val: Vec<usize>) -> Option<usize> {
//     if val.len() <= 1 {
//         return Some(0);
//     }
//     let mut res = None;
//     for i in 0..val.len() - 1 {
//         let mut val = val.clone();
//         let tmp = val[i] + val[i + 1];
//         if tmp >= 10 {
//             val[i] = tmp / 10;
//             val[i + 1] = tmp % 10;
//         } else {
//             val[i] = tmp;
//             val.remove(i + 1);
//         }
//         if let Some(r) = dfs(val) {
//             if let Some(res) = res.as_mut() {
//                 if *res != r + 1 {
//                     return None;
//                 }
//             } else {
//                 res = Some(r + 1);
//             }
//         } else {
//             return None;
//         }
//     }
//     res
// }

fn calc(mut val: Vec<usize>) -> (usize, usize) {
    if val.len() <= 1 {
        return (0, val[0]);
    }
    let tmp = val[0] + val[1];
    if tmp >= 10 {
        val[0] = tmp / 10;
        val[0 + 1] = tmp % 10;
    } else {
        val[0] = tmp;
        val.remove(1);
    }
    let (count, res) = calc(val);
    (count + 1, res)
}

fn solve() {
    let m = get!(usize);
    let dc = get!(usize,usize;m);

    let mut res = 0usize;
    let mut tvec = Vec::new();
    let mut vec = vec![0; 10];
    for (d, c) in dc {
        vec[d] += c;
    }
    for d in 0..10 {
        let c = vec[d];
        if c == 0 {
            continue;
        } else if c == 1 {
            tvec.push(d);
            continue;
        }
        res += (c - 1) / 9 * (9 + d);
        let mut vec = Vec::new();
        if (c - 1) / 9 > 0 {
            if d >= 1 {
                vec.push(1);
                vec.push(d - 1);
            } else {
                vec.push(d);
            }
        } else {
            vec.push(d);
        }
        for _ in 0..(c - 1) % 9 {
            vec.push(d);
        }
        let (count, val) = calc(vec);
        tvec.push(val);
        res += count;
    }
    res += calc(tvec).0;
    println!("{}", res);
}