Buggy python submission (verdict: Time Limit Exceeded):

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)


Fixed submission:

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 = []
for m in range(M):
    d,c = inpl()

    while True:
        if c == 1:
            zan.append(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.append(d)
            c -= 1

zan.reverse()

bx = zan[0]
for x in zan[1:]:
    d = x + bx
    if d >= 10:
        bx = d - 10 + 1
        ans += 2
    else:
        bx = d
        ans += 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):

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)


Fixed submission:

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 = []
for m in range(M):
    d,c = inpl()

    while True:
        if c == 1:
            zan.append(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.append(d)
            c -= 1

zan.reverse()

bx = zan[0]
for x in zan[1:]:
    d = x + bx
    if d >= 10:
        bx = d - 10 + 1
        ans += 2
    else:
        bx = d
        ans += 1

print(ans)