Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**6)

n = int(input())

readline = sys.stdin.readline

MAX_N = 10**9+5
w = [0]*MAX_N

for _ in range(n):
    a,b = [int(i) for i in readline().split()]
    w[b] += a

s = 0
for i in range(MAX_N):
    if w[i]:
        s += w[i]
        if s > i:
            print('No')
            break
else:
    print('Yes')

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

n = int(input())

readline = sys.stdin.readline

d = {}

for _ in range(n):
    a,b = [int(i) for i in readline().split()]
    d[b] = d.get(b, 0) + a

d_sorted = sorted(d.items(), key=lambda x:x[0])

s = 0
for t, w in d_sorted:
    s += w
    if s > t:
        print('No')
        break
else:
    print('Yes')

Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**6)

n = int(input())
#b = list(map(int, input().split()))
#n, m = map(int, input().split())
#s = input()
#s,t = input().split()
#
readline = sys.stdin.readline
#n,m = [int(i) for i in readline().split()]
#ab = [[int(i) for i in readline().split()] for _ in range(n)]

MAX_N = 10**5*2+5
w = [0]*MAX_N

for _ in range(n):
    a,b = [int(i) for i in readline().split()]
    w[b] += a

s = 0
for i in range(MAX_N):
    if w[i]:
        s += w[i]
        if s > i:
            print('No')
            break
else:
    print('Yes')

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

n = int(input())

readline = sys.stdin.readline

d = {}

for _ in range(n):
    a,b = [int(i) for i in readline().split()]
    d[b] = d.get(b, 0) + a

d_sorted = sorted(d.items(), key=lambda x:x[0])

s = 0
for t, w in d_sorted:
    s += w
    if s > t:
        print('No')
        break
else:
    print('Yes')
Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**6)

w, h, x, y = map(int, input().split())

area = max(min(w*y, w*(h-y)), min(x*h, (w-x)*h))
is_multi = 0

if x == w//2 and y == h//2
    is_multi = 1
    
print(area, is_multi)

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

w, h, x, y = map(int, input().split())

ans1 = w*h/2
ans2 = 0

if x == w/2 and y == h/2:
    ans2 = 1

print(ans1, ans2)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**6)

n = int(input())
a = list(map(int, input().split()))
#n, m = map(int, input().split())
#s = input()
#s,t = input().split()
#a = [int(input()) for _ in range(n)]
#
#readline = sys.stdin.readline
#n,m = [int(i) for i in readline().split()]
#ab = [[int(i) for i in readline().split()] for _ in range(n)]

ans = 1

from math import gcd

for i in range(n):
    tmp = -1
    for j in range(n):
        if i != j:
            if tmp == -1:
                tmp = a[j]
            else:
                tmp = gcd(tmp, a[j])
    ans = max(ans, tmp)

print(ans)

Fixed submission:
"""
累積GCD
"""

import sys
sys.setrecursionlimit(10**6)

n = int(input())
A = list(map(int, input().split()))

from math import gcd

L = [0] * n
R = [0] * n

for i in range(n-1):
    L[i+1] = gcd(L[i], A[i])
for i in range(n-1, 0, -1):
    R[i-1] = gcd(R[i], A[i])

ans = 0
for i in range(n):
    ans = max(ans, gcd(L[i], R[i]))

print(ans)
Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**6)

n = int(input())
A = list(map(int, input().split()))
INF = 10**9

dp = [[0]*2 for _ in range(n+1)]　# dp[i][0]がiを選んで反転させない場合の最大値, dp[i][1]がiを選んで反転させた場合の最大値

dp[0][0] = 0 
dp[0][1] = -INF

for i in range(1,n+1):
    dp[i][0] = max(dp[i-1][0]+A[i-1], dp[i-1][1]-A[i-1]) # iを選んで反転させない時、 i-1を選んで反転させていなかったらiはそのままで、i-1を反転させていたらiは反転する
    dp[i][1] = max(dp[i-1][0]-A[i-1], dp[i-1][1]+A[i-1]) # iを選んで反転させる時、i-1を選んで反転させていなかったらiは反転していて、i-1を反転させていたらiはそのまま

print(dp[n][0]) # nを選ぶことはできない（n+1が存在しない）ため、答えはdp[n][0]

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

n = int(input())
a = list(map(int, input().split()))

flip_a = [0]*n
cnt_minus = 0

for i in range(n):
	if a[i] < 0:
		cnt_minus += 1
		flip_a[i] = -a[i]
	else:
		flip_a[i] = a[i]

flip_a.sort()

if cnt_minus%2 == 1:
	flip_a[0] = -flip_a[0]

print(sum(flip_a))
Buggy python submission (verdict: Runtime Error):
"""
keyword: 尺取法
"""

import sys
sys.setrecursionlimit(10**6)

n, k = map(int, input().split())
s = input()

nums = []
now = 1 # 今見ている数
cnt = 0 # nowがいくつ並んでいるか

for i in range(n):
    if s[i] == str(now):
        cnt += 1
    else:
        nums.append(cnt)
        now ^= 1 # 0と1を切り替える
        cnt = 1 # 新しいのをカウント
if cnt != 0:
    nums.append(cnt)

# 1-0-1-0-1-0-1って感じの配列が欲しい
# 1-0-1-0みたいに0で終わっていたら、適当に1つ足す
# 補足：1-0-1-0-1というのは、1が0個、0が2個、1が1個、、、などと並んでいるという意味。このとき1つ目の0をひっくり返すと1-1-1と連続する。1-...-1と連続している数が求める値なので、左端と右端は1にしたい
if len(nums)%2 == 0:
    nums.append(0)
    
add = 2*k+1

# 累積和を作る
tot = [0]*(len(nums)+1)
for i in range(len(tot)):
    tot[i+1] = tot[i]+nums[i]

ans = 0

# 1-0-1...の、1から始まり1で終わる範囲を見るので、偶数番目だけ見る
for i in range(0, len(nums), 2):
    
    # 次のleft, rightを計算する
    left = i
    right = min(i+add, len(nums))
    tmp = tot[right] - tot[left]
    
    ans = max(tmp, ans)
    
print(ans)

Fixed submission:
"""
keyword: 尺取法
"""

import sys
sys.setrecursionlimit(10**6)

n, k = map(int, input().split())
s = input()

nums = []
now = 1 # 今見ている数
cnt = 0 # nowがいくつ並んでいるか

for i in range(n):
    if s[i] == str(now):
        cnt += 1
    else:
        nums.append(cnt)
        now ^= 1 # 0と1を切り替える
        cnt = 1 # 新しいのをカウント
if cnt != 0:
    nums.append(cnt)

# 1-0-1-0-1-0-1って感じの配列が欲しい
# 1-0-1-0みたいに0で終わっていたら、適当に1つ足す
# 補足：1-0-1-0-1というのは、1が0個、0が2個、1が1個、、、などと並んでいるという意味。このとき1つ目の0をひっくり返すと1-1-1と連続する。1-...-1と連続している数が求める値なので、左端と右端は1にしたい
if len(nums)%2 == 0:
    nums.append(0)
    
add = 2*k+1

ans = 0
left = 0
right = 0
tmp = 0 # [left, right)のsum

# 1-0-1...の、1から始まり1で終わる範囲を見るので、偶数番目だけ見る
for i in range(0, len(nums), 2):
    
    nextleft = i
    nextright = min(i+add, len(nums))
    
    # 左端を移動
    while (nextleft > left):
        tmp -= nums[left]
        left += 1
    # 右端を移動
    while (nextright > right):
        tmp += nums[right]
        right += 1
        
    ans = max(tmp, ans)
    
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
T = input()
ans = ""
for i in T:
    if i == "?":
        ans += "D"
    else:
        ans += i
print(ans)

Fixed submission:
T = input()
ans = []
for i in T:
    if i == "?":
        ans.append("D")
    else:
        ans.append(i)
print("".join(ans))
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
n, m = map(int, input().split())
rode = []
for _ in range(m):
    rode.append(list(map(int, input().split())))

l= deque([1])
unvisit= [True]*(n+1)
unvisit[1] = False
anslis = [0]*(n+1)
while len(l) != 0:
    now = l.popleft()
    for r in rode:
        if r[0] == now:
            if unvisit[r[1]]:
                unvisit[r[1]] = False
                l.append(r[1])
                anslis[r[1]] = now
        if r[1] == now:
            if unvisit[r[0]]:
                unvisit[r[0]] = False
                l.append(r[0])
                anslis[r[0]] = now
print("Yes")
for i in range(2,len(anslis)):
    print(anslis[i])

Fixed submission:
from collections import deque
n, m = map(int, input().split())
rinsetsu_lis = [[] for _ in range(n+1)]
for _ in range(m):
    a,b = map(int, input().split())
    rinsetsu_lis[a].append(b)
    rinsetsu_lis[b].append(a)
l= deque([1])
unvisit= [True]*(n+1)
unvisit[1] = False
anslis = [0]*(n+1)
while len(l) != 0:
    now = l.popleft()
    for r in rinsetsu_lis[now]:
        if unvisit[r]:
            unvisit[r] = False
            l.append(r)
            anslis[r] = now
print("Yes")
for i in range(2,len(anslis)):
    print(anslis[i])
Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
a = [int(x) - 1 for x in input().split()]
now = 0
b = [0] * n
while k!= 0:
    if k & 1:
        now = a[now]
    k >>= 1
    for i in range(n):
        b[i] = a[a[i]]
    a = b[:]
print(now+1)

Fixed submission:
n, k = map(int, input().split())
a = [int(x)-1 for x in input().split()]
lis = [None] * n
now = 0
lis[0] = 0
num = 0
rode = [0]
while True:
    now = a[now]
    num += 1
    rode.append(now)
    if num == k:
        print(now + 1)
        break
    if lis[now] == None:
        lis[now] = num
    else:
        print(rode[lis[now] + (k-lis[now]) % (num - lis[now])] + 1)
        break
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(1,n):
    for j in range(i):
        if a[i] + a[j] == abs(i-j):
            ans += 1
print(ans)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
dic = {}
ans = 0
for i in range(n):
    r = i -a[i]
    ans += dic.get(r, 0)
    l = a[i] + i
    dic[l] = dic.get(l, 0) + 1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = []
for _ in range(n):
    x = input()
    if not x in s:
        s.append(x)
print(len(s))

Fixed submission:
n = int(input())
s = set()
for _ in range(n):
    s. add(input())
print(len(s))
Buggy python submission (verdict: Time Limit Exceeded):
s = input()
ans = 0
for j in range(3, len(s)):
    for i in range(len(s)-j):
        if int(s[i:i+j + 1]) %2019 ==0:
            ans += 1
print(ans)

Fixed submission:
numbers = [0]*2019
numbers[0] = 1
s = input()
t = 0
mod = 2019
u = 1
for i in range(1, len(s) + 1):
    t = (t + int(s[-i]) * u )% mod
    numbers[t] += 1
    u = 10*u%mod
ans = 0
for n in numbers:
    ans += n*(n-1)/2
print(int(ans))

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
ans = 0
for j in range(3, len(s)):
    for i in range(len(s)-j):
        if int(s[i:i+j + 1]) %2019 ==0:
            ans += 1
print(ans)

Fixed submission:
numbers = [0]*2019
numbers[0] = 1
s = input()
t = 0
mod = 2019
u = 1
for i in range(1, len(s) + 1):
    t = (t + int(s[-i]) * u )% mod
    numbers[t] += 1
    u = 10*u%mod
ans = 0
for n in numbers:
    ans += n*(n-1)/2
print(int(ans))
Buggy python submission (verdict: Runtime Error):
def main():
    import numpy as np
    inf = 100100100
    ans = inf
    H, W, K = map(int, input().split())
    S = []
    for _ in range(H):
        S.append([int(x) for x in input()])
    S = np.array(S)
    for i in range(2**(H-1)):
        group = [0]
        g = 0
        for j in range(H-1):
            if (i >> j) & 1:
                g += 1
            group.append(g)
        group = np.array(group)
        g += 1
        c = np.array([[0]*W]*g)
        for j in range(H):
            for k in range(W):
                c[group[j],k] += S[j,k]
        num = g-1
        now = np.zeros(g)
        def large(now):
            for n in now:
                if n > K:
                    return True
            else:
                return False
        for k in range(W):
            now += c[:,k]
            if large(now):
                now = c[:,k]
                num += 1
                if large(now):
                    num = inf
                    break
        ans = min(ans, num)
    print(ans)
main()


Fixed submission:
N, M = map(int, input().split())
print(int(N*(N-1)/2) + int(M*(M-1)/2))
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = [int(x) for x in input().split()]
l = []
ans = 0
for i in range(N):
    l.append(A.count(i+1))
for j in l:
    ans += int(j*(j-1)/2)
for k in range(N):
    print(ans - l[A[k]-1] + 1)    


Fixed submission:
N = int(input())
A = [int(x) for x in input().split()]
l = [0]*N
ans = 0
for a in A:
    l[a-1] += 1
for j in l:
    ans += int(j*(j-1)/2)
for k in range(N):
    print(ans - l[A[k]-1] + 1)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = [int(x) for x in input().split()]
l = []
ans = 0
for i in range(N):
    l.append(A.count(i+1))
for j in l:
    ans += int(j*(j-1)/2)
for k in range(N):
    print(ans - l[A[k]-1] + 1)    


Fixed submission:
N = int(input())
A = [int(x) for x in input().split()]
l = [0]*N
ans = 0
for a in A:
    l[a-1] += 1
for j in l:
    ans += int(j*(j-1)/2)
for k in range(N):
    print(ans - l[A[k]-1] + 1)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
inf = 1001001001
H, W = map(int, input().split())
s = []
for _ in range(H):
    s.append(input())
queue = deque([[0,0]])
count = [[inf for i in range(W)] for j in range(H)]
if s[0][0] == "#":
    count[0][0] = 1
else:
    count[0][0] = 0
dh = [0,1]
dw = [1,0]
while len(queue) != 0:
    now = queue.popleft()
    h = now[0]
    w = now[1]
    for i in range(2):
        nh = h + dh[i]
        nw = w + dw[i]
        if nh >= H or nw >= W:
            continue
        else:
            queue.append([nh,nw])
            number = count[h][w]
            if s[h][w] == "." and s[nh][nw] == "#":
                number += 1
            count[nh][nw] = min(number, count[nh][nw])
print(count[H-1][W-1])

Fixed submission:
inf = 1001001001
h, w = map(int, input().split())
s = []
for _ in range(h):
    s.append(input())
dp = [[inf for j in range(w)] for j in range(h)]
if s[0][0] == "#":
    dp[0][0] = 1
else:
    dp[0][0] = 0
for i in range(h):
    for j in range(w):
        if i+1 < h:
            count = dp[i][j]
            if s[i][j] == "." and s[i+1][j] == "#":
                count += 1
            dp[i+1][j] = min(dp[i+1][j], count)
        if j+1 < w:
            count = dp[i][j]
            if s[i][j] == "." and s[i][j+1] == "#":
                count += 1
            dp[i][j+1] = min(dp[i][j+1], count)
print(dp[h-1][w-1])
Buggy python submission (verdict: Runtime Error):
n, m, k = map(int, input().split())
ab = []
for _ in range(m):
    ab.append([int(x)-1 for x in input().split()])
cd = []
for _ in range(k):
    cd.append([int(x)-1 for x in input().split()])
group = [-1 for _ in range(n)]
lis = [[] for _ in range(n)]
for x in ab:
    a = x[0]
    b = x[1]
    lis[a].append(b)
    lis[b].append(a)
g_n = 0
def dfs(now):
    global g_n
    group[now] = g_n
    global group_len_n
    group_len_n += 1
    for i in lis[now]:
        if group[i] == -1:
            dfs(i)
group_len = []
for i in range(n):
    if group[i] == -1:
        group_len_n = 0
        dfs(i)
        group_len.append(group_len_n)
        g_n += 1
friend = [0 for _ in range(n)]
block = [0 for _ in range(n)]
for x in ab:
    a = x[0]
    b = x[1]
    if group[a] == group[b]:
        friend[a] += 1
        friend[b] += 1
for x in cd:
    c = x[0]
    d = x[1]
    if group[c] == group[d]:
        block[c] += 1
        block[d] += 1
ans = []
for i in range(n):
    ans.append(str(group_len[group[i]] - friend[i] - block[i] -1))
print(" ".join(ans))

Fixed submission:
import sys
sys.setrecursionlimit(2**20)
n, m, k = map(int, input().split())
ab = []
for _ in range(m):
    ab.append([int(x)-1 for x in input().split()])
cd = []
for _ in range(k):
    cd.append([int(x)-1 for x in input().split()])
group = [-1 for _ in range(n)]
lis = [[] for _ in range(n)]
for x in ab:
    a = x[0]
    b = x[1]
    lis[a].append(b)
    lis[b].append(a)
g_n = 0
def dfs(now):
    global g_n
    group[now] = g_n
    global group_len_n
    group_len_n += 1
    for i in lis[now]:
        if group[i] == -1:
            dfs(i)
group_len = []
for i in range(n):
    if group[i] == -1:
        group_len_n = 0
        dfs(i)
        group_len.append(group_len_n)
        g_n += 1
friend = [0 for _ in range(n)]
block = [0 for _ in range(n)]
for x in ab:
    a = x[0]
    b = x[1]
    if group[a] == group[b]:
        friend[a] += 1
        friend[b] += 1
for x in cd:
    c = x[0]
    d = x[1]
    if group[c] == group[d]:
        block[c] += 1
        block[d] += 1
ans = []
for i in range(n):
    ans.append(str(group_len[group[i]] - friend[i] - block[i] -1))
print(" ".join(ans))
Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**6)
n = int(input())
k = int(input())
def dfs(s, count):
    global ans
    if int(s) > n:
        return
    if count == 0:
        ans += 1
        dfs(s + "0", count)
    else:
        dfs(s + "0", count)
        count -= 1
        for i in range(1, 10):
            dfs(s + str(i), count)
ans = 0
for i in range(1,10):
    dfs(str(i), k-1)
print(ans)

Fixed submission:
s = input()
k = int(input())
l = len(s)
dp = [[[0 for i in range(k+1)] for j in range(2)] for m in range(l+1)]
dp[0][0][0] = 1
for i in range(l):
    for x in range(k+1):
        if s[i] == "0":
            dp[i+1][0][x] += dp[i][0][x]
        else:
            if x < k:
                dp[i+1][0][x+1] += dp[i][0][x]
            dp[i+1][1][x] += dp[i][0][x]
        for j in range(1, int(s[i])):
            if x < k:
                dp[i+1][1][x+1] += dp[i][0][x]
        dp[i+1][1][x] += dp[i][1][x]
        for j in range(1, 10):
            if x < k:
                dp[i+1][1][x+1] += dp[i][1][x]
print(dp[-1][0][-1] + dp[-1][1][-1])
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
n = int(input())
matrix= []
lis = [[] for i in range(n)]
ans = [[0 for i in range(n)] for j in range(n)]
for i in range(n-1):
    a,b = map(int, input().split())
    a -= 1
    b -= 1
    matrix.append(sorted([a,b]))
    lis[a].append(b)
    lis[b].append(a)
color = [-1 for _ in range(n)]
queue = deque([0])
color[0] = 1001001001
maxc = 0
while len(queue) > 0:
    now = queue.pop()
    used = color[now]
    tmp = 1
    for i in lis[now]:
        if color[i] == -1:
            queue.append(i)
            if tmp == used:
                tmp += 1
            color[i] = tmp
            ans[now][i] = tmp
            ans[i][now] = tmp
            maxc = max(maxc, tmp)
            tmp += 1
print(maxc)
for i in range(n-1):
    root = matrix[i]
    print(ans[root[0]][root[1]])

Fixed submission:
from collections import deque
n = int(input())
matrix= []
lis = [[] for i in range(n)]
ans = {}
for i in range(n):
    ans[i] = {}
for i in range(n-1):
    a,b = map(int, input().split())
    a -= 1
    b -= 1
    matrix.append(sorted([a,b]))
    lis[a].append(b)
    lis[b].append(a)
color = [-1 for _ in range(n)]
queue = deque([0])
color[0] = 1001001001
maxc = 0
while len(queue) > 0:
    now = queue.pop()
    used = color[now]
    tmp = 1
    for i in lis[now]:
        if color[i] == -1:
            queue.append(i)
            if tmp == used:
                tmp += 1
            color[i] = tmp
            ans[now][i] = tmp
            ans[i][now] = tmp
            maxc = max(maxc, tmp)
            tmp += 1
print(maxc)
for i in range(n-1):
    root = matrix[i]
    print(ans[root[0]][root[1]])
Buggy python submission (verdict: Time Limit Exceeded):
mod = 998244353
n = int(input())
d = list(map(int, input().split()))
if d[0] != 0:
    print(0)
    exit()
lis = []
for i in range(n):
    lis.append(d.count(i))
if lis[0] != 1:
    print(0)
    exit()
ans = 1
for i in range(1, n):
    ans *= pow(lis[i-1], lis[i], mod)
    ans %= mod
print(ans)

Fixed submission:
mod = 998244353
n = int(input())
d = list(map(int, input().split()))
if d[0] != 0:
    print(0)
    exit()
lis = [0] * n
for i in d:
    lis[i] += 1
if lis[0] != 1:
    print(0)
    exit()
ans = 1
for i in range(1, n):
    ans *= pow(lis[i-1], lis[i], mod)
    ans %= mod
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**6)
n, q = map(int, input().split())
lis = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    lis[a].append(b)
    lis[b].append(a)
c_plus = [0 for _ in range(n)]
for _ in range(q):
    p, x = map(int, input().split())
    p -= 1
    c_plus[p] += x
ans = [-1 for _ in range(n)]
def dfs(inp):
    now = inp[0]
    count = inp[1]
    count += c_plus[now]
    ans[now] = count
    for next in lis[now]:
        if ans[next] == -1:
            dfs([next, count])
dfs([0,0])
for i in range(n):
    ans[i] = str(ans[i])
print(" ".join(ans))

Fixed submission:
import sys
sys.setrecursionlimit(10**6)
n, q = map(int, input().split())
lis = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    lis[a].append(b)
    lis[b].append(a)
c_plus = [0 for _ in range(n)]
for _ in range(q):
    p, x = map(int, input().split())
    p -= 1
    c_plus[p] += x
ans = [-1 for _ in range(n)]
def dfs(inp):
    now = inp[0]
    count = inp[1]
    count += c_plus[now]
    ans[now] = count
    for next in lis[now]:
        if ans[next] == -1:
            dfs([next, count])
dfs([0,0])
for i in range(n):
    ans[i] = str(ans[i])
print(" ".join(ans))

Buggy python submission (verdict: Runtime Error):
n, q = map(int, input().split())
lis = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    lis[a].append(b)
    lis[b].append(a)
c_plus = [0 for _ in range(n)]
for _ in range(q):
    p, x = map(int, input().split())
    p -= 1
    c_plus[p] += x
ans = [-1 for _ in range(n)]
def dfs(inp):
    now = inp[0]
    count = inp[1]
    count += c_plus[now]
    ans[now] = count
    for next in lis[now]:
        if ans[next] == -1:
            dfs([next, count])
dfs([0,0])
for i in range(n):
    ans[i] = str(ans[i])
print(" ".join(ans))

Fixed submission:
import sys
sys.setrecursionlimit(10**6)
n, q = map(int, input().split())
lis = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    lis[a].append(b)
    lis[b].append(a)
c_plus = [0 for _ in range(n)]
for _ in range(q):
    p, x = map(int, input().split())
    p -= 1
    c_plus[p] += x
ans = [-1 for _ in range(n)]
def dfs(inp):
    now = inp[0]
    count = inp[1]
    count += c_plus[now]
    ans[now] = count
    for next in lis[now]:
        if ans[next] == -1:
            dfs([next, count])
dfs([0,0])
for i in range(n):
    ans[i] = str(ans[i])
print(" ".join(ans))