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 Kotlin (1.0.0) (judge verdict: Wrong Answer)

import java.util.*

fun f(n: Int): Pair<Int, Int> {
    if (n < 10) {
        return Pair(0, 0)
    }
    val s = n.toString()

    var mn = 1e8.toInt()
    var mx = 0

    for (i in 0 until s.length - 1) {
        val t = s.substring(0, i) + (s[i].toInt() + s[i + 1].toInt() - 2 * '0'.toInt()).toString() + s.substring(i + 2)

        val r = f(t.toInt())
        mn = Math.min(mn, r.first + 1)
        mx = Math.min(mn, r.second + 1)
    }

    return Pair(mn, mx)
}

fun update(p: TreeMap<Long, Long>): Triple<TreeMap<Long, Long>, Long, Boolean> {
    var cnt = 0L
    var updated = false
    val res = TreeMap<Long, Long>()

    for (e in p) {
        val pair = Pair(e.key, e.value)
        if (pair.second > 1) {
            cnt += pair.second / 2
            val a = pair.first * 2

            res[a % 10] = (res[a % 10] ?: 0L) + pair.second / 2

            if (a >= 10) {
                res[a / 10] = (res[a / 10] ?: 0L) + pair.second / 2
            }

            if (a % 2 == 1L) {
                res[pair.first] = (res[pair.first] ?: 0L) + 1
            }
            updated = true
        } else {
            val v = res.get(pair.first)
            res[pair.first] = v?.plus(pair.second) ?: pair.second
        }
    }

    return Triple(res, cnt, updated)
}

fun main(args: Array<String>) {
//    var q = true
//    for (i in 1..10000) {
//        val r = f(i)
//        val s = f(String(i.toString().toCharArray().sortedDescending().toCharArray()).toInt())
////        println("$i ${r.first} ${r.second}")
//        q = q && r.first == r.second && r.first == s.first && r.second == s.second
//        if (r.first != r.second || r.first != s.first || r.second != s.second) {
//            println("$i $r $s")
//        }
//    }
//    println(q)

    val M = readLine()!!.toInt()

    var p = TreeMap<Long, Long>()

    for(i in 1 .. M) {
        val (d, c) = readLine()!!.split(" ").map{it.toLong()}
        p[d] = (p[d] ?: 0L) + c
    }

    var res = 0L

    do {
        val (q, s, f) = update(p)
        res += s
        p = q
    } while(f)

    val s = p.map { if (it.value > 0) it.key.toString() else "" }.joinToString("")
    var t = String(s.toCharArray().sortedDescending().toCharArray())

    while(t.length > 1) {
        res ++
        t = (t[0].toInt() + t[1].toInt() - 2 * '0'.toInt()).toString() + t.substring(2)
    }

    println(Math.max(res, 1))
}