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: Time Limit Exceeded

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] = (i + 1) % 10
    # c[i] = 10**15 // n
    d[i], c[i] = read_ints()
    if c[i] == 0:
        c[i] = 1/c[i]
    # 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):
    if not (c > 0):
        while True:
            pass

    if c < 0:
        c = 0
        c = 1 / c
        
    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

    # c % 2 == 1
    # c != 1

    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: Time Limit Exceeded

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] = (i + 1) % 10
    # c[i] = 10**15 // n
    d[i], c[i] = read_ints()
    if c[i] == 0:
        c[i] = 1/c[i]
    # 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):
    if not (c > 0):
        while True:
            pass
    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

    # c % 2 == 1
    # c != 1

    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: Time Limit Exceeded

Solution code:

def sum_lr(l, r, cnt):
    cnt += 1
    lr = l + r
    if lr < 10:
        l = lr
        return l, cnt
    else:
        l = lr // 10
        r = lr % 10
        return sum_lr(l, r, cnt)

def dc_compression(d, c, ret, cnt):
    if c == 1:
        ret.append((d, 1))
        return ret, cnt
    
    if c % 2 == 1:
        ret.append((d, 1))
        c -= 1
    d, tmp_cnt = sum_lr(d, d, 0)
    c = c // 2
    cnt += tmp_cnt * c
    #print(d, c, ret, cnt)
    return dc_compression(d, c, ret, cnt)
    
    
    
        

M = int(input())

nums = []
l = 0
cnt = 0
for _ in range(M):
    d, c = map(int, input().split())
    tmp_nums, tmp_cnt= dc_compression(d,c,[], 0)
    nums += tmp_nums
    cnt += tmp_cnt
    
for d, c in nums:
    for _ in range(c):
        l, tmp_cnt = sum_lr(l,d,0)
        cnt += tmp_cnt

print(cnt-1)

Submission status: Time Limit Exceeded

Solution code:
def judge(a, b):
    if a+b <= 9:
        return a+b
    return a+b-9

n = int(input())
ans = 0
key = 0
for i in range(n):
    a, b = map(int, input().split())
    if a == 0:
        ans += b
        continue
    for j in range(b):
        if key + a <= 9:
            ans += 1
        else:
            ans += 2
        key = judge(key, a)
print(ans-1)

Submission status: Time Limit Exceeded

Solution code:
M = int(input())
DC = [list(map(int, input().split())) for _ in range(M)]

n = ""
ans = 0

for d, c in DC:
    total = 0
    if c >= 2:
        for i in range(c):
            total += d
            ans += 1
            if total >= 10:
                total = 1 + total % 10
                ans += 1
        ans -= 1
    else:
        total = d
    n = n + str(total)
total = 0
if len(n) >= 2:
    for i in n:
        i = int(i)
        total += i
        ans += 1
        if total >= 10:
            total = 1 + total % 10
            ans += 1
    ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
M = int(input())
d = [0] * M
c = [0] * M
S = []
for i in range(M):
    d[i], c[i] = map(int, input().split())
    for j in range(c[i]):
        S.append(d[i])

cnt = 0
while len(S) > 1:
    S2 = [0] * (len(S)-1)
    
    for i in range(len(S)-1):
        S2[i] = S[i] + S[i+1]
    if max(S2) > 9:
        S[S2.index(max(S2))] = max(S2) // 10
        S[S2.index(max(S2)) + 1] = max(S2) % 10
    else:
        S[S2.index(max(S2))] = max(S2)
        del S[S2.index(max(S2)) + 1]

    cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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] = (i + 1) % 10
    # c[i] = 10**15 // 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):
    if not (c > 0):
        while True:
            pass
    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: Time Limit Exceeded

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())
    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: Time Limit Exceeded

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()
    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: Time Limit Exceeded

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()
    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: Time Limit Exceeded

Solution code:

from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())


ans = 0
M = inp()
zan_all = deque()
for m in range(M):
    d,c = inpl()
    zan = []

    while True:
        if c == 1:
            zan_all.appendleft(d)
            break
        elif c%2 == 0:
            if d == 0:
                ans += c
                break
            elif d <= 4:
                ans += c//2
                d *= 2
                c //= 2
            else:
                ans += c
                d = d*2 - 9
                c //= 2
        else:
            zan_all.appendleft(d)
            c -= 1


for i in range(1,len(zan_all)):
    d = zan_all[i] + zan_all[i-1]
    if d >= 10:
        zan_all[i] = d - 10 + 1
        ans += 2
    else:
        zan_all[i]   = d
        ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
l = 0
cnt = 0
def sum_lr(r):
    global l
    global cnt
    cnt += 1
    lr = l + r
    if lr < 10:
        l = lr
    else:
        l = lr // 10
        r = lr % 10
        sum_lr(r)


M = int(input())

nums = []
for _ in range(M):
    d, c = map(int, input().split())
    nums.append((d,c))
    
for d, c in nums:
    for _ in range(c):
        sum_lr(d)

print(cnt-1)

Submission status: Time Limit Exceeded

Solution code:

from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())


def calc(d,c):
    global zan
    if c == 1:
        zan.append(d)
        return 0
    elif c%2 == 0:
        if d == 0:
            return c
        elif d <= 4:
            return calc(d*2,c//2)+1*(c//2)
        else:
            return calc(d*2-9,c//2)+2*(c//2)
    else:
        zan.append(d)
        return calc(d,c-1)


ans = 0
M = inp()
zan_all = []
for m in range(M):
    d,c = inpl()
    zan = []
    ans += calc(d,c)
    zan.reverse()
    zan_all += zan

for i in range(1,len(zan_all)):
    d = zan_all[i] + zan_all[i-1]
    if d >= 10:
        zan_all[i] = d - 10 + 1
        ans += 2
    else:
        zan_all[i]   = d
        ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(1000000)
M=int(input())
N=[]
for i in range(M):
  d,c=map(int,input().split())
  for j in range(c):
    N.append(d)

ans=0
def dfs(n,d):
  global ans
  l=len(n)
  if l<=1:
    ans=max(ans,d)
  else:
    for i in range(l-1):
      x=n[i]+n[i+1]
      nn=[]
      for j in range(i):
        nn.append(n[j])
      if x>=10:
        nn.append(1)
        nn.append(x-10)
      else:
        nn.append(x)
      for j in range(i+2,l):
        nn.append(n[j])
      dfs(nn,d+1)
      
dfs(N,0)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
m,*t=map(int,open(0).read().split())
*z,=zip(*[iter(t)]*2)
a=0
flag=False
for d,c in z:
    if flag:
        m=[b]
    else:
        m=[]
        flag=True
    while c//2:
        cd=c//2
        a+=cd
        if c%2:
            m+=d,
        d*=2
        if d>9:
            a+=cd
            d=sum(map(int,str(d)))
        c=cd
    for t in m[::-1]:
        d+=t
        a+=1
        if d>9:
            d=sum(map(int,str(d)))
            a+=1
    b=d
print(a)

Submission status: Time Limit Exceeded

Solution code:
m = int(input())
cnt = 0
res = None
for _ in range(m):
    d,c = map(int,input().split())
    while c:
        if c%2:
            if res is None:
                res = d
            else:
                res += d
                cnt += 1
                if res > 9:
                    res = res//10 + res%10
                    cnt += 1 
        d *= 2
        c //= 2
        cnt += c
        if d > 9:
            d = d//10 + d%10
            cnt += c
print(cnt)

Submission status: Accepted

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:
        temp += a*b
        ans += b
        while temp >= 10:
            temp2 = temp // 10
            ans += temp2
            temp = (temp % 10) + temp2

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

Submission status: Accepted

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

M = int(input())
dcs = map(int, read().split())

digit_sum = 0
digit_count = 0

for d, c in zip(dcs, dcs):
    digit_sum += d * c
    digit_count += c

print((digit_sum - 1) // 9 + digit_count - 1)

Submission status: Accepted

Solution code:
def main():
    m=int(input())
    sm=0
    dig=0
    for _ in range(m):
        d,c=map(int,input().split())
        sm+=d*c
        dig+=c
    print((dig-1)+((sm-1)//9))

main()

Submission status: Accepted

Solution code:
import sys
from collections import deque
import bisect
import copy
import heapq
import itertools
import math
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


def f(S, res, d, c):
    if c == 0:
        return S, res
    
    if c % 2 == 1:
        S += d
        res += 1
        if S >= 10:
            S -= 9
            res += 1
        
    t = d * 2
    res += c // 2
    if t >= 10:
        t -= 9
        res += c // 2
    return f(S, res, t, c // 2)


def main():
    M = int(input())
    S = 0
    res = -1
    for _ in range(M):
        d, c = read_values()
        S, res = f(S, res, d, c)
    
    print(res)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())

dnum =0
dsum =0

for i in range(n):
  d,c=map(int,input().split())
  dnum += c
  dsum += d*c
  
ans = dnum-1 + (dsum-1)//9

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
M=int(input())
D,S=0,0
for _ in range(M):
  d,c=map(int,input().split())
  D+=c;S+=d*c
print(D-1+(S-1)//9)

Submission status: Accepted

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

M = int(input())
D,S = 0,0
for _ in range(M):
  d,s = map(int, input().split())
  D += s
  S += d*s
  
print(D-1+(S-1)//9)

Submission status: Accepted

Solution code:
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())


M = I()
digit = 0  # 桁数
digit_sum = 0  # 桁和
for i in range(M):
    d,c = MI()
    digit += c
    digit_sum += d*c

print(digit+(digit_sum-1)//9-1)

# 和が9以下の2数を選ぶ ⇒ 桁数:-1,桁和:±0
# 和が10以上の2数を選ぶ ⇒ 桁数:±0,桁和:-9

Submission status: Accepted

Solution code:
def main():
    M = int(input())
    n, s = 0, 0
    for _ in range(M):
        d, c = list(map(int, input().split()))
        n += c
        s += d * c
    # 9 * n + s から 9 ずつ減らしていって、 10 ～ 18 になったら終わり
    print((9 * n + s - 10) // 9)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def steps(c, s):
    # c: No. of elements,  s: sum of elements
    q, r = divmod(s, 9)
    if r != 0:
        cnt = (c-1) + q
    else:
        cnt = (c-1) + q-1
        r = 9
    return cnt, r

m = int(input())
tot_c, tot_s = 0, 0
for _ in range(m):
    d, c = map(int, input().split())
    tot_c += c
    tot_s += c*d

ans, r = steps(tot_c, tot_s)
print(ans)

Submission status: Accepted

Solution code:
def steps(c, s):
    # c: No. of elements,  s: sum of elements
    q, r = divmod(s, 9)
    if r != 0:
        cnt = (c-1) + q
    else:
        cnt = (c-1) + q-1
        r = 9
    return cnt, r

m = int(input())
r0 = 0; ans = 0   
for _ in range(m):
    d, c = map(int, input().split())
    cnt, r = steps(c, d*c)
    ans += cnt
    cnt, r0 = steps(2, r0+r)
    ans += cnt
print(ans-1)

Submission status: Accepted

Solution code:
mat = [[[0, 0]]*10 for _ in range(10)]
def dsr(i):
    x, cnt = 0, 0
    for j in range(10):
        w = x + i
        if w > 9:
            x = w - 9
            cnt += 2
        else:
            x = w
            cnt += 1
        mat[i][j] = [x, cnt-1]
    #mat[i][0][0] = mat[i][9][0]
    x0 = mat[i][9][1] - mat[i][8][1]   #; print('x0', x0)
    for j in range(10):
        mat[i][j][1] += x0 

for i in range(10):
    dsr(i)
#print2(mat)

m = int(input())
cnt = 0; x0 = 0   
for _ in range(m):
    d, c = map(int, input().split())
    q, r = divmod(c-1, 9)
    cnt += mat[d][8][1]*q + mat[d][r][1] - mat[d][0][1] 
    x = mat[d][r][0]
    #print('x, cnt', x, cnt)
    w = x0 + x
    if w > 9:
        x0 = w - 9
        cnt += 2
    else:
        x0 = w
        cnt += 1
    #print('x0, x, cnt', x0, x,  cnt)    
print(cnt-1)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    m = int(input())
    DC = [list(map(int, input().split())) for _ in range(m)]
    total = 0
    res = 0
    for d, c in DC:
        r = c * d
        res += c
        while r // 10:
            q, r = divmod(r, 10)
            res += q
            r += q
        total += r

    while total // 10:
        q, total = divmod(total, 10)
        res += q
        total += q

    print(res - 1)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
from heapq import heapify, heappop, heappush
import math
import random
import string
from copy import deepcopy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

#############
# Main Code #
#############

M = getN()
# dがc個並ぶ
que = [getList() for i in range(M)]

# 圧縮を繰り返して１桁になったら終了
# 圧縮する順番を工夫する
# なるべく繰り上がるようにする
# 5 + 5 = 10, 6 + 7 = 13

# 10 - 1消費することでラウンドを一つ増やせる
cnt = 0
left = 0
for d, c in que:
    cnt += c
    left += d * c

while left >= 10:
    t = left // 10
    cnt += t
    left %= 10
    left += t
print(cnt - 1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
M = int(input())

ans = -1

s = 0

for _ in range(M):
    d, c = map(int, input().split())
    s += d * c
    ans += c

ans += (s - 1) // 9
print(ans)

Submission status: Accepted

Solution code:
m = int(input())
s = ""
S = 0
C = 0

"""
各桁をa,bとすると
(1)a+b < 10 
(2)a+b >= 10
で場合分け
1234567890 -> 桁がn, 総和がs
(1)の操作 : n-1, s
(2)の操作 : n, s-9
POINT (1),(2) 両方の操作でも、9n+sの値が9ずつ減っていく
"""

for i in range(m):
    d,c = map(int, input().split())
    S += c*d # 総和
    C += c # 桁

n = S
while n >= 10:
    cnt = 0
    for s in str(n):
        cnt += int(s)
    n = cnt

begin = S + 9*C
end = n + 9
print((begin-end-1)//9 + 1)

Submission status: Accepted

Solution code:
n=int(input())
DC=[list(map(int,input().split())) for _ in range(n)]

D,S=0,0
for d,c in DC:
    D +=c
    S +=d*c
print(D-1+(S-1)//9)

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil,pi,factorial
    from operator import itemgetter
    def I(): return int(input())
    def MI(): return map(int, input().split())
    def LI(): return list(map(int, input().split()))
    def LI2(): return [int(input()) for i in range(n)]
    def MXI(): return [[LI()]for i in range(n)]
    def SI(): return input().rstrip()
    def printns(x): print('\n'.join(x))
    def printni(x): print('\n'.join(list(map(str,x))))
    inf = 10**17
    mod = 10**9 + 7
#main code here!
    m=I()
    ans=-1
    sm=0
    for i in range(m):
        d,c=MI()
        ans+=c
        sm+=d*c
    while sm>=10:
        ans+=sm//10
        sm=sm%10+sm//10
    
    print(ans)
    
        
                
                
    
        
        
        
        




if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from collections import Counter
    M = int(readline())
    C = Counter()

    for _ in range(M):
        d, c = map(int, readline().split())
        C[d] += c
    ans = 0
    ans += C[0]
    s = ""
    for i in range(1, 10):
        p = str(i) * 10 + "*"
        cnt = 0
        while len(p) > 2:
            p = str(int(p[0]) + int(p[1])) + p[2:]
            cnt += 1

        while C[i] >= 10:
            k = C[i] // 10
            C[i] = C[i] % 10 + k
            ans += k * cnt
        s += str(i) * C[i]

    s += "*"
    while len(s) > 2:
        s = str(int(s[0]) + int(s[1])) + s[2:]
        ans += 1

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
M = int(input())
d, c = zip(*[[int(x) for x in input().split()] for _ in range(M)])
n_digits = sum(c) # 桁数
sum_digits = sum(d[i] * c[i] for i in range(M))
ans = n_digits - 1 + (sum_digits - 1) // 9
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/ddcc2020-qual/tasks/ddcc2020_qual_d
m = int(input())
D = 0
S = 0
for _ in range(m):
    d, c = list(map(int, input().split()))
    D += c
    S += c * d
result1 = D - 1
result2 = (S-1)//9
# print(D)
# print(S)
print(result1+result2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
m = int(input())
DC = [tuple(int(x) for x in input().split()) for _ in range(m)]
ans = sum(c for _, c in DC) - 1
S = sum(d * c for d, c in DC)
ans += (S - 1) // 9
print(ans)

Submission status: Accepted

Solution code:
M=int(input())

n_digit=0
n_sum=0
for _ in range(M):
  d,c=map(int,input().split())
  n_digit+=c
  n_sum+=d*c
#print(n_digit,n_sum)

print(n_digit-1+(n_sum-1)//9)

Submission status: Accepted

Solution code:
M = int(input())
D = 0
S = 0

for _ in range(M):
    d, c = map(int,input().split())
    D += c
    S += c * d

ans = D - 1 + (S - 1) // 9

print(ans)

Submission status: Accepted

Solution code:
m = int(input())
l = [list(map(int,input().split())) for _ in range(m)]
ans = 0
t = 0
for i in range(m):
    ans += l[i][1]
    t += l[i][0]*l[i][1]
ans += (t-1)//9
print(ans-1)

Submission status: Accepted

Solution code:
def main():
    m = int(input())
    arr = [list(map(int, input().split())) for _ in range(m)]
    ans = 0
    sm = 0
    tmp = 0
    for i in range(m):
        sm += arr[i][0] * arr[i][1]
        tmp += arr[i][1]
    ans += tmp-1
    ans += (sm-1)//9
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
M = int(input())
D = [];C = []
S = 0
ans = -1
for i in range(M):
  d,c = map(int,input().split())
  D.append(d);C.append(c)
  S += d*c
  ans += c
kaisu = (S-1)//9
#print(ans,kaisu)
ans += kaisu
print(ans)