atcoder#DDCC2020QUALD. Digit Sum Replace

Digit Sum Replace

Score: 500500 points

Problem Statement

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

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

  • All the NN contestants will participate in the first round.
  • When XX 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 XX, 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=2378X = 2378, the number of contestants advancing to the next round will be 578578 (if 22 and 33 are chosen), 21082108 (if 33 and 77 are chosen), or 23152315 (if 77 and 88 are chosen). When X=100X = 100, the number of contestants advancing to the next round will be 1010, no matter which two digits are chosen.
  • The organizer will choose two consecutive digits in the decimal notation of XX, 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=2378X = 2378, the number of contestants advancing to the next round will be 578578 (if 22 and 33 are chosen), 21082108 (if 33 and 77 are chosen), or 23152315 (if 77 and 88 are chosen). When X=100X = 100, the number of contestants advancing to the next round will be 1010, no matter which two digits are chosen.
  • The preliminary stage ends when 99 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, NN, can be enormous, it is given to you as two integer sequences d1,,dMd_1, \ldots, d_M and c1,,cMc_1, \ldots, c_M, which means the following: the decimal notation of NN consists of c1+c2++cMc_1 + c_2 + \ldots + c_M digits, whose first c1c_1 digits are all d1d_1, the following c2c_2 digits are all d2d_2, \ldots, and the last cMc_M digits are all dMd_M.

Constraints

  • 1M2000001 \leq M \leq 200000
  • 0di90 \leq d_i \leq 9
  • d10d_1 \neq 0
  • didi+1d_i \neq d_{i+1}
  • ci1c_i \geq 1
  • 2c1++cM10152 \leq c_1 + \ldots + c_M \leq 10^{15}

Input

Input is given from Standard Input in the following format:

MM

d1d_1 c1c_1

d2d_2 c2c_2

::

dMd_M cMc_M

Output

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

2
2 2
9 1
3

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

  • 229229 contestants participate in Round 11, 4949 contestants participate in Round 22, 1313 contestants participate in Round 33, and 44 contestants advance to the finals.

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

3
1 1
0 8
7 1
9

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