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.  



Sample input:
2
2 2
9 1


Sample output:
3

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**6)

def read_tokens():
    return input().strip().split(' ')

def read_ints():
    return [int(token) for token in read_tokens()]

n, = read_ints()
c = [0] * n
d = [0] * n

s = ''
for i in range(n):
    d[i], c[i] = read_ints()
    s += str(d[i]) * c[i]

# print(f's={s}')
cache = {}
def f(s):
    global cache
    x = int(s)
    if x <= 9:
        return 0
    if s in cache:
        return cache[s]
    ans = 0
    best = ''
    for i in range(len(s) - 1):
        a = int(s[i])
        b = int(s[i+1])
        t = f(s[:i] + str(a+b) + s[i+2:])
        ans = max(ans, t + 1)
        if t + 1 == ans:
            best = s[:i] + str(a+b) + s[i+2:]

    for i in range(len(s) - 1):
        a = int(s[i])
        b = int(s[i+1])
        t = f(s[:i] + str(a+b) + s[i+2:])
        if t + 1 != ans:
            print(f'!!! {s} {i} {s[:i] + str(a+b) + s[i+2:]} {t + 1} {bedt} {ans}')

    cache[s] = ans
    return ans

# print(f(s))

# while True:
#     s, = read_tokens()
#     print(f(s))

def processBlock(n, c):
    assert(c > 0)
    if c == 1:
        return n, 0
    if c % 2 == 0:
        if 2 * n < 10:
            rn, rc = processBlock(2 * n, c // 2)
            return rn, rc + c // 2
        else:
            d = (2 * n) // 10 + (2 * n) % 10
            rn, rc = processBlock(d, c // 2)
            return rn, rc + c
    rn, rc = processBlock(n, c - 1)
    if n + rn < 10:
        return n + rn, rc + 1
    else:
        d = (n + rn) // 10 + (n + rn) % 10
        return d, rc + 2

def solve(D, C):
    ret = 0
    a = []
    for d, c in zip(D, C):
        n, cc = processBlock(d, c)
        ret += cc
        a.append(n)
    while len(a) > 1:
        d1 = a.pop()
        d2 = a.pop()
        ret += 1
        if d1 + d2 < 10:
            a.append(d1 + d2)
        else:
            a.append(1)
            a.append((d1 + d2) % 10)
    return ret

print(solve(d, c))
# print(f(s))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import array
from bisect import *
from collections import *
import fractions
import heapq
from itertools import *
import math
import random
import re
import string
import sys


M = int(input())
S = ""
DCs = []
for m in range(M):
    d, c = map(int, input().split())
    S += str(d) * c
    DCs.append((d, c))

def func(n):
    cnt = 1
    n *= 2
    if n >= 10:
        cnt += 1
        s = str(n)
        n = int(s[0]) + int(s[1])
    return n, cnt


def random_solve(S):
    S = S[:]
    ans = 0
    while int(S) >= 10:
        size = len(S)
        i = random.randint(0, size - 2)
        S = S[:i] + str(int(S[i]) + int(S[i + 1])) + S[i+2:]
        ans += 1
    return ans
    

def solve(DCs):
    ans = 0
    remain = []
    for d, c in DCs:
        local_ans = 0
        while c >= 2:
            if c % 2:
                remain.append(d)
                c -= 1
            new_d, cnt = func(d)
            local_ans += cnt * (c // 2)

            d = new_d
            c //= 2
        remain.append(d)
        ans += local_ans
        # print(remain, local_ans, d, c)

    S = "".join(str(x) for x in remain)
    ans += random_solve(S)

    return ans
            
        
print(solve(DCs))
    



# for rep2 in range(100):
#     size = 100
#     S = ''
#     for s in range(size):
#         S += str(random.randint(0, 9))
#     print(S)

#     for rep in range(100):
#         ans = random_solve(S)
#         print(ans, end=" ")

Submission status: Runtime Error

Solution code:
M = int(input())
sumd, sumc = 0, 0
for i in range(M):
    d, c = map(int, input().split())
    sumd += d * c
    sumc += c

for i in range(1, len(str(sumd))):
    tmp = sum // (10 ** i)
    sumd += tmp

ans = sumd // 10 + sumc - 1
print(ans)

Submission status: Runtime Error

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
inf=float("inf")
m=int(input())
dc=[list(map(int,input().split())) for i in range(m)]
n=""
keta=0
for i in range(m):
    n+=str(dc[i][0])*dc[i][1]
    keta+=dc[i][1]
i=0
nex=int(n[i])
cnt=0
while(i<keta-1):
    tmp=nex+int(n[i+1])
    if tmp>9:
        tmp=str(tmp)
        tmp=int(tmp[0])+int(tmp[1])
        cnt+=1
    nex=tmp
    i+=1
    cnt+=1
    # print(cnt,nex)
print(cnt)

Submission status: Runtime Error

Solution code:
M = int(input())
l = list()
for m in range(M):
    l.append(input().split())
number = ""
c = 0
for m in range(M):
    number += l[m][0]*int(l[m][1])
    while len(number) > 1:
        k = str(int(number[0])+int(number[1]))
        number = k + number[2:]
        c += 1
print(c)

Submission status: Runtime Error

Solution code:
M = int(input())

n = []


for i in range(M):
    d, c = [v for v in input().split()]

    dv = int(d)
    cv = int(c)
    if cv == 1:
        n.append(dv)
    else:
        n.extend([dv] * cv)

def max_i(n):
    n1 = n[0]
    for i in range(1, len(n)):
        n2 = n[i]
        if n1 < n1 + n2:
            return i - 1
        n1 = n2

def next_n(n, i):
    v = n[i] + n[i + 1]
    if v > 9:
        n[i] = v // 10
        n[i + 1] = v % 10
    else:
        n[i] += n.pop(i + 1)

a = 0
while len(n) > 1:
    #print(n)
    j = max_i(n)
    next_n(n, j)
    a += 1

print(a)

Submission status: Runtime Error

Solution code:
m=int(input())
d=[]
c=[]
for i in range(m):
    a,b=map(int,input().split())
    d.append(a)
    c.append(b)
p=[0]*10
for i in range(m):
    p[d[i]]+=c[i]
ans=0
def ika(x,y):
    ret=0
    z=x*y
    ret+=y-1
    while z>9:
        s=list(str(z))
        ret+=len(s)-1
        for i in range(len(s)):
            s[i]=int(s[i])
        z=sum(s)
    return [ret,z]
ka=[]
for i in range(m):
    f=ika(d[i],c[i])
    ans+=f[0]
    ka.append(f[1])
print(ans+ika(sum(ka),1)+len(ka-1))

Submission status: Runtime Error

Solution code:
M = int(input())
l = list()
for m in range(M):
    l.append(input().split())
number = ""
for m in range(M):
    number += l[m][0]*int(l[m][1])
c = 0
n = 0
while len(number) > 1:
    k = str(int(number[n])+int(number[n+1]))
    number = number[:n] + k + number[n+2:]
    c += 1
print(c)

Submission status: Runtime Error

Solution code:
m = int(input())

digits = []
for i in range(m):
    d, c = [int(x) for x in input().split(' ')]
    digits.extend([d] * c)

rounds = 1
while True:
    max_value = 0
    max_digits = []
    l = len(digits)

    for i in range(l - 1):
        a = []
        x = -1
        for j in range(l):
            if j == i:
                x = digits[j]
            elif j == i + 1:
                x = x + digits[j]
                if x >= 10:
                    a.append(1)
                a.append(x % 10)
            else:
                a.append(digits[j])
        v = int(''.join([str(x) for x in a]))
        if max_value < v:
            max_value = v
            max_digits = a
    digits = max_digits
    if max_value <= 9:
        break
    rounds += 1

print(rounds)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
#DISCO C
import sys
import math
import collections
#sys.setrecursionlimit(100000)

#n=int(input())

tmp = input().split()
h,w,k = list(map(lambda a: int(a), tmp))

list = [[0 for i in range(w+1)] for j in range(h)]
for i in range(0,h):
	tmp = input()
	for j in range(0,w):
		if(tmp[j]=='#'):
			list[i][j]+=1
			list[i][w]+=1

ans = [[0 for i in range(w)] for j in range(h)]
index=1
for i in range(0,h):
	if(list[i][w]!=0):
		flg=0
		for j in range(0,w):
#			print(index)
			if(list[i][j]==1):
				if(flg==0):
					flg=1
				else:
					index+=1
			ans[i][j] = index
		index+=1
#			print(list[i][j])
for i in range(1,h):
	if(list[i][w]==0)and(list[i-1][w]!=0):
		for j in range(0,w):
			ans[i][j]=ans[i-1][j]
		list[i][w]==1
for i in range(1,h):
	if(list[h-i-1][w]==0)and(list[h-i][w]!=0):
		for j in range(0,w):
			ans[h-i-1][j]=ans[h-i][j]
		list[h-i-1][w]==1

#print(list)
#print(ans)


for i in range(0,h):
	outstr=""
	for j in range(0,w):
		outstr+=str(ans[i][j])
		if(j!=w-1):
			outstr+=" "
	print(outstr)

Submission status: Runtime Error

Solution code:
def MAP(): return list(map(int,input().split()))
def INT(): return int(input())
def FLOAT(): return float(input())
MOD = 10**9+7

m = INT()
ll = []
for i in range(m):
    tmp = MAP()
    ll += [tmp[0]]*tmp[1]

cnt=0
while len(ll)!=1:
    tmp = ll[0] + ll[1]
    del ll[0:2]
    if tmp>=10:
        ll.insert(0,tmp-10)
        ll.insert(0,tmp//10)
    else:
        ll.insert(0,tmp)
    cnt+=1

print(cnt)

Submission status: Runtime Error

Solution code:
m = int(input())


def a(x):
    k = 0
    while len(x) > 1:
        x = str(int(str(int(x[0]) + int(x[1])) + x[2:len(x)]))
        k += 1
    return x, k


k = 0
x = ""
for _ in range(m):
    d, c = [int(i) for i in input().split()]
    x += str(d) * c
    x, ka = a(x)
    k += ka
print(k)

Submission status: Runtime Error

Solution code:
def main():
    m=int(input())
    dc=[list(map(int,input().split())) for _ in [0]*m]
    n="".join([str(dc[i][0])*dc[i][1] for i in range(m)])
    sum_n=sum([d*c for d,c in dc])
    sum_c=sum([c for d,c in dc])
    print((sum_n-1)//9+sum_c-1)
main()

Submission status: Runtime Error

Solution code:
m = int(input())


def a(x):
    x = str(x)
    k = 0
    while len(x) != 1:
        x = str(int(x[0]) + int(x[1])) + x[2:]
        k += 1
    return x, k


k = 0
x = ""
for _ in range(m):
    d, c = [int(i) for i in input().split()]
    x += str(d) * c
    x, ka = a(x)
    k += ka
print(k)