Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p03546 (python)

Fixed submission:
from collections import defaultdict, deque
h, w = map(int, input().split())
table = [list(map(int, input().split())) for _ in range(10)]
dd = defaultdict(int)
for _ in range(h):
    for x in map(int, input().split()):
        if x == -1:
            continue
        dd[x] += 1

INF = 10**9
scores = [INF]*10
decided = [0]*10
queue = deque()
trans_table = list(zip(*table))
for i in range(10):
    x = trans_table[1][i]
    scores[i] = x

MIN = min(set(scores) ^ {0})

while 0 in decided:
    for idx, score in enumerate(scores):
        if score <= MIN:
            decided[idx] = 1

    for i in range(10):
        if i == 1:
            continue
        if decided[i]:
            for j in range(10):
                if not decided[j]:
                    queue.append((i, j))

    while queue:
        i, j = queue.popleft()
        x = trans_table[i][j]
        scores[j] = min(scores[j], scores[i]+x)

    MIN += 1



ans = 0
for k, v in dd.items():
    ans += scores[k]*v
print(ans)

Buggy submission (verdict: Runtime Error):
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import shortest_path
from collections import defaultdict
h, w = map(int, input().split())
table = [list(map(int, input().split())) for _ in range(10)]
dd = defaultdict(int)
for _ in range(h):
    for x in map(int, input().split()):
        if x == -1:
            continue
        dd[x] += 1

trans_table = list(zip(*table))
scores = shortest_path(csr_matrix(trans_table), indices=1)

ans = 0
for k, v in dd.items():
    ans += int(scores[k])*v
print(ans)
Problem p03608 (python)

Fixed submission:
import heapq
from itertools import permutations
n, m, r = map(int, input().split())
R = list(map(lambda x: int(x)-1, input().split()))
edges = [[]for _ in range(n)]
for _ in range(m):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    edges[a].append((c, b))
    edges[b].append((c, a))


def dijkstra(init_v):
    next_v = [(0, init_v)]
    INF = 10**18
    dist = [INF]*n
    dist[init_v] = 0
    while next_v:
        d, v = heapq.heappop(next_v)
        if dist[v] < d:
            continue
        for d, v2 in edges[v]:
            if dist[v2] <= dist[v]+d:
                continue
            dist[v2] = dist[v]+d
            heapq.heappush(next_v, (dist[v2], v2))
    return dist


dists = []
for x in R:
    dist = dijkstra(x)
    dists.append(dist)

INF = 10**18
ans = INF
for subset in permutations(range(r)):
    d = 0
    for v, v2 in zip(subset, subset[1:]):
        d += dists[v][R[v2]]
    if d < ans:
        ans = d

print(ans)

Buggy submission (verdict: Runtime Error):
import heapq
from itertools import permutations
n, m, r = map(int, input().split())
R = list(map(lambda x: int(x)-1, input().split()))
edges = [[]for _ in range(n)]
for _ in range(m):
    a, b, c = map(int, input().split())
    edges[a].append((c, b-1))
    edges[b].append((c, a-1))


def dijkstra(init_v):
    next_v = [(0, init_v)]
    INF = 10**18
    dist = [INF]*n
    dist[init_v] = 0
    while next_v:
        d, v = heapq.heappop(next_v)
        if dist[v] < d:
            continue
        for d, v2 in edges[v]:
            if dist[v2] <= dist[v]+d:
                continue
            dist[v2] = dist[v]+d
            heapq.heappush(next_v, (dist[v2], v2))
    return dist


dists = []
for x in R:
    dist = dijkstra(x)
    dists.append(dist)

INF = 10**18
ans = INF
for subset in permutations(range(r)):
    d = 0
    for v, v2 in zip(subset, subset[1:]):
        d += dists[v][R[v2]]
    if d < ans:
        ans = d

print(ans)
Problem p03640 (python)

Fixed submission:
h, w = map(int, input().split())
n = int(input())
A = list(map(int, input().split()))
ans = [[0]*w for _ in range(h)]

idx_ls = []

flip = 0
for i in range(h):
    if not flip:
        for j in range(w):
            idx_ls.append((i, j))
    else:
        for j in range(w-1, -1, -1):
            idx_ls.append((i, j))
    flip ^= 1


for color, a in enumerate(A):
    color += 1
    for _ in range(a):
        i, j = idx_ls.pop()
        ans[i][j] = color

for x in ans:
    print(*x)

Buggy submission (verdict: Runtime Error):
h, w = map(int, input().split())
n = int(input())
A = list(map(int, input().split()))
ans = [[0]*w for _ in range(h)]
idx_ls = [(0, 0)]

i = j = 0
flip = 0
flag = 1
for _ in range(h*w-1):
    if (j == w-1 or j == 0)and not flag:
        i += 1
        flip ^= 1
        flag = 1
        idx_ls.append((i, j))
        continue
    flag = 0
    j = j+[1, -1][flip]
    idx_ls.append((i, j))


for color, a in enumerate(A):
    color += 1
    for _ in range(a):
        i, j = idx_ls.pop()
        ans[i][j] = color

for x in ans:
    print(*x)
Problem p03651 (python)

Fixed submission:
n, k = map(int, input().split())
A = sorted(map(int, input().split()))
INF = 10**9+1
diff = INF
for x, y in zip(A, A[1:]):
    z = abs(x-y)
    if z > 0:
        diff = min(diff, z)

MAX = A[-1]
ans = False
for a in A:
    if a-k < 0:
        continue
    x, r = divmod((a-k), diff)
    if r == 0:
        ans = True
        break
print("POSSIBLE" if ans else "IMPOSSIBLE")

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
A = sorted(map(int, input().split()))
INF = 10**5
diff = INF
for x, y in zip(A, A[1:]):
    diff = min(diff, abs(x-y))

MAX = A[-1]
ans = False
for a in A:
    if a-k < 0:
        continue
    x, r = divmod((a-k), diff)
    if r == 0:
        ans = True
        break
print("POSSIBLE" if ans else "IMPOSSIBLE")
Problem p03673 (python)

Fixed submission:
from collections import deque

q = deque()

n = int(input())
*a, = map(int, input().split())
for i, x in enumerate(a):
    if i % 2 == 0:
        q.append(x)
    else:
        q.appendleft(x)
if n % 2:
    print(*reversed(q))
else:
    print(*q)

Buggy submission (verdict: Runtime Error):
n, a = int(input()), list(map(int, input().split()))
print(*a[::-2], *a[n % 2::2])
Problem p03722 (python)

Fixed submission:
n, m = map(int, input().split())
edges = []
for _ in range(m):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    edges.append((a, b, -c))

init_v = 0
INF = float('inf')
dist = [INF]*n
dist[init_v] = 0
for i in range(n):
    for v, v2, c in edges:
        if dist[v2] <= dist[v]+c:
            continue
        dist[v2] = dist[v]+c

negative = [0]*n
for i in range(n):
    for v, v2, c in edges:
        if dist[v2] <= dist[v]+c:
            continue
        dist[v2] = dist[v]+c
        negative[v2] = 1

if negative[-1]:
    print('inf')
else:
    print(-dist[-1])

Buggy submission (verdict: Runtime Error):
from scipy.sparse.csgraph import csgraph_from_dense, bellman_ford
from scipy.sparse.csgraph._shortest_path import NegativeCycleError

n, m = map(int, input().split())
GRAPH = [[0]*n for _ in range(n)]

for _ in range(m):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    GRAPH[a][b] = -c

try:
    sp = bellman_ford(csgraph_from_dense(GRAPH), indices=0)
except NegativeCycleError:
    print('inf')
else:
    print(-int(sp[-1]))
Problem p03732 (python)

Fixed submission:
n, w = map(int, input().split())
weights, values = zip(*[tuple(map(int, input().split()))for _ in range(n)])
min_w = min(weights)-1
weights = [weight-min_w for weight in weights]
W_MAX = 4*200
dp = [[0]*(W_MAX+1) for _ in range(n+1)]
for i in range(1, n+1):
    for k in reversed(range(1, n+1)):
        for j in range(1, W_MAX+1):
            if j-weights[i-1] < 0:
                continue
            dp[k][j] = max(dp[k][j], dp[k-1][j-weights[i-1]]+values[i-1])

ans = 0
for k in range(n+1):
    w_max = min(w-k*min_w, W_MAX)
    if w_max < 0:
        continue
    ans = max(ans, dp[k][w_max])
print(ans)

Buggy submission (verdict: Runtime Error):
n, W = map(int, input().split())
dd = defaultdict(list)
for i in range(n):
    weight, value = map(int, input().split())
    dd[weight].append(value)

for k in dd.keys():
    dd[k].sort(reverse=True)

a = min(dd.keys())
b, c, d = a+1, a+2, a+3
ans = 0
for w in range(len(dd[a])+1):
    for x in range(len(dd[b])+1):
        for y in range(len(dd[c])+1):
            for z in range(len(dd[d])+1):
                if W < a*w+b*x+c*y+d*z:
                    break
                val = sum(dd[a][:w])+sum(dd[b][:x]) + \
                    sum(dd[c][:y])+sum(dd[d][:z])
                ans = max(ans, val)
print(ans)

Buggy submission (verdict: Runtime Error):
n, w = map(int, input().split())
weights, values = zip(*[tuple(map(int, input().split()))for _ in range(n)])
min_w = min(weights)-1
weights = [weight-min_w for weight in weights]
W_MAX = 4*200
dp = [[0]*(W_MAX+1) for _ in range(n+1)]
for i in range(1, n+1):
    for k in reversed(range(1, n+1)):
        for j in range(1, W_MAX+1):
            if j-weights[i-1] < 0:
                continue
            dp[k][j] = max(dp[k][j], dp[k-1][j-weights[i-1]]+values[i-1])

ans = 0
for k in range(n+1):
    w_max = w-k*min_w
    if w_max < 0 <= W_MAX:
        continue
    ans = max(ans, dp[k][w_max])
print(ans)
Problem p03786 (python)

Fixed submission:
import heapq
n = int(input())
A = list(map(int, input().split()))
heapq.heapify(A)

cnt = 1
prev = heapq.heappop(A)
while A:
    x = heapq.heappop(A)
    if prev > x:
        heapq.heappush(A, prev)
        prev = x
        continue
    if 2*prev < x:
        cnt = 1
    else:
        cnt += 1
    prev += x

print(cnt)

Buggy submission (verdict: Runtime Error):
import heapq
n = int(input())
A = list(map(int, input().split()))
heapq.heapify(A)

cnt = 1
prev = heapq.heappop(A)
while A:
    x = heapq.heappop(A)
    if prev > x:
        heapq.heappush(A, prev)
        prev = x
        continue
    print(prev, x, A, cnt)
    if 2*prev < x:
        cnt = 1
    else:
        cnt += 1
    prev += x

print(cnt)
Problem p03959 (python)

Fixed submission:
MOD = 10**9+7
n = int(input())
T = list(map(int, input().split()))
A = list(map(int, input().split()))
n_cases = [0]*n
n_cases[0] = n_cases[-1] = 1
height = [0]*n
if T[-1] != A[0]:
    print(0)
    exit()
height[0] = T[0]
height[-1] = A[-1]
for i, (t1, t2) in enumerate(zip(T, T[1:]), 1):
    if t1 != t2:
        n_cases[i] = 1
        height[i] = t2

for i, (a1, a2) in enumerate(zip(A[::-1], A[::-1][1:]), 2):
    if a1 != a2:
        n_cases[-i] = 1
        if height[-i] == a2 or (height[-i] == 0 and a2 <= T[-i]):
            continue
        print(0)
        exit()

for i, (t, a) in enumerate(zip(T, A)):
    if n_cases[i]:
        continue
    n_cases[i] = min(t, a)


ans = 1
for x in n_cases:
    ans *= x
    ans %= MOD
print(ans)

Buggy submission (verdict: Runtime Error):
MOD = 10**9+7
n = int(input())
T = list(map(int, input().split()))
A = list(map(int, input().split()))
n_cases = [0]*n
n_cases[0] = n_cases[-1] = 1
height = [0]*n
if T[-1] != A[0]:
    print(0)
    exit()
height[0] = T[0]
height[-1] = A[-1]
for i, (t1, t2) in enumerate(zip(T, T[1:]), 1):
    if t1 != t2:
        n_cases[i] = 1
        height[i] = t2


for i, (a1, a2) in enumerate(zip(A[::-1], A[::-1][1:]), 2):
    if a1 != a2:
        n_cases[-i] = 1
        assert height[-i] == a2 or height[-i] == 0
        height[-i] = a2


for i, (t, a) in enumerate(zip(T, A)):
    if n_cases[i]:
        continue
    n_cases[i] = min(t, a)


ans = 1
for x in n_cases:
    ans *= x
    ans %= MOD
print(ans)
Problem p02609 (python)

Fixed submission:
def popcount_int(n):
    return bin(n).count('1')

def popcount_bin(s):
    return s.count('1')

def poptimes(n,cnt=1):
    if n==0:
        return cnt
    else:
        n%=popcount_int(n)
        cnt+=1
        return poptimes(n,cnt)

N=int(input())
S=input()

S_int=int(S,2)
pc=popcount_bin(S)
pc_plus=S_int%(pc+1)
if pc!=1:
    pc_minus=S_int%(pc-1)

for i in range(N):
    if S[i]=='1':
        if pc!=1:
            tmp=pc_minus-pow(2,N-i-1,pc-1)
            print(poptimes(tmp%(pc-1)))
        else:
            print(0)
            continue
    else:
        tmp=pc_plus+pow(2,N-i-1,pc+1)
        print(poptimes(tmp%(pc+1)))

Buggy submission (verdict: Runtime Error):
def popcount(n):
    ret=0
    while n>0:
        if n%2==1:
            ret+=1
        n//=2
    return ret

def f(n):
    ret=0
    while True:
        n%=popcount(n)
        if n==0:
            ret+=1
            break
        else:
            ret+=1
    return ret

N=int(input())
X=input()
base=0
for i in range(len(X)):
    x=int(X[-(i+1)])
    if x==1:
        base+=2**i

for i in range(len(X),0,-1):
    if X[-i]=='1':
        tmp=base-2**(i-1)
    else:
        tmp=base+2**(i-1)
    print(f(tmp))

Buggy submission (verdict: Runtime Error):
def popcount(n):
    ret=0
    while n!=0:
        if n%2==1:
            ret+=1
        n//=2
    return ret

def f(n):
    ret=0
    original=n
    while True:
        n=n%popcount(n)
        if n in memo:
            memo[original]=ret+memo[n]
            return memo[original]
        if n!=0:
            ret+=1
        else:
            ret+=1
            break
    memo[original]=ret
    return ret

N=int(input())
X=input()
bit=[""]*N
for i in range(N):
    bit[i]=X[i]

n=int(X,2)
memo={}
for i in range(N):
    tmp=n
    if bit[i]=="0":
        tmp+=2**(N-i-1)
    else:
        tmp-=2**(N-i-1)
    print(f(tmp))

Buggy submission (verdict: Runtime Error):
def popcount(n):
    ret=0
    while n!=0:
        if n%2==1:
            ret+=1
        n//=2
    return ret

def f(n):
    ret=0
    while True:
        n=n%popcount(n)
        if n!=0:
            ret+=1
        else:
            ret+=1
            break
    return ret

N=int(input())
X=input()
bit=[""]*N
for i in range(N):
    bit[i]=X[i]

n=int(X,2)
for i in range(N):
    tmp=n
    if bit[i]=="0":
        tmp+=2**(N-i-1)
    else:
        tmp-=2**(N-i-1)
    print(f(tmp))
Problem p02708 (python)

Fixed submission:
N,K=map(int,input().split())
n=[i for i in range(N+1)]
mod=10**9+7

min_value=[0]*(N+1)
max_value=[0]*(N+1)
min_value[0]=0
max_value[0]=N
for i in range(1,N+1):
  min_value[i]=min_value[i-1]+n[i]
  max_value[i]=max_value[i-1]+n[-(i+1)]

ans=0
for i in range(K-1,N+1):
  ans+=max_value[i]-min_value[i]+1
  ans%=mod
print(ans)

Buggy submission (verdict: Runtime Error):
N,K=map(int,input().split())
n=[i for i in range(N+1)]
mod=10**9+7141421 35623

min_value=[0]*(N+1)
max_value=[0]*(N+1)
min_value[0]=0
max_value[0]=N
for i in range(1,N+1):
  min_value[i]=min_value[i-1]+n[i]
  max_value[i]=max_value[i-1]+n[-(i+1)]

ans=0
for i in range(K-1,N+1):
  ans+=max_value[i]-min_value[i]+1
  ans%=mod
print(ans)
Problem p02761 (python)

Fixed submission:
N,M=map(int,input().split())
sc=[list(map(int,input().split())) for i in range(M)]
if N==1:
  start=0
else:
  start=10**(N-1)

for i in range(start,10**N):
  n=str(i)
  cnt=0
  for j in range(M):
    if n[sc[j][0]-1]==str(sc[j][1]):
      cnt+=1
    else:
      break
  if cnt==M:
    print(n)
    exit()
print('-1')

Buggy submission (verdict: Runtime Error):
N,M=map(int,input().split())
sc=[list(map(int,input().split())) for i in range(M)]
s=[]
c=[]
for i in range(M):
  s.append(sc[i][0]-1)
  c.append(sc[i][1])

S=[s[0]]
C=[c[0]]
for i in range(1,M):
  if s[i] in S:
    if C[S.index(s[i])]==c[i]:
      continue
    else:
      print('-1')
      exit()
  else:
    S.append(s[i])
    C.append(c[i])

ans=[0]*(N)
ans[0]=1

for i in range(M):
  ans[s[i]]=c[i]

if len(ans)!=0 and ans[0]==0:
  print('-1')
  exit()

ANS=""
for i in range(N):
  ANS+=str(ans[i])
print(ANS)

Buggy submission (verdict: Runtime Error):
N,M=map(int,input().split())
sc=[list(map(int,input().split())) for i in range(M)]
s=[]
c=[]
for i in range(M):
  s.append(sc[i][0]-1)
  c.append(sc[i][1])

S=[s[0]]
C=[c[0]]
for i in range(1,M):
  if s[i] in S:
    if C[S.index(s[i])]==c[i]:
      continue
    else:
      print('-1')
      exit()
  else:
    S.append(s[i])
    C.append(c[i])

ans=[0]*(N)

for i in range(M):
  ans[s[i]]=c[i]

if len(ans)!=0 and ans[0]==0:
  print('-1')
  exit()

ANS=""
for i in range(N):
  ANS+=str(ans[i])
print(ANS)
Problem p02791 (python)

Fixed submission:
N=int(input())
P=list(map(int,input().split()))

ans=0
val=P[0]
for i in range(N):
  if val>=P[i]:
    ans+=1
    val=P[i]
print(ans)

Buggy submission (verdict: Runtime Error):
N=int(input())
P=list(map(int,input().split()))
ans=1

for i in range(1,N):
  if P[i]!=1 and P[i]<=min(P[:i]):
    ans+=1
  elif P[i]==1:
    ans+=1
    break

for j in range(i+1,N):
  if P[j]==1:
    ans+=1

print(ans)
Problem p02818 (python)

Fixed submission:
A,B,K=map(int,input().split())
ans=[0]*2

if A>=K:
  ans[0]=A-K
  ans[1]=B
else:
  ans[1]=max(0,B-(K-A))

print(ans[0],ans[1])

Buggy submission (verdict: Runtime Error):
A,B,K=map(int,input().split())
ans=[0]*2

if A>=K:
  ans[0]=max(ans,A-K)
  ans[1]=B
else:
  ans[1]=B-(K-A)

print(ans[0],ans[1])
Problem p02850 (python)

Fixed submission:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**9)

N=int(input())
G=[[] for _ in range(N)]
for i in range(N-1):
  a,b=map(int,input().split())
  a,b=a-1,b-1
  G[a].append([b,i])

ans=[None]*(N-1)
def dfs(now,color):
  cnt=1
  for to,j in G[now]:
    if cnt==color:
      cnt+=1
    ans[j]=cnt
    dfs(to,cnt)
    cnt+=1

dfs(0,0)
print(max(ans))
print(*ans, sep="\n")

Buggy submission (verdict: Runtime Error):
N=int(input())
G=[[] for _ in range(N)]
for i in range(N-1):
  a,b=map(int,input().split())
  a,b=a-1,b-1
  G[a].append([b,i])

ans=[None]*(N-1)
def dfs(now,color):
  cnt=1
  for to,j in G[now]:
    if cnt==color:
      cnt+=1
    ans[j]=cnt
    dfs(to,cnt)
    cnt+=1
dfs(0,0)
print(max(ans))
print(*ans,sep="\n")

Buggy submission (verdict: Runtime Error):
N=int(input())
AB=[list(map(int,input().split())) for _ in range(N-1)]

G=[[] for _ in range(N)]
cnt=[0]*N
for i in range(N-1):
  cnt[AB[i][0]-1]+=1
  cnt[AB[i][1]-1]+=1
  G[AB[i][0]-1].append(AB[i][1])
  G[AB[i][1]-1].append(AB[i][0])
K=max(cnt)
print(K)
#print(G)
#print(cnt)

color=[[] for _ in range(N)]
for i in range(N-1):
  colors=[n for n in range(1,K+1)]
  for j in range(len(color[AB[i][0]-1])):
    colors.remove(color[AB[i][0]-1][j])
  for k in range(len(color[AB[i][1]-1])):
    colors.remove(color[AB[i][1]-1][k])
  color[AB[i][0]-1].append(colors[0])
  color[AB[i][1]-1].append(colors[0])
  print(colors[0])
Problem p02900 (python)

Fixed submission:
def gcd(a,b): # 最大公約数を求める
  while b:
    a,b=b,a%b
  return abs(a)
  
def solve(A:int, B:int):
  a=gcd(A,B)
  ap=a
  ct=0
  x=2
  if a%x==0:
    ct+=1
    while a%x==0:
      a=a//x
  
  x=3
  while x*x<ap+1:
    if a%x==0:
      ct+=1
      while a%x==0:
        a=a//x
    x+=2
  
  if a!=1:
    ct+=1
  print(ct+1)
  
  return

def main():
  A,B=map(int,input().split())
  solve(A,B)

if __name__=="__main__":
  main()

Buggy submission (verdict: Runtime Error):
import math

A,B=map(int,input().split())

gcd=math.gcd(A,B)
val=1
ans=0
for i in range(1,gcd+1):
  if gcd%i==0:
    if math.gcd(i,val)==1:
      ans+=1
      val*=i
print(ans)

Buggy submission (verdict: Runtime Error):
import math

A,B=map(int,input().split())

div=[]
for i in range(1,math.gcd(A,B)+1):
  if A%i==0 and B%i==0:
    div.append(i)

div.pop(0)
ans=1
val=1
for i in range(len(div)):
  if math.gcd(val,div[i])==1:
    ans+=1
    val*=div[i]
print(ans)

Buggy submission (verdict: Runtime Error):
import math

A,B=map(int,input().split())

div=[]
for i in range(1,min(A,B)+1):
  if A%i==0 and B%i==0:
    div.append(i)

div.pop(0)
ans=1
val=1
for i in range(len(div)):
  if math.gcd(val,div[i])==1:
    ans+=1
    val*=div[i]
print(ans)
Problem p02921 (python)

Fixed submission:
S=input()
T=input()
ans=0
for i in range(3):
  if S[i]==T[i]:
    ans+=1
print(ans)

Buggy submission (verdict: Runtime Error):
A,B=map(int,input().split())
for i in range(1,13):
  if 1+(A-1)*i>=B:
    print(i)
    exit()
Problem p02936 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**9)

N,Q,*abpx=map(int,sys.stdin.read().split())

graph=[[] for _ in range(N)]
for a,b in zip(*[iter(abpx[:2*(N-1)])]*2):
    a-=1
    b-=1
    graph[a].append(b)
    graph[b].append(a)

f=[0]*N
for p,x in zip(*[iter(abpx[2*(N-1):])]*2):
    p-=1
    f[p]+=x

ans=[0 for _ in range(N)]
visited=[False]*N
def c(i,p):
    if visited[i]:
        return 
    visited[i]=True
    w=f[i]+p
    ans[i]=w
    ch=graph[i]
    for x in ch:
        c(x,w)

c(0,0)
print(*ans)

Buggy submission (verdict: Runtime Error):
mport sys
sys.setrecursionlimit(10**9)

N,Q=map(int,input().split())

link=[[] for _ in range(N)]
for i in range(N-1):
  a,b=map(int,input().split())
  a,b=a-1,b-1
  link[a].append(b)

ans=[0]*N

def px(p,x):
  for i in link[p]:
    ans[i]+=x
    px(i,x)

for i in range(Q):
  p,x=map(int,input().split())
  p=p-1
  ans[p]+=x
  px(p,x)

print(*ans)

Buggy submission (verdict: Runtime Error):
N,Q=map(int,input().split())
AB=[list(map(int,input().split())) for _ in range(N-1)]
PX=[list(map(int,input().split())) for _ in range(Q)]

link=[[] for _ in range(N)]
for i in range(N-1):
  a=AB[i][0]-1
  b=AB[i][1]-1
  link[a].append(b)

ans=[0]*N

def px(p,x):
  for i in link[p]:
    ans[i]+=x
    px(i,x)

for i in range(Q):
  p=PX[i][0]-1
  x=PX[i][1]
  ans[p]+=x
  px(p,x)

print(*ans)
Problem p03062 (python)

Fixed submission:
N=int(input())
A=list(map(int,input().split()))
pbox=[]
nbox=[]
if 0 in A:
  count=0
  for i in A:
    count += abs(i)
  print(count)
else:
  for i in A:
    if i > 0:
      pbox.append(i)
    else:
      nbox.append(i)
  if len(nbox)%2==0:
    count=0
    for i in A:
      count += abs(i)
    print(count)
  else:
    box=[]
    for i in A:
      box.append(abs(i))
    mi=min(box)
    count=0
    for i in A:
      count += abs(i)
    print(count-2*mi)

Buggy submission (verdict: Runtime Error):
N=int(input())
A=list(map(int,input().split()))
pbox=[]
nbox=[]
if 0 in A:
  count=0
  for i in A:
    count += abs(i)
  print(count)
else:
  for i in a:
    if i > 0:
      pbox.append(i)
    else:
      nbox.append(i)
  if len(nbox)%2==0:
    count=0
    for i in a:
      count += abs(i)
    print(count)
  else:
    box=[]
    for i in a:
      box.append(abs(i))
    mi=min(box)
    count=0
    for i in a:
      count += abs(i)
    print(count-2*mi)
Problem p03071 (python)

Fixed submission:
a,b=map(int,input().split())
c=[a+a-1,b+b-1,a+b]
print(max(c))

Buggy submission (verdict: Runtime Error):
A,B=map(int,input().split())
C=[A+A-1, B+B-1, A+B]
print(max(c))