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 Scala (2.11.7) (judge verdict: Runtime Error)

import java.io.PrintWriter

object Loop {
  @inline def rep(n: Int, f: => Unit): Unit = { var c = 0; while (c < n) { f; c += 1 } }
  @inline def rep(n: Int, f: Int => Unit): Unit = { var c = 0; while (c < n) { f(c); c += 1 } }

  @inline def breakAble(n: Int, m: Int, f: Int => Boolean): Unit = if (f(n) && n < m) breakAble(n + 1, m, f)
  @inline def !(n: Int): Int = (1 + n) * n / 2
}
object Mod {
  def create(n: Int, mod: Int) = Mod(n, mod)
}
case class Mod(n: Int, mod: Int) {
  val factorial: Array[Int] = Array.fill[Int](n + 1)(1)
  val inverse: Array[Int] = Array.fill[Int](n + 1)(1)
  val inverseFactorial: Array[Int] = Array.fill[Int](n + 1)(1)
  Loop.rep(n - 1, i => {
    val k = i + 2
    factorial(k) = modMultiplyExact(factorial(k - 1), k, mod)
    inverse(k) = modMultiplyExact(inverse(mod % k), mod - (mod / k): Int, mod)
    inverseFactorial(k) = modMultiplyExact(inverseFactorial(k - 1), inverse(k), mod)
  })
  def getFactorial(n: Int): Int = factorial(n)
  def getInverse(n: Int): Int = inverse(n)
  def getInverseFactorial(n: Int): Int = inverseFactorial(n)
  def C(n: Int, k: Int): Int = {
    if (n <= 0 || k < 0 || k > n) 0 else if (k == 0 || k == n) 1 else {
      modMultiplyExact(
        getFactorial(n),
        getInverseFactorial(n - k),
        getInverseFactorial(k),
        mod
      )
    }
  }
  def modMultiplyExact(a: Int, b: Int, m: Int): Int =
    ((a.toLong * b.toLong) % m).toInt
  def modMultiplyExact(a: Int, b: Int, c: Int, m: Int): Int =
    modMultiplyExact(a, modMultiplyExact(b, c, m), m)
}
object Scanner {
  private val buf = new Array[Byte](1024); private var ptr = 0; private var len = 0
  @inline private def isPrintableChar(c: Int): Boolean = 33 <= c && c <= 126
  @inline private def hasNextByte(): Boolean =
    if (ptr >= len) { ptr = 0; len = System.in.read(buf); len > 0 } else { true }
  @inline private def hasNext(): Boolean = {
    while (hasNextByte() && !isPrintableChar(buf(ptr))) ptr += 1
    hasNextByte()
  }
  @inline private def readByte(): Byte =
    if (hasNextByte()) { val res = buf(ptr); ptr += 1; res } else { -1 }
  def next(): String = {
    if(!hasNext()) ???
    val sb = new StringBuilder; var b = readByte()
    while (isPrintableChar(b)) { sb.append(b.toChar); b = readByte() }
    sb.toString
  }
  def nextInt(): Int = {
    val n = nextLong()
    if (n < Int.MinValue || Int.MaxValue < n) ???
    n.toInt
  }
  def nextLong(): Long = {
    if(!hasNext()) ???
    var minus = false; var b = readByte()
    if (b == '-') { minus = true; b = readByte() }
    def go (b: Byte, n: Long = 0): Long =
      if ('0' <= b && b <= '9') { go(readByte(), n * 10 + b - '0') }
      else if (minus) { -n } else { n }
    go(b)
  }
  def nextDouble(): Double = next.toDouble

  def print(f:PrintWriter => Unit): Unit = {
    val pw: PrintWriter = new java.io.PrintWriter(System.out)
    //pw.println("")
    f(pw)
    pw.flush()
  }
}
////////////////////////////////////////
//
////////////////////////////////////////
object Main extends App {

  val m = Scanner.nextInt()
  val data = Array.fill[Int](m, 2)(Scanner.nextInt())


  var count = -1
  var sumTmp = 0

  def plus(a: Int, b: Int): Int = {
    count += 1
    val v = a + b
    val one = v % 10
    val ten = (v - one) / 10
    if (v >= 10) plus(one, ten) else one
  }
  def plusLoop(parentSum: Int, oneValue: Int, oneCount: Int): Int = {
    if (oneCount >= 1) {
      plusLoop(plus(parentSum, oneValue), oneValue, oneCount - 1)
    } else parentSum
  }

  Loop.rep(m, i => {
    val oneValue = data(i)(0)
    val oneCount = data(i)(1)
    sumTmp = plusLoop(sumTmp, oneValue, oneCount)
  })

  if (sumTmp >= 10) plus(sumTmp, 0)

  println(count)

}