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:
################################
def dp(dc,count):
  if dc[0]==0:
    return 0, len(dc)-1
  
  num = int("".join(map(str, dc)))
  if num<10:
    return num, count
  else:
    tmp = [int(si) for si in str(num)]
    tmp[-2] = tmp[-2]+tmp[-1]
    tmp=tmp[:-1]
    ret,count = dp(tmp,count)
    return ret, count+1

################################
m = int(input())
all_count=0
d = [0]*m
c = [0]*m
dc=[0]*m
for i in range(m):
  d[i],c[i] = map(int, input().split())
  tmp = [d[i]]*c[i]

  ret, count = dp(tmp,0)
  dc[i] = ret
  all_count+=count

#print(dc)
ret, count = dp(dc,0)
all_count+=count
print(all_count)

Submission status: Runtime Error

Solution code:
# 2019-11-23 21:00:35(JST)
import sys

def main():
    m = int(sys.stdin.readline().rstrip())

    s = ''
    for i in range(m):
        d, c = sys.stdin.readline().split()
        c = int(c)
        s += d * c

    count = 0
    while int(s) >= 10:
        s = str(int(s[0]) + int(s[1])) + s[2:]
        count += 1
    
    print(count)




if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#dfs
import sys
sys.setrecursionlimit(10 ** 7)

#dfs
M = int(input())
L = []
for i in range(M):
    d, c = map(int, input().split())
    L.extend([d] * c)

answer = 100

def dfs(count, l):
    global answer
    if len(l) == 1:
        #print(count)
        answer = min(answer, count)
    else:
        for i in range(len(l)-1):
            sum = l[i] + l[i+1]
            result = l[:i] + [int(x) for x in list(str(sum))] + l[i+2:]
            #print(result)
            dfs(count+1, result)

dfs(0, L)
print(answer)

Submission status: Runtime Error

Solution code:
#include<iostream>
#include<vector>
using namespace std;
#define ll long long

int main(){
  ll M, ans;
  cin >> M;
  
  vector<ll> X;
  //                     1   2  3   4    5   6   7    8  9
  vector<ll> cost = {0, 70, 77, 4, 91, 119,  5, 112, 98, 2};
  vector<ll> num  = {0, 64, 64, 4, 64,  64,  4,  64, 64, 2};
  for(ll i=0; i<M; ++i) {
    ll d, c;
    cin >> d >> c;
    if(d==0){ ans+=c; continue; }
    ans += (c/num[d])*cost[d];
    for(ll i=0; i<c%num[d]; ++i) X.emplace_back(d);
  }

  ll v = X[0];
  for(ll i=1; i<X.size(); ++i){
    ans += 1 + (v+X[i])/10;
    v = (v+X[i])/10 + (v+X[i])%10;
  }

  cout << ans << endl;

  return 0;
}

Submission status: Runtime Error

Solution code:
from decimal import *

m = int(input())
ans = 0
num = ""
for i in range(m):
    d, c = input().split()
    d = int(d)
    if len(c) > 10:
        num += c[:10]
        c = int(c[10:])
    if d == 0:
        ans += c
    else:
        k = str(d) * c
        while len(k) > 1:
            if len(k) > 2:
                k = str(int(k[0]) + int(k[1])) + k[2:]
            else:
                k = str(int(k[0]) + int(k[1]))
            ans += 1
        num += k
while len(num) > 1:
    if len(num) > 2:
        num = str(int(num[0]) + int(num[1])) + num[2:]
    else:
        num = str(int(num[0]) + int(num[1]))
    ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
m = int(input())
keta = 0
for i in range(m):
    a, b = map(int, input().split())
    fig_sum+=a*b
    keta+=b

sub = keta-1
while fig_sum >= 10:
    fig_sum = fig_sum//10
    sub+= fig_sum

#print("fig_sum",fig_sum)
#print("keta",keta)
print(sub)

Submission status: Runtime Error

Solution code:
#dfs
import sys
sys.setrecursionlimit(10 ** 7)

def dfs(count, l):
    global answer
    if len(l) == 1:
        #print(count)
        answer = min(answer, count)
    elif count > answer:
        return
    else:
        for i in range(len(l)-1):
            sum = l[i] + l[i+1]
            result = l[:i] + [int(x) for x in list(str(sum))] + l[i+2:]
            #print(result)
            dfs(count+1, result)

def main():
    global answer
    M = int(input())
    L = []
    for i in range(M):
        d, c = map(int, input().split())
        L.extend([d] * c)

    answer = 100
    dfs(0, L)
    print(answer)

if __name__ == '__main__':
    answer = 1000
    main()

Submission status: Runtime Error

Solution code:
from decimal import *

m = int(input())
ans = 0
num = ""
for i in range(m):
    d, c = input().split()
    d = int(d)
    if len(c) > 3:
        num += c[:10]
        c = int(c[10:])
    if d == 0:
        ans += c
    else:
        k = str(d) * c
        while len(k) > 1:
            if len(k) > 2:
                k = str(int(k[0]) + int(k[1])) + k[2:]
            else:
                k = str(int(k[0]) + int(k[1]))
            ans += 1
        num += k
while len(num) > 1:
    if len(num) > 2:
        num = str(int(num[0]) + int(num[1])) + num[2:]
    else:
        num = str(int(num[0]) + int(num[1]))
    ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

def main():
	M = inputInt()
	dc = []
	n = []
	for i in range(M):
		d,c = inputMap()
		dc.append((d,c))
		for i in range(c):
			n.append(d)

	ans = 0
	while True:
		if len(n) <= 1:
			print(ans)
			break

		while True:
			for i,val in enumerate(n):
				flg = False
				if i == 0:
					continue

				tmp = val + n[i-1]
				if tmp >= 10:
					flg = True
					ans += 1
					tmp2 = str(tmp)
					tmp2 = list(tmp2)

					n[i-1] = int(tmp2[0])
					n[i]   = int(tmp2[1])
			if flg == False:
				break

		while True:
			if len(n) <= 1:
				print(ans)
				break
				
			max = 0
			for i,val in enumerate(n):
				if i == 0:
					continue
					tmp = val + n[i-1]
				if max < tmp:
					max = tmp

			if max >= 10:
				break:

			for i,val in enumerate(n):
				if i == 0:
					continue
				tmp = val + n[i-1]
				if tmp == max:
					ans += 1
					n[i-1] = 99
					n[i]   = max
			n.remove(99)


if __name__ == "__main__":
	main()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000000000000)

def answer():
    input = sys.stdin.readline
    M = int(input())

    part = ''
    for i in range(M):
        d, c = input().split()
        part += d * int(c)

    _round = 0
    while len(part) > 1:
        _round += 1
        # 和10以上を探索
        mt10 = 0
        for i in range(1, len(part)):
            s1 = part[-i]
            s2 = part[-i-1]
            if int(s1) + int(s2) >= 10:
                mt10 = - i - 1
                break

        if mt10 == 0:
            s1 = part[0]
            s2 = part[1]
            s3 = part[2:]
            part = str(int(s1) + int(s2)) + s3
        else:
            s1 = part[:mt10]
            s2 = part[mt10]
            s3 = part[mt10+1]
            s4 = ''
            if mt10 < -2:
                s4 = part[mt10+2:]
            part = s1 + str(int(s2)+int(s3)) + s4

    print(_round)


if __name__ == '__main__':
    answer()

Submission status: Runtime Error

Solution code:
INF = float("inf")
IINF = 10 ** 18
MOD = 998244353


def main():
    M = int(input())
    # d = [0] * M
    # c = [0] * M
    digits = []
    # d = sorted(d)

    for i in range(M):
        d, c = list(map(int, input().split()))
        for i in range(c):
            digits.append(d)

    # print(digits)
    ith = 1
    nxt = digits[i] + digits[i+1]
    # print(nxt)
    for i in range(2, len(digits)):
        if nxt + digits[i] >= 10:
            nxt = nxt % 10
            ith += 2
        else:
            nxt = nxt + digits[i]
            ith += 1

    print(ith)


if __name__ == '__main__':
    main()

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]
    j = -1
    for i in range(1, len(n)):
        n2 = n[i]
        n3 = n1 + n2
        if n3 > 10:
            return i - 1
        if j < 0 and n1 < n3:
            j = i - 1
        n1 = n2
    return j

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:
import sys
sys.setrecursionlimit(1000000000)

def answer():
    input = sys.stdin.readline
    M = int(input())

    part = ''
    for i in range(M):
        d, c = input().split()
        part += d * int(c)

    _round = 0
    while len(part) > 1:
        _round += 1
        # 和10以上を探索
        mt10 = 0
        for i in range(1, len(part)):
            s1 = part[-i]
            s2 = part[-i-1]
            if int(s1) + int(s2) >= 10:
                mt10 = - i - 1
                break

        if mt10 == 0:
            s1 = part[0]
            s2 = part[1]
            s3 = part[2:]
            part = str(int(s1) + int(s2)) + s3
        else:
            s1 = part[:mt10]
            s2 = part[mt10]
            s3 = part[mt10+1]
            s4 = ''
            if mt10 < -2:
                s4 = part[mt10+2:]
            part = s1 + str(int(s2)+int(s3)) + s4

    print(_round)


if __name__ == '__main__':
    answer()

Submission status: Runtime Error

Solution code:
M = int(input())
d, c = [], []
N = ''
for i in range(M):
    di, ci = map(int, input().split())
    N += str(di)*ci
N = list(N)

num_round = 0
while True:
    if int(''.join(N)) <= 9:
        break
    n_list = []
    max_n = 0
    max_i = 0
    for i in range(len(N)-1):
        n = int(''.join(N[:i])+str(int(N[i])+int(N[i+1]))+''.join(N[i+2:]))
        if max_n < n:
            max_n = n
            max_i = i
    N = list(str(max_n))
    num_round += 1
print(num_round)

Submission status: Runtime Error

Solution code:
#dfs
M = int(input())
L = []
for i in range(M):
    d, c = map(int, input().split())
    L.extend([d] * c)

answer = 100

def dfs(count, l):
    global answer
    if len(l) == 1:
        #print(count)
        answer = min(answer, count)
    else:
        for i in range(len(l)-1):
            sum = l[i] + l[i+1]
            result = l[:i] + [int(x) for x in list(str(sum))] + l[i+2:]
            #print(result)
            dfs(count+1, result)

dfs(0, L)
print(answer)

Submission status: Runtime Error

Solution code:
#順番考えなくていいかとりあえず試す
m = int(input())
nm = [(0,0)]
for i in range(m):
    di,ci = map(int, input().split( ))
    nm.append((di,ci))
cnt = 0
tmp = 0
while nm:
    ci,di = nm.pop()
    if di>100:
        tmpy = 1
        tmpz = 1
        tmpx = ci*2
        if tmpx>=10:
            tmpx = str(tmpx)
            tmp2 = int(tmpx[0])+int(tmpx[1])
            tmpy += 1
        while tmpx != ci:
            tmpx += ci
            tmpy +=1
            tmpz +=1
            if tmpx>=10:
                tmpx = str(tmpx)
                tmp2 = int(tmpx[0])+int(tmpx[1])
                tmpy += 1
        cnt += tmpz*(di//tmpy)
        di -= (di//tmpy)*tmpy

    while di:
        cnt += 1
        #print(di,cnt)
        tmp += ci
        di-=1
        if tmp>=10:
            tmp = str(tmp)
            tmp2 = int(tmp[0])+int(tmp[1])
            cnt += 1
            #print(di,cnt)
            tmp = tmp2
print(cnt-1)

Submission status: Runtime Error

Solution code:
m = int(input())
CD = [input().split() for _ in range(m)]
let = ''.join(d * int(c) for d, c in CD)
cnt = 0
while len(let) > 1:
    cnt += 1
    let = let[:-2] + str(int(let[-2])+ int(let[-1]))
print(cnt)

Submission status: Runtime Error

Solution code:
#順番考えなくていいかとりあえず試す
m = int(input())
nm = []
for i in range(m):
    di,ci = map(int, input().split( ))
    nm.append((di,ci))
cnt = 0
tmp = 0
while nm:
    ci,di = nm.pop()
    if di>100:
        tmpy = 1
        tmpz = 1
        tmpx = ci*2
        if tmpx>=10:
            tmpx = str(tmpx)
            tmp2 = int(tmpx[0])+int(tmpx[1])
            tmpy += 1
        while tmpx != ci:
            tmpx += ci
            tmpy +=1
            tmpz +=1
            if tmpx>=10:
                tmpx = str(tmpx)
                tmp2 = int(tmpx[0])+int(tmpx[1])
                tmpy += 1
        cnt += tmpz*(di//tmpy)
        di -= (di//tmpy)*tmpy

    while di:
        cnt += 1
        #print(di,cnt)
        tmp += ci
        di-=1
        if tmp>=10:
            tmp = str(tmp)
            tmp2 = int(tmp[0])+int(tmp[1])
            cnt += 1
            #print(di,cnt)
            tmp = tmp2
print(cnt-1)

Submission status: Runtime Error

Solution code:
import sys

def answer():
    input = sys.stdin.readline
    M = int(input())

    part = ''
    for i in range(M):
        d, c = input().split()
        part += d * int(c)

    _round = 0
    while len(part) > 1:
        _round += 1
        # 和10以上を探索
        mt10 = 0
        for i in range(1, len(part)):
            s1 = part[-i]
            s2 = part[-i-1]
            if int(s1) + int(s2) >= 10:
                mt10 = - i - 1
                break

        if mt10 == 0:
            s1 = part[0]
            s2 = part[1]
            s3 = part[2:]
            part = str(int(s1) + int(s2)) + s3
        else:
            s1 = part[:mt10]
            s2 = part[mt10]
            s3 = part[mt10+1]
            s4 = ''
            if mt10 < -2:
                s4 = part[mt10+2:]
            part = s1 + str(int(s2)+int(s3)) + s4

    print(_round)


if __name__ == '__main__':
    answer()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(1000000)

m = int(input())

def doit(s):
    if s in memo:
        return memo[s]
    if len(s) == 1:
        return 0
    res = float('inf')
    for i in range(len(s) - 1):
        val = str(int(s[i]) + int(s[i + 1]))
        res = min(res, 1 + doit(s[:i] + val + s[i + 2:]))
    memo[s] = res
    return res

a = []
for _ in range(m):
    d, c = input().split()
    a.append(d * int(c))
val = ''.join(a)
n = len(val)
memo = {}
print(doit(val))

Submission status: Runtime Error

Solution code:
def answer():
    input = sys.stdin.readline
    M = int(input())

    part = ''
    for i in range(M):
        d, c = input().split()
        part += d * int(c)

    _round = 0
    while len(part) > 1:
        _round += 1
        # 和10以上を探索
        mt10 = 0
        for i in range(1, len(part)):
            s1 = part[-i]
            s2 = part[-i-1]
            if int(s1) + int(s2) >= 10:
                mt10 = - i - 1
                break

        if mt10 == 0:
            s1 = part[0]
            s2 = part[1]
            s3 = part[2:]
            part = str(int(s1) + int(s2)) + s3
        else:
            s1 = part[:mt10]
            s2 = part[mt10]
            s3 = part[mt10+1]
            s4 = ''
            if mt10 < -2:
                s4 = part[mt10+2:]
            part = s1 + str(int(s2)+int(s3)) + s4

    print(_round)


if __name__ == '__main__':
    answer()

Submission status: Runtime Error

Solution code:
import sys
def input() -> str:
    return sys.stdin.readline()
m = int(input())
n = "".join(d * int(c) for d, c in [input().split() for i in range(m)])
cnt = 0
while int(n) >= 10:
  n = n[:-2] + str(int(n[-1]) + int(n[-2]))
  cnt += 1
print(cnt)

Submission status: Runtime Error

Solution code:
m = input()
int N = 0
for i in range(m):
  d = input()
  c = input()
  N = N*10 + d
  for i in range(c-1):
    N = N*10 + d
int count = 0
while N>9:
  tem1 = N%10
  tem2 = N%100
  t = tem1 + tem2
  N = N/100
  if t>9:
    N = N * 100 + t
    count += 1
  else:
    N = N * 10 + t
    count += 1
print("number of round", count)

Submission status: Runtime Error

Solution code:
m = int(input())
d, c = [], []
n = ''

for i in range(m):
    v1, v2 = map(int, input().split())
    d.append(v1)
    c.append(v2)
    n += str(v1) * v2

#print(n)

cnt = 0
length = sum(c)

for i in range(length-1):
    if int(n[i]) + int(n[i+1]) > 9:
        cnt += 1

#print(cnt)

ans = length - 1 + cnt

print(ans)

Submission status: Runtime Error

Solution code:
m = int(input())
ans = 0
num = ""
for i in range(m):
    d, c = map(int, input().split())
    if d == 0:
        ans += c
    else:
        k = str(d) * c
        while len(k) > 1:
            if len(k) > 2:
                k = str(int(k[0]) + int(k[1])) + k[2:]
            else:
                k = str(int(k[0]) + int(k[1]))
            ans += 1
        num += k
while len(num) != 1:
    if len(num) > 2:
        num = str(int(num[0]) + int(num[1])) + num[2:]
    else:
        num = str(int(num[0]) + int(num[1]))
    ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
#順番考えなくていいかとりあえず試す
m = int(input())
nm = []
for i in range(m):
    di,ci = map(int, input().split( ))
    nm.append((di,ci))
cnt = 0
tmp = 0
while nm:
    ci,di = nm.pop()
    if di>100:
        tmpy = 1
        tmpz = 1
        tmpx = ci*2
        if tmpx>=10:
            tmpx = str(tmpx)
            tmp2 = int(tmpx[0])+int(tmpx[1])
            tmpy += 1
        while tmpx != ci:
            tmpx += ci
            tmpy +=1
            tmpz +=1
            if tmpx>=10:
                tmpx = str(tmpx)
                tmp2 = int(tmpx[0])+int(tmpx[1])
                tmpy += 1
        tmp += tmpz*(di//tmpy)
        di -= (di//tmpy)*tmpy

    while di:
        cnt += 1
        #print(di,cnt)
        tmp += ci
        di-=1
        if tmp>=10:
            tmp = str(tmp)
            tmp2 = int(tmp[0])+int(tmp[1])
            cnt += 1
            #print(di,cnt)
            tmp = tmp2
print(cnt-1)

Submission status: Runtime Error

Solution code:
from collections import Counter

mod = 1000000007


def inverse(a):
    return pow(a, mod - 2, mod)


def usearch(x, a):
    lft = 0
    rgt = len(a) + 1
    while rgt - lft > 1:
        mid = (rgt + lft) // 2
        if a[mid] <= x:
            lft = mid
        else:
            rgt = mid
    return lft

memo = [
    [0, 1, 1],
    [1,10,9],
    [2,11,9],
    [3,4,3],
    [4,13,9],
    [5,14,9],
    [6,5,3],
    [7,16,9],
    [8,17,9],
    [9,2,1],
]


def main():
    m = int(input())
    dc = [[int(i) for i in input().split()] for _ in range(m)]
    s = []
    ans = 0
    for i in range(m):
        d = dc[i][0]
        c = dc[i][1]
        ans += c // memo[d][2] * memo[d][1]
        if d == 9:
            ans -= 2
            s.append(d)
        else:
            s.extend([d] * (c % memo[d][2]))

    t = s[0]
    for i in range(1, len(s)):
        ans += 1
        t += s[i]
        if t > 9:
            ans += 1
            t = t % 10 + t // 10

    print(ans)

main()

Submission status: Runtime Error

Solution code:
m = int(input())
dc = [0 for _ in range(m)]
ans = 0
for i in range(m):
    d, c = map(int, input().split())
    dc[i] = d * c
    ans += c - 1

# print(dc, ans)
SUM = 0
for i in range(m):
    SUM += dc[i] * 10 ** (m - i - 1)

# print(SUM)

i = 0
while (1):
    # print(i, ans, SUM)
    SUM -= dc[i] * 10 ** (m - i - 1) + dc[i + 1] * 10 ** ((m - i - 2))
    # print(SUM, 12121212)
    ans += 1
    tmp = dc[i] + dc[i + 1]
    # print(tmp, SUM, i)
    if len(str(tmp)) == 2:
        dc[i] = tmp // 10
        dc[i + 1] = tmp % 10
        SUM += tmp * 10 ** ((m - i - 2))
    else:

        dc[i + 1] = tmp
        SUM += tmp * 10 ** ((m - i - 2))
        i += 1

    if SUM <= 9:
        break

print(ans)

Submission status: Runtime Error

Solution code:
#%%
m = int(input())
d = [0] * m
c = [0] * m
for i in range(m):
    d[i], c[i] = map(int, input().split())

ans = 0
i = 0
flag = False

while len(d) > 1:
#for k in range(10):
    if flag:
        a, b = (d[i] + d[i+1])//10, (d[i] + d[i+1])%10
        tmp = min(c[i], c[i+1])
        if c[i] > c[i+1]:
            c[i] -= tmp
            d[i+1] = a
            d.insert(i+2, b)
            c.insert(i+2, c[i])
        elif c[i] < c[i+1]:
            c[i+1] -= tmp
            d[i] = a
            d.insert(i+1, b)
            c.insert(i+1, c[i])
        else:
            d[i] = a
            d[i+1] = b
        flag = False
        ans += 1
    else:
        if d[i] + d[i+1] <= 9:
            tmp = min(c[i], c[i+1])
            c[i] -= tmp
            c[i+1] -= tmp
            if c[i] > c[i+1]:
                d[i+1] = d[i] + d[i+1]
                c[i+1] = tmp
            elif c[i] < c[i+1]:
                d[i] = d[i] + d[i+1]
                c[i] = tmp
            else:
                d[i] = d[i] + d[i+1]
                c[i] = tmp
                d.pop(i+1)
                c.pop(i+1)
            ans += 1
        else:
            i += 1
            if i >= len(d)-1:
                i = 0
                flag = True
    #print(d, c)
print(ans)

Submission status: Runtime Error

Solution code:
def func(n, cnt, l):
    if int(n) < 10:
        return cnt
    cnt += 1
    for i in range(len(n) - 1):
        s = n[:i] + str(int(n[i]) + int(n[i + 1])) + n[i + 2:]
        l.append(func(s, cnt, l))
    return l


m = int(input())
d = ''
for i in range(m):
    a = list(map(int, input().split()))
    d += str(a[0]) * a[1]

cnt = 0
l = []
l = func(d, cnt, l)
l2 = []
for i in l:
    try:
        l2.append(int(i))
    except TypeError:
        continue
print(min(l2))

Submission status: Runtime Error

Solution code:
m = int(input())
ans = 0
num = ""
for i in range(m):
    d, c = map(int, input().split())
    if d == 0:
        ans += c
    else:
        k = str(d) * c
        while len(k) != 1:
            k = str(int(k[0]) + int(k[1])) + k[2:]
            ans += 1
        num += k
while len(num) != 1:
    num = str(int(num[0]) + int(num[1])) + num[2:]
    ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
def func(n, cnt, l):
    if int(n) < 10:
        return cnt
    cnt += 1
    for i in range(len(n) - 1):
        s = n[:i] + str(int(n[i]) + int(n[i + 1])) + n[i + 2:]
        l.append(func(s, cnt, l))
    return l


m = int(input())
d = ''
for i in range(m):
    a = list(map(int, input().split()))
    d += str(a[0]) * a[1]

cnt = 0
l = []
l = func(str(229), cnt, l)
l2 = []
for i in l:
    try:
        l2.append(int(i))
    except TypeError:
        continue
print(min(l2))

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:
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:
m = int(input())
ans = 0
num = ""
for i in range(m):
    d, c = map(int, input().split())
    if d == 0:
        ans += c
    else:
        k = str(d) * c
        while len(k) != 1:
            if len(k) == 2:
                k = str(int(k[0]) + int(k[1]))
            else:
                k = str(int(k[0]) + int(k[1])) + k[:2]
            ans += 1
        num += k
while len(num) != 1:
    print(num)
    if len(num) == 2:
        num = str(int(num[0]) + int(num[1]))
    else:
        num = str(int(num[0]) + int(num[1])) + num[:2]
    ans += 1
print(ans)