D - Digit Sum Replace Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点: 500500

問題文

DDCC 20XX の予選には,NN 人のプログラマーが参加する予定です.しかし,会場の都合上,本戦には 99 人までしか参加できません.

そこで,予選を何ラウンドかに分けて勝ち抜き方式で行うことにしました.これは,以下のルールに従って行われます.

  • 最初のラウンドには NN 人全員が参加する.
  • あるラウンドに X (X10)X \ (X \geq 10) 人が参加するとき,次のラウンドに勝ち残る人数を以下のように決定する.
    • XX の十進表記において,ある連続する 22 桁を選び,それらをその和で置き換えて得られる数を勝ち残る人数とする.
      例えば,X=2378X = 2378 のとき,勝ち残る人数は 578578 (2,32,3 を選んだ場合),21082108 (3,73,7 を選んだ場合),23152315 (7,87,8 を選んだ場合) 人のいずれかとなる.
      X=100X = 100 のときは,どちらの 22 桁を選んだとしても勝ち残る人数は 1010 人となる.
  • 勝ち残った人数が 99 人以下となったら,予選を終了する.

DDCC 20XX の運営リーダーであるりんごさんは,できるだけ多くの予選ラウンドを開催したいです.
最大で何ラウンドの予選を開催できるか求めてください.

ただし,参加者数 NN は非常に多くなる場合があるので,22 つの整数列 d1,,dMd_1, \ldots, d_Mc1,,cMc_1, \ldots, c_M として与えられます.
これは,NN が十進表記において c1+c2++cMc_1 + c_2 + \ldots + c_M 桁の数であり,その先頭の c1c_1 桁の数字がいずれも d1d_1,続く c2c_2 桁の数字がいずれも d2d_2\ldots,最後の cMc_M 桁の数字がいずれも dMd_M であることを表します.

制約

  • 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}

入力

入力は以下の形式で標準入力から与えられます.

MM
d1d_1 c1c_1
d2d_2 c2c_2
::
dMd_M cMc_M

出力

予選ラウンドの数の最大値を出力してください.


入力例 1Copy

Copy
2
2 2
9 1

出力例 1Copy

Copy
3

この場合,予選の最初のラウンドには N=229N=229 人が参加します.大会の経過の一例として、次のパターンがありえます.

  • ラウンド 11229229 人が参加し,ラウンド 224949 人が参加し,ラウンド 331313 人が参加し,本戦に 44 人が進出する.

このとき,予選は 33 ラウンド行われ、これが実は最適であることが分かります。


入力例 2Copy

Copy
3
1 1
0 8
7 1

出力例 2Copy

Copy
9

この場合,最初のラウンドには 10000000071000000007 人が参加します.

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 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.


Sample Input 1Copy

Copy
2
2 2
9 1

Sample Output 1Copy

Copy
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.


Sample Input 2Copy

Copy
3
1 1
0 8
7 1

Sample Output 2Copy

Copy
9

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



2025-04-27 (Sun)
19:13:37 +00:00