Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
N,Q=map(int,input().split())
ans=[0]*N
ki=[[] for _ in range(N)]
for i in range(N-1):
 a,b=map(int,input().split())
 ki[a-1].append(b-1)
 ki[b-1].append(a-1)
for i in range(Q):
 p,q=map(int,input().split())
 ans[p-1]+=q
# dfs
from collections import deque
stack =deque([0])
visited = ["False"] * N
while stack:
 ne = stack.popleft()
 if visited[ne] == "False":
  visited[ne] = "True"
  for j in ki[ne]:
   if visited[j] == "False":
    ans[j]+=ans[ne]
    stack.append(j)
print(*ans)


Fixed submission:
N=input()
L=len(N)
ans=[[0 for e in range(2)] for f in range(L)]
k=int(N[L-1])
ans[0][0]=k
ans[0][1]=10-k
if L>1:
 for i in range(1,L):
  k=int(N[L-1-i])
  ans[i][0]=min(ans[i-1][0]+k,ans[i-1][1]+k+1)
  ans[i][1]=min(ans[i-1][0]+10-k,ans[i-1][1]+10-k-1)
print(min(ans[L-1][0],ans[L-1][1]+1))

Buggy python submission (verdict: Time Limit Exceeded):
N=input()
L=len(N)
ans=[[0 for e in range(2)] for f in range(L)]
k=int(N[L-1])
ans[0][0]=k
ans[0][1]=10-k
if L>1:
 for i in range(1,L):
  k=int(N[L-1-i])
  ans[i][0]=min(ans[i-1][0]+k,ans[i-1][1]+k+1)
  ans[i][1]=min(ans[i-1][0]+10-k,ans[i-1][1]+10-k-1)
print(min(ans[L-1][0],ans[L-1][1]+1))


Fixed submission:
N=input()
L=len(N)
ans=[[0 for e in range(2)] for f in range(L)]
k=int(N[L-1])
ans[0][0]=k
ans[0][1]=10-k
if L>1:
 for i in range(1,L):
  k=int(N[L-1-i])
  ans[i][0]=min(ans[i-1][0]+k,ans[i-1][1]+k+1)
  ans[i][1]=min(ans[i-1][0]+10-k,ans[i-1][1]+10-k-1)
print(min(ans[L-1][0],ans[L-1][1]+1))

Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
lst=[int(d) for d in str(N)]
L=len(lst)
ans=[[0 for e in range(2)] for f in range(L)]
ans[0][0]=lst[L-1]
ans[0][1]=10-lst[L-1]
for i in range(1,L):
 ans[i][0]=min(ans[i-1][0]+lst[L-1-i],ans[i-1][1]+lst[L-1-i]+1)
 ans[i][1]=min(ans[i-1][0]+10-lst[L-1-i],ans[i-1][1]+10-lst[L-1-i]-1)
print(min(ans[L-1][0],ans[L-1][1]+1))


Fixed submission:
N=input()
L=len(N)
ans=[[0 for e in range(2)] for f in range(L)]
k=int(N[L-1])
ans[0][0]=k
ans[0][1]=10-k
if L>1:
 for i in range(1,L):
  k=int(N[L-1-i])
  ans[i][0]=min(ans[i-1][0]+k,ans[i-1][1]+k+1)
  ans[i][1]=min(ans[i-1][0]+10-k,ans[i-1][1]+10-k-1)
print(min(ans[L-1][0],ans[L-1][1]+1))

Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
lst=[int(d) for d in str(N)]
L=len(lst)
ans=[[0 for e in range(2)] for f in range(L)]
ans[0][0]=lst[L-1]
ans[0][1]=10-lst[L-1]
for i in range(1,L):
 ans[i][0]=min(ans[i-1][0]+lst[L-1-i],ans[i-1][1]+lst[L-1-i]+1)
 ans[i][1]=min(ans[i-1][0]+10-lst[L-1-i],ans[i-1][1]+10-lst[L-1-i]-1)
print(min(ans[L-1][0],ans[L-1][1]+1))


Fixed submission:
N=input()
L=len(N)
ans=[[0 for e in range(2)] for f in range(L)]
k=int(N[L-1])
ans[0][0]=k
ans[0][1]=10-k
if L>1:
 for i in range(1,L):
  k=int(N[L-1-i])
  ans[i][0]=min(ans[i-1][0]+k,ans[i-1][1]+k+1)
  ans[i][1]=min(ans[i-1][0]+10-k,ans[i-1][1]+10-k-1)
print(min(ans[L-1][0],ans[L-1][1]+1))

Buggy python submission (verdict: Runtime Error):
def digit(i):
 if i > 0:
  return digit(i//10) + [i%10]
 else:
  return []
N=int(input())
lst=digit(N)
L=len(lst)
ans=[[0 for e in range(2)] for f in range(L)]
ans[0][0]=lst[L-1]
ans[0][1]=10-lst[L-1]
for i in range(1,L):
 ans[i][0]=min(ans[i-1][0]+lst[L-1-i],ans[i-1][1]+lst[L-1-i]+1)
 ans[i][1]=min(ans[i-1][0]+10-lst[L-1-i],ans[i-1][1]+10-lst[L-1-i]-1)
print(min(ans[L-1][0],ans[L-1][1]+1))


Fixed submission:
N=input()
L=len(N)
ans=[[0 for e in range(2)] for f in range(L)]
k=int(N[L-1])
ans[0][0]=k
ans[0][1]=10-k
if L>1:
 for i in range(1,L):
  k=int(N[L-1-i])
  ans[i][0]=min(ans[i-1][0]+k,ans[i-1][1]+k+1)
  ans[i][1]=min(ans[i-1][0]+10-k,ans[i-1][1]+10-k-1)
print(min(ans[L-1][0],ans[L-1][1]+1))