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: Wrong Answer

Solution code:
from collections import Counter,defaultdict,deque
import sys
import bisect
import math
import itertools
import string
import queue
import copy
# import numpy as np
# import scipy
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7

def inp(): # n=1
    return int(input())
def inpm(): # x=1,y=2
    return map(int,input().split())
def inpl(): # a=[1,2,3,4,5,...,n]
    return list(map(int, input().split()))
def inpls(): # a=['1','2','3',...,'n']
    return list(input().split())
def inplm(n): # x=[] 複数行
    return list(int(input()) for _ in range(n))
def inpll(n): # [[1,1,1,1],[2,2,2,2],[3,3,3,3]]
    return sorted([list(map(int, input().split())) for _ in range(n)])
    
def main():
    m=inp()
    now = 0
    ans = -1
    for _ in range(m):
        d,c = inpm()
        ans+=c
        now+=d*c
        x=now//10
        y=now-x*10
        ans+=x
        now=y
    print(ans)
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = -1
num = 0
for i in range(n):
    a,b = map(int,input().split())
    ans += b
    num += a * b
    if num > 9:
        ans += num // 9
        num %= 9
        if num == 0:
            num = 9
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = -1
num = 0
for i in range(n):
    a,b = map(int,input().split())
    ans += b
    num += a * b
    if num > 9:
        ans += (num-9) // 9
        num %= 9
        if num == 0:
            num = 9
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = -1
num = 0
for i in range(n):
    a,b = map(int,input().split())
    ans += b
    num += a * b
    if num > 9:
        ans += (num-9) // 9
        num %= 9
        if num == 0:
            num = 9
print(ans,num)

Submission status: Wrong Answer

Solution code:
M = int(input())
D = [0]*M
C = [0]*M
hoge = 0
fuga = 0
for i in range(M):
    a,b = map(int,input().split())
    D[i] = a
    C[i] = b
    hoge += b
    fuga += a * b

print(hoge-1+fuga//10)

Submission status: Wrong Answer

Solution code:
m = int(input())
info = [list(map(int, input().split())) for i in range(m)]

ans = 0
len_ans = 0
for i in range(m):
    a, b = info[i]
    len_ans += b
    ans += a*b
print(len_ans +  ans // 10 - 1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
M=int(input())
DC=[tuple(map(int,input().split())) for i in range(M)]

ANS=0
keta=0
for d,c in DC:
    ANS+=d*c
    keta+=c

print(ANS//10+keta-1)

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.readline
M = int(readline())
ans = 0
res = 0
for _ in range(M):
    d, c = map(int, readline().split())
    ans += d*c
    res += c

print(ans//9 + res - 1)

Submission status: Wrong Answer

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

tmp = sumd
while tmp > 10:
    tmp = tmp // 10
    sumd += tmp

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

Submission status: Wrong Answer

Solution code:
M=int(input())
C=0
S=0
for i in range(M):
    d,c=map(int,input().split())
    C+=c
    S+=d*c
print(C-1+S//10)

Submission status: Wrong Answer

Solution code:
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


M = int(sys.stdin.buffer.readline())
DC = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(M)]

ans = 0
num = 0
for i, (d, c) in enumerate(DC):
    if not (i == M - 1 and num == 0):
        n = num + d * c
        ans += c
        ans += n // 10
        num = n % 10
print(ans - 1)

Submission status: Wrong Answer

Solution code:
m=int(input())
A=[]
keta=0
for i in range(m):
  c,d=map(int,input().split())
  A.append([c,d])
sum=0
for i in range(m):
  sum+=A[i][0]*A[i][1]
  keta+=A[i][1]
print(keta-1+sum//10)

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = -1
num = 0
for i in range(n):
    a,b = map(int,input().split())
    ans += b
    num += a * b
    if num > 9:
        ans += num // 9
        num %= 9
print(ans)

Submission status: Wrong Answer

Solution code:
m = int(input())
L = []
for _ in range(m):
    L.append(list(map(int, input().split())))

short = []
additional = 0
for elm in L:
    additional += elm[1] - 1 
    total = elm[0] * elm[1] - 1
    additional += total // 9
    res = total % 9
    short.append(res)

prev = 0
additional += len(short) - 1
for elm in short:
    prev = prev + elm
    if prev > 9:
        prev -= 9
        additional += 1

print(additional)

Submission status: Wrong Answer

Solution code:
m = int(input())
x = 0
n = 0
for i in range(m):
    d, c = map(int, input().split())
    x += d * c
    n += c
print(x // 9 + n - 1)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
m=int(input())
now=0
g=0
for _ in range(m):
    d,c=map(int,input().split())
    now+=d*c
    g+=c
now//=10
print(now+(g-1))

Submission status: Wrong Answer

Solution code:
M = int(input())
s = 0
a = 0
for _ in range(M):
    d, c = map(int, input().split())
    s += d * c
    a += c
print(a - 1 + s // 10)

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = -1
num = 0
for i in range(n):
    a,b = map(int,input().split())
    ans += b
    num += a * b
    ans += num // 10
    num %= 10
    ans %= (10 ** 9 + 7)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = -1
num = 0
for i in range(n):
    a,b = map(int,input().split())
    ans += b
    num += a * b
    ans += num // 10
    num %= 10
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
m = int(raw_input())

s = 0
t = -1

for i in range(m):
  a, b = map(int, raw_input().split())
  s += a * b
  
  t += b
  t += s / 10
  s %= 10

print(t)

Submission status: Wrong Answer

Solution code:
m=int(input())
total=0
keta=0
for i in range(m):
	d,c=map(int,input().split())
	total+=d*c
	keta+=c
print(keta-1+total//9)

Submission status: Wrong Answer

Solution code:
m=int(input())
total=0
keta=0
for i in range(m):
	d,c=map(int,input().split())
	total+=d*c
	keta+=c
print(keta-1+total//10)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
from collections import defaultdict, deque, Counter
from copy import deepcopy
from bisect import bisect, bisect_right, bisect_left
from heapq import heapify, heappop, heappush
    
input = sys.stdin.readline
def RD(): return input().rstrip()
def F(): return float(input().rstrip())
def I(): return int(input().rstrip())
def MI(): return map(int, input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def LF(): return list(map(float,input().split()))
def Init(H, W, num): return [[num for i in range(W)] for j in range(H)]
    
    
def main():
    M = I()
    L = [LI() for i in range(M)]
    L = L[::-1]
    temp = 0
    ans = 0
    
    for a, b in L:
        for j in range(b):
            temp += a
            ans += 1
            if temp >= 10:
                temp = 1 + temp % 10
                ans += 1

    print(ans-1)
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
m = int(input())
cnt = 0  ; d0 = 0 
for _ in range(m):
    d, c = map(int, input().split())
    for i in range(c):
        w = d0 + d
        if w > 9:
            d0 = w - 9
            cnt += 2
        else:
            d0 = w
            cnt += 1
print(cnt-1)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
M = int(input())
deq = deque()

for _ in range(M):
  d,c = map(int,input().split())
  for _ in range(c):
    deq.append(d)

def is_handred():
  if deq[0]==1 and deq[1]==0 and deq[2]==0:
    return True
  return False

times = 0
while len(deq) > 1:
  if len(deq) == 3 and is_handred():
    deq = deque([1,0])
  else:
    i,j = deq.popleft(), deq.popleft()
    for x in list(str(i+j)):
      deq.append(int(x))

  times += 1

print(times)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**9)

next_num = {
  0:(1, 0), 1:(1, 2), 2:(1, 4), 3:(1, 6), 4:(1, 8),
  5:(2, 1), 6:(2, 3), 7:(2, 5), 8:(2, 7), 9:(2, 9)
}
memo = {}
def rec_calc(d, c):
  global memo
  if (d, c) in memo:
    return memo[(d, c)]
  if c == 1:
    memo[(d, c)] = (0, d)
    return (0, d)
  elif c == 2:
    n = 2 * d
    if n <= 9:
      memo[(d, c)] = (1, n)
      return (1, n)
    else:
      a1 = n % 10
      a2 = n // 10
      memo[(d, c)] = (2, a1+a2)
      return (2, a1+a2)
  else:
    t, nn = next_num[d]
    time, n = rec_calc(nn, c//2)
    if c % 2 == 0:
      res = ((c//2)*t + time, n)
      memo[(d, c)] = res
      return res
    else:
      n += d
      if n <= 9:
        res = ((c//2)*t+time+1, n)
        memo[(d, c)] = res
      else:
        a1 = n % 10
        a2 = n // 10
        res = ((c//2)*t+time+2, a1+a2)
        memo[(d, c)] = res
      return res

M = int(input())
ans = 0
D = []
for i in range(M):
  d, c = map(int, input().split())
  time, n = rec_calc(d, c)
  ans += time
  D.append(n)

d1 = D.pop()
while D:
  d2 = D.pop()
  n = d1 + d2
  if n <= 9:
    ans += 1
    d1 = n
  else:
    ans += 2
    d1 = (n%10) + (n//10)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

# import math
# import bisect

# import copy
# import heapq
# from collections import deque
# import decimal

# sys.setrecursionlimit(100001)
# INF = sys.maxsize
# MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===


def main():
    n = ni()
    d = []
    for i in range(n):
        num, time = ns()
        for j in range(time):
            d.append(str(num))

    cnt = 0
    while True:
        tmp = 0
        idx = -1
        for i in range(len(d) - 1):
            tmp_tmp = int(d[i]) + int(d[i + 1])
            if tmp_tmp > 9:
                tmp_tmp = tmp_tmp % 10 + 1

            if tmp < tmp_tmp:
                tmp = tmp_tmp
                idx = i

        d = d[:idx] + list(str(int(d[idx]) + int(d[idx + 1]))) + d[idx + 2:]
        tmp_d = "".join(d)
        if int(tmp_d) < 10:
            break
        cnt += 1

    print(cnt+1)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
M = int(input())
N = []

for _ in range(M):
    d, c = map(int, input().split())
    for _ in range(c):
        N.append(d)

Q = deque([[N, 0]])# 人数、行った予選の回数
ans = 0

while Q:
    cur_N, held = Q.popleft()
    if len(cur_N) == 1:
        ans = max(held, ans)
        continue
    for i in range(len(cur_N) - 1):
        # [2, 2, 9] -> [4, 9]
        # [2, 2, 9] -> [2, 1, 1]
        # などの処理
        new = list(map(int, list(cur_N[0:i] + list(str(cur_N[i] + cur_N[i + 1])) + cur_N[i + 2:])))
        Q.append([new, held + 1])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def g(x):
    if len(x)==1:
        return x,0
    y = int(x[0])+int(x[1])
    if y<=9:
        return str(y),1
    else:
        z = int(str(y)[0])+int(str(y)[1])
        return str(z),2
def f(d,c):
    if c==1:
        return str(d),0
    if c==2:
        return g(str(d)+str(d))
    if c%2==0:
        x,n = f(d,c//2)
        return g(x+x)[0],2*n+g(x+x)[1]
    else:
        x,n = f(d,(c-1)//2)
        y,m = g(x+x)
        return g(y+str(d))[0],2*n+m+g(y+str(d))[1]
M = int(input())
x = []
cnt = 0
for _ in range(M):
    d,c = map(int,input().split())
    y,n = f(d,c)
    x.append(y)
    cnt += n
while len(x)>1:
    y0 = x.pop()
    y1 = x.pop()
    y,n = g(y0+y1)
    x.append(y)
    cnt += n
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
def g(x):
    if len(x)==1:
        return x,0
    y = int(x[0])+int(x[1])
    if y<=9:
        return str(y),1
    else:
        z = int(str(y)[0])+int(str(y)[1])
        return str(z),2
def f(d,c):
    if c==1:
        return str(d),0
    if c==2:
        return g(str(d)+str(d))
    if c%2==0:
        x,n = f(d,c//2)
        return g(x+x)[0],2*n+g(x+x)[1]
    else:
        x,n = f(d,(c-1)//2)
        y,m = g(x+x)
        return g(y+str(d))[0],2*n+m+g(y+str(d))[1]
M = int(input())
x = deque([])
cnt = 0
for _ in range(M):
    d,c = map(int,input().split())
    y,n = f(d,c)
    x.append(y)
    cnt += n
while len(x)>1:
    y0 = x.popleft()
    y1 = x.popleft()
    y,n = g(y0+y1)
    x.appendleft(y)
    cnt += n
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

from collections import deque

q = deque()

def calc(d, c):
    if c&1:
        q.append(d)
    if c == 1:
        return 0
    d *= 2
    if d >= 10:
        return c//2 * 2 + calc(1+d%10, c//2)
    return c//2 + calc(d, c//2)
    

def main():
    M = int(input())
    ans = 0
    for i in range(M):
        d, c = map(int, input().split())
        ans += calc(d, c)
    
    num = q.popleft()
    while len(q) > 0:
        t = q.popleft()
        num += t
        if num >= 10:
            num = 1 + num%10
            ans += 1
        ans += 1
    
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def g(i,j):
    if i+j<10:
        return 1,i+j
    else:
        a = i+j
        b = a%10+a//10
        return 2,b
def f(c,d):
    if d==1:
        return 0,c
    if d==2:
        return g(c,c)
    if d%2==0:
        a,b = f(c,d//2)
        x,y = g(b,b)
        return 2*a+x,y
    else:
        a,b = f(c,d-1)
        x,y = g(c,b)
        return a+x,y
M = int(input())
A = [list(map(int,input().split())) for _ in range(M)]
cnt = 0
B = []
for i in range(M):
    c,d = A[i][0],A[i][1]
    a,b = f(c,d)
    cnt += a
    B.append(b)
b = B[0]
for i in range(1,M):
    a,b = g(b,B[i])
    cnt += a
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
def g(i,j):
    if i+j<10:
        return 1,i+j
    else:
        a = i+j
        b = a%10+a//10
        return 2,b
def f(c,d):
    if d==1:
        return 0,c
    if d==2:
        return g(c,c)
    if d%2==0:
        return f(c,d//2)[0]*2+g(f(c,d//2)[1],f(c,d//2)[1])[0],g(f(c,d//2)[1],f(c,d//2)[1])[1]
    else:
        return f(c,d-1)[0]+g(c,f(c,d-1)[1])[0],g(c,f(c,d-1)[1])[1]
M = int(input())
A = [list(map(int,input().split())) for _ in range(M)]
cnt = 0
B = []
for i in range(M):
    c,d = A[i][0],A[i][1]
    a,b = f(c,d)
    cnt += a
    B.append(b)
b = B[0]
for i in range(1,M):
    a,b = g(b,B[i])
    cnt += a
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque

def q(d, c, dq):
    if c == 1: 
        if d < 10: 
            dq.append(d)
            return 0
        else:
            a = d //10 + d % 10
            if a < 10: 
                dq.append(a)
                return 1
            else: 
                dq.append(a-9)
                return 2
    a = d * 2
    if a < 10: count = q(a, c//2, dq)
    else:
        count = q(a - 9, c//2, dq)
        count += c//2
    count += c//2
    if c % 2 == 1: dq.append(d)
    return count

def solve():
    input = sys.stdin.readline
    M = int(input())
    count = 0
    dq = deque()
    digit = 0
    for _ in range(M):
        d, c = map(int, input().split())
        add = q(d, c, dq)
        count += add
    
    digit = len(dq)
    for _ in range(digit - 1):
        #print(dq)
        a = int(dq.pop())
        a += int(dq.pop())
        if a >= 10:
            dq.append(str(a % 10 + a // 10))
            count += 2
        else: 
            dq.append(str(a))
            count += 1

    print(count)
    
    return 0

if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque

def q(d, c):
    if c == 1: 
        if d < 10: return str(d), 0
        else:
            a = d //10 + d % 10
            if a < 10: return str(a), 1
            else: return str(a - 9), 2
    a = d * 2
    if a < 10:
        lem, count = q(a, c//2)
    else:
        lem, count = q(a - 9, c//2)
        count += c // 2
    count += c//2
    if c % 2 == 1: lem += str(d)
    return lem, count

def solve():
    input = sys.stdin.readline
    M = int(input())
    N = ""
    count = 0
    dq = deque()

    for _ in range(M):
        d, c = map(int, input().split())
        lem, add = q(d, c)
        for i in range(len(lem)): 
            dq.append(lem[i])
        count += add
    
    digit = len(dq)
    for _ in range(digit - 1):
        #print(dq)
        a = int(dq.pop())
        a += int(dq.pop())
        if a >= 10:
            dq.append(str(a % 10 + a // 10))
            count += 2
        else: 
            dq.append(str(a))
            count += 1

    print(count)
    
    return 0

if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque

def q(d, c):
    if c == 1: 
        if d < 10: return str(d), 0
    if d >= 10: 
        lem, count = q(d // 10 + d % 10, c)
        return lem, count + c
    elif c % 2 == 0:
        lem, count = q(d * 2, c // 2)
        return lem, count + c // 2
    elif c % 2 == 1:
        lem, count = q(d * 2, c //2)
        return lem + str(d), count + c // 2

def solve():
    input = sys.stdin.readline
    M = int(input())
    N = ""
    count = 0
    dq = deque()

    for _ in range(M):
        d, c = map(int, input().split())
        lem, add = q(d, c)
        for i in range(len(lem)): 
            dq.append(lem[i])
        count += add
    
    digit = len(dq)
    for _ in range(digit - 1):
        #print(dq)
        a = int(dq.pop())
        a += int(dq.pop())
        if a >= 10:
            dq.append(str(a % 10 + a // 10))
            count += 2
        else: 
            dq.append(str(a))
            count += 1

    print(count)
    
    return 0

if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

Solution code:
import sys

def q(d, c):
    if c == 1: return str(d), 0
    if d >= 10: 
        lem, count = q(d // 10 + d % 10, c)
        return lem, count + c
    elif c % 2 == 0:
        lem, count = q(d * 2, c // 2)
        return lem, count + c // 2
    elif c % 2 == 1:
        lem, count = q(d * 2, c //2)
        return lem + str(d), count + c // 2

def solve():
    input = sys.stdin.readline
    M = int(input())
    N = ""
    count = 0
    for _ in range(M):
        d, c = map(int, input().split())
        lem, add = q(d, c)
        N += lem
        count += add

    while len(N) > 1:
        N = str(int(N[0]) + int(N[1])) + N[2:]
        count += 1

    print(count)
    
    return 0

if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

Solution code:
M = int(input())
D, C = [0] * M, [0] * M
total = 0
count = 0
for i in range(M):
	D[i], C[i] = map(int, input().split())
	count += D[i] * C[i]
	while count > 9:
		total += 1
		count -= 9
print(sum(C)-1+total)

Submission status: Time Limit Exceeded

Solution code:
M=int(input())
N=[]
cishu=0
zuizhongliebiao=[]
for i in range(M):
    N.append(list(map(int,input().split())))
for i in range(M):
    wuyong = N[i][0]
    for j in range(N[i][-1]-1):
        wuyong=wuyong+N[i][0]
        cishu+=1
        if wuyong>=10:
            wuyong=wuyong-9
            cishu+=1
    zuizhongliebiao.append(wuyong)
wuyong2=zuizhongliebiao[0]
for k in range(M-1):
    wuyong2=wuyong2+zuizhongliebiao[k+1]
    cishu+=1
    if wuyong2>=10:
        wuyong2=wuyong2-9
        cishu+=1
print(cishu)

Submission status: Time Limit Exceeded

Solution code:
M=int(input())
N=[]
cishu=0
zuizhongliebiao=[]
for i in range(M):
    N.append(list(map(int,input().split())))
for i in range(M):
    wuyong = N[i][0]
    for j in range(N[i][-1]-1):
        wuyong=wuyong+N[i][0]
        cishu+=1
        if wuyong>=10:
            wuyong=wuyong-9#求个位与十位的和
            cishu+=1
    zuizhongliebiao.append(wuyong)
wuyong2=zuizhongliebiao[0]
for k in range(M-1):
    wuyong2=wuyong2+zuizhongliebiao[k+1]
    cishu+=1
    if wuyong2>=10:
        wuyong2=wuyong2-9
        cishu+=1
print(cishu)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

input = stdin.readline

m, *t = map(int, open(0).read().split())

from functools import lru_cache


@lru_cache()
def cal(d, c):
    if c == 1:
        return [0, d]
    t = 1 + (d + d >= 10)
    a = (d + d >= 10) + (d + d) % 10

    ca = cal(a, c // 2)

    if c % 2:
        return [(c // 2) * t + ca[0] + 1 + (ca[1] + d >= 10), (ca[1] + d >= 10) + (ca[1] + d) % 10]
    else:
        return [(c // 2) * t + ca[0], ca[1]]


ans = 0
now = []
for d, c in zip(*[iter(t)] * 2):
    ca = cal(d, c)
    ans += ca[0]
    now.append(ca[1])

pre = 0
for i in now:
    pre = pre + i
    ans += 1
    if pre >= 10:
        ans += 1
        pre = pre % 10 + 1

print(ans - 1)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

input = stdin.readline

m, *t = map(int, open(0).read().split())

from functools import lru_cache


@lru_cache(None)
def cal(d, c):
    if c == 1:
        return [0, d]
    t = 1 + (d + d >= 10)
    a = (d + d >= 10) + (d + d) % 10

    ca = cal(a, c // 2)

    if c % 2:
        return [(c // 2) * t + ca[0] + 1 + (ca[1] + d >= 10), (ca[1] + d >= 10) + (ca[1] + d) % 10]
    else:
        return [(c // 2) * t + ca[0], ca[1]]


ans = 0
now = []
for d, c in zip(*[iter(t)] * 2):
    ca = cal(d, c)
    ans += ca[0]
    now.append(ca[1])

pre = 0
for i in now:
    pre = pre + i
    ans += 1
    if pre >= 10:
        ans += 1
        pre = pre % 10 + 1

print(ans - 1)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

input = stdin.readline

m, *t = map(int, open(0).read().split())


def cal(d, c):

    if c == 1:
        return [0, d]
    t = 1 + (d + d >= 10)
    a = (d + d >= 10) + (d + d) % 10

    ca = cal(a, c // 2)

    if c % 2:
        return [(c // 2) * t + ca[0] + 1 + (ca[1] + d >= 10), (ca[1] + d >= 10) + (ca[1] + d) % 10]
    else:
        return [(c // 2) * t + ca[0], ca[1]]


ans = 0
now = []
for d, c in zip(*[iter(t)] * 2):
    ca = cal(d, c)
    ans += ca[0]
    now.append(ca[1])

pre = 0
for i in now:
    pre = pre + i
    ans += 1
    if pre >= 10:
        ans += 1
        pre = pre % 10 + 1

print(ans - 1)

Submission status: Time Limit Exceeded

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, 1])
        else:
            s.append([d, (c % memo[d][2])])
    if not s:
        print(ans)
        return

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

    print(ans)

main()

Submission status: Time Limit Exceeded

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


def main():
    M = int(input())
    digits = []
    S = 0

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

        if d == 0 and i == 0:
            continue

        for i in range(c):
            digits.append(d)
            S += d

    print((len(digits) - 1) + (S - 1) // 9)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
M = int(input())
ans = 0
hoge = 0
for _ in range(M):
    d,c = map(int,input().split())
    while c:
        if c % 2 == 1:
            hoge += d
            ans += 1
            if hoge > 9:
                hoge = (hoge%10)+1
                ans += 1
        c //= 2
        ans += c
        d = d*2
        if d > 9:
            ans += c
            d = d%10+1

print(ans-1)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

def main():
    M = int(input())
    wa = 0
    ans = 0
    for _ in range(M):
        d, c = [int(x) for x in input().split()]
        for _ in range(c):
            wa += d
            ans += 1
            if wa >= 10:
                ans += 1
                wa = wa % 10 + 1
    
    print(ans - 1)

    



if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
M = int(input())
now = 0
ans = 0

for _ in range(M):
    d, c = map(int, input().split())
    for _ in range(c):
        now += d
        ans += 1
        if now >= 10:
            now = now // 10 + now % 10
            ans += 1

print(ans - 1)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

def func(D, C):
    A, B = D, D
    rests = []
    num = 0
    while True:
        if C%2:
            rests.append(A)
            C -= 1
        if C == 0:
            break
        num += C//2
        X = A+B
        if X >= 10:
            A, B = X//10, X%10
        else:
            A, B = X, X
            C //= 2

    rests = rests[::-1]
    ret, E = func_naive(rests)
    return num+ret, E

def func_naive(Xs):
    num = 0
    while True:
        if len(Xs) == 1:
            break
        B = Xs.pop()
        A = Xs.pop()
        X = A+B
        if X >= 10:
            Xs.append(X//10)
            Xs.append(X%10)
        else:
            Xs.append(X)
        num += 1
    return num, Xs[0]

M = int(input())
dcs = [tuple(map(int, input().split())) for _ in range(M)]

ans = 0
Es = []
for d, c in dcs:
    num, E = func(d, c)
    ans += num
    Es.append(E)

num, E = func_naive(Es)
ans += num

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.buffer.readline
M = int(input())
ans = 0
cnt = [1,1,1,1,1,2,2,2,2,2]
res = [0,2,4,6,8,1,3,5,7,9]
    
ans = 0
s = ""
for i in range(M):
    d,c= list(map(int,input().split()))
    t = ""
    while c:
        t += str(d)*(c % 2)
        c = c // 2
        ans += c * cnt[d]
        d = res[d]
    s += t

s = list(map(int,s))
while len(s) > 1:
    a = s.pop()
    b = s.pop()
    t = [a+b] if (a+b) < 10 else [1, (a+b)%10]
    s.extend(t)
    ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

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

    res = []

    total = 0
    for i in range(m):
        d, c = sys.stdin.readline().split()
        s = d
        count = 0
        for _ in range(int(c)-1):
            s = str(int(s) + int(d))
            count += 1
            if len(s) == 2:
                s = str(int(s[0]) + int(s[1]))
                count += 1
        total += count
        res.append(s)
    

    s = res[0]
    for i in range(1, m):
        s = str(int(s) + int(res[i]))
        total += 1
        if len(s) == 2:
            s = str(int(s[0]) + int(s[1]))
            total += 1
    print(total)
    

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

M = int(input())
D = defaultdict(int)
for i in range(M):
    d,c = list(map(int,input().split()))
    D[d] += c

s = 0
while True:
    a = list(filter(lambda x:x,D.values()))
    if len(a) != 1:
        k1,k2 = list(D.keys())[:2]
        v = min(D[k1],D[k2])
        s += v
        D[k1] -= v
        D[k2] -= v
        D[(k1+k2)%10] += v
        if k1+k2 >= 10:
            D[1] += v
        if D[k1] == 0:
            del D[k1]
        if D[k2] == 0:
            del D[k2]
        continue
    if a[0] != 1:
        k = list(D.keys())[0]
        v = D[k] // 2
        s += v
        D[k] -= 2 * v
        D[(k*2)%10] += v
        if k * 2 >= 10:
            D[1] += v
        if D[k] == 0:
            del D[k]
        continue
    break

print(s)