Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
n = int(input())
print("Yes" if n[0]== n[2] else 'No' )

Fixed submission:
n=input()
print("Yes" if n[0]==n[2] else "No")

Buggy python submission (verdict: Runtime Error):
n = int(input())
print('Yes' if n[0] == n[2] else 'No')

Fixed submission:
n=input()
print("Yes" if n[0]==n[2] else "No")
Buggy python submission (verdict: Runtime Error):
from math import gcd

n = int(input())


def lcm(a, b):
    return a * b // gcd(a, b)


ans = 1
for _ in range(n):
    t = int(input())
    ans = lcm(ans, t)

print(ans)


Fixed submission:
from fractions import gcd

n = int(input())


def lcm(a, b):
    return a * b // gcd(a, b)


ans = 1
for _ in range(n):
    t = int(input())
    ans = lcm(ans, t)

print(ans)
Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
print(~-a*~-b)


Fixed submission:
n, m = map(int, input().split())
print((n-1)*(m-1))
Buggy python submission (verdict: Runtime Error):
a, *b, c = input()
print(a+str(len(*b))+c)

Fixed submission:
s = input()
print(s[0], str(len(s)-2), s[-1], sep='')
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
A = list(map(int, input().split()))
B = []

for i in range(n):
    B.append(A[i])
    B = B[::-1]

print(*B)


Fixed submission:
from collections import deque

n = int(input())
A = list(map(int, input().split()))
deq = deque([])

for i in range(1, n+1):
    if i % 2 == 1:
        deq.append(A[i-1])
    else:
        deq.appendleft(A[i-1])

if n % 2 == 0:
    print(*deq)
else:
    print(*reversed(list(deq)))

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
A = list(map(int, input().split()))
B = []

for i in range(n):
    B.append(A[i])
    B = B[::-1]

print(*B)


Fixed submission:
from collections import deque

n = int(input())
A = list(map(int, input().split()))
deq = deque([])

for i in range(1, n+1):
    if i % 2 == 1:
        deq.append(A[i-1])
    else:
        deq.appendleft(A[i-1])

if n % 2 == 0:
    print(*deq)
else:
    print(*reversed(list(deq)))
Buggy python submission (verdict: Runtime Error):
a, b, c = map(int, input().split()), reverse = True
A = 100*a + 10*b + c

if A % 4 == 0:
    print('YES')
else:
    print('NO')


Fixed submission:
a, b, c = map(int, input().split())
A = 100*a + 10*b + c

if A % 4 == 0:
    print('YES')
else:
    print('NO')
Buggy python submission (verdict: Time Limit Exceeded):
k, s = map(int, input().split())
cnt = 0

for x in range(k+1):
    for y in range(k+1):
        for z in range(k+1):
            if x+y+z == s:
                cnt += 1

print(cnt)


Fixed submission:
k, s = map(int, input().split())
cnt = 0

for x in range(k+1):
    for y in range(k+1):
        if s-k <= x+y <= s:
            cnt += 1

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
k, s = map(int, input().split())
cnt = 0

for x in range(k+1):
    for y in range(k+1):
        for z in range(k+1):
            if x+y+z == s:
                cnt += 1

print(cnt)


Fixed submission:
k, s = map(int, input().split())
cnt = 0

for x in range(k+1):
    for y in range(k+1):
        if s-k <= x+y <= s:
            cnt += 1

print(cnt)
Buggy python submission (verdict: Runtime Error):
a,b,c,d=[int(input) for i in range(4)]
print(a*c-(c-d)*max(b-a,0))

Fixed submission:
n,k,x,y=(int(input()) for i in [0]*4)
print(n*x-(x-y)*max(n-k,0))
Buggy python submission (verdict: Runtime Error):
def patterns(start_n, broken_n):
    # start;自分がいる場所，こわれていない．一つ前はこわれている
    diff = broken_n - start_n
    if diff == 1 or diff == 2:
        return 1
    return patterns(start_n, broken_n - 1) + patterns(start_n, broken_n - 2)


inp = input()
split_input = inp.split()
n = int(split_input[0])
m = int(split_input[1])
start = 0
patterns_count = 1
# broken_number = -1
for i in range(m+1):
    if i == m:
        broken = n + 1
    else:
        inp = input()
        broken = int(inp)
    if broken == start:
        # 二つ連続でと通れない
        print(0)
        exit()
    # print(start, broken)
    p = patterns(start, broken)
    patterns_count = patterns_count * p % 1000000007
    start = broken + 1

print(patterns_count)






Fixed submission:
patterns_dictionary = {}
patterns_dictionary[1] = 1
patterns_dictionary[2] = 1


def patterns(start_n, broken_n):
    # start;自分がいる場所，こわれていない．一つ前はこわれている
    diff = broken_n - start_n
    if diff in patterns_dictionary:
        return patterns_dictionary[diff]
    for ii in range(len(patterns_dictionary)+1, diff+1):
        patterns_dictionary[ii] = patterns_dictionary[ii-1] + patterns_dictionary[ii-2]
    ans = patterns(start_n, broken_n - 1) + patterns(start_n, broken_n - 2)
    patterns_dictionary[diff] = ans
    return ans


inp = input()
split_input = inp.split()
n = int(split_input[0])
m = int(split_input[1])
start = 0
patterns_count = 1
# broken_number = -1
for i in range(m+1):
    if i == m:
        broken = n + 1
    else:
        inp = input()
        broken = int(inp)
    if broken == start:
        # 二つ連続で通れない
        print(0)
        exit()
    # print(start, broken)
    p = patterns(start, broken)
    patterns_count = (patterns_count * p) % 1000000007
    start = broken + 1

print(patterns_count)
Buggy python submission (verdict: Runtime Error):
alphabet = "abcdefghijklmnopqrstrvwxyz"


n = int(input()) - 1

A = []
while n > 0:
    A.append(n % 26)
    n //= 26

m = len(A)

if m == 1:
    print(alphabet[A[0]])
    exit()

ANS = []
ANS.append(alphabet[A[0]])
for i in range(1, m - 1):
    if A[i] == 0:
        ANS.append("z")
        A[i + 1] -= 1
    else:
        ANS.append(alphabet[A[i] - 1])
if A[-1]:
    ANS.append(alphabet[A[-1] - 1])

print("".join(ANS[::-1]))


Fixed submission:
alphabets = "abcdefghijklmnopqrstuvwxyz"


n = int(input())

A = []
while True:
    A.append(n % 26)
    n //= 26
    if n == 0:
        break

for i in range(len(A) - 1):
    if A[i] <= 0:
        A[i] += 26; A[i + 1] -= 1

ANS = []
for a in A[::-1]:
    if a == 0:
        continue
    ANS.append(alphabets[a - 1])

print("".join(ANS))
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
A = sorted(map(int, input().split()))

B = [1] * n
for i in range(n):
    if B[i] == 0:
        continue
    a = A[i]
    for j in range(i + 1, n):
        if A[j] % a == 0:
            B[j] = 0

for i in range(n - 1):
    if B[i] == 1:
        if A[i] == A[i + 1]:
            B[i] = 0

print(sum(B))


Fixed submission:
from collections import Counter


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

ok = [False] * (10**6 + 1)
for a in A:
    ok[a] = True

for i in range(10**6 + 1):
    if ok[i]:
        j = 2
        while i * j <= 10**6:
            ok[i * j] = False
            j += 1

for a, cnt in Counter(A).items():
    if cnt >= 2:
        ok[a] = False

print(sum(ok))
Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline


alphabets = "abcdefghijklmnopqrstuvwxyz"

c = input()
i = alphabets.index(c)

answer = alphabets[i + 1]
print(answer)


Fixed submission:
import sys
input = sys.stdin.readline


alphabets = "abcdefghijklmnopqrstuvwxyz"

c = input().strip()
i = alphabets.index(c)

answer = alphabets[i + 1]
print(answer)
Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

mod = 10**9 + 7


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

C1 = [1]
for i in range(n - k):
    C1.append((k + i) / (i + 1))
C2 = [1]
for i in range(n - k):
    C2.append(int(C2[i] * C1[i + 1]))

A.sort()
A_max = A[k - 1:]
sum_max = 0
for i in range(n - k + 1):
    sum_max += A_max[i] * C2[i]

A.sort(reverse = True)
A_min = A[k - 1:]
sum_min = 0
for i in range(n - k + 1):
    sum_min += A_min[i] * C2[i]

print((sum_max - sum_min) % mod)


Fixed submission:
import sys
input = sys.stdin.readline

mod = 10**9 + 7


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

FACTORIAL = [1] * (n + 1)
for i in range(1, n + 1):
    FACTORIAL[i] = FACTORIAL[i - 1] * i % mod
INVERSE = [1] * (n + 1)
INVERSE[n] = pow(FACTORIAL[n], mod - 2, mod)
for i in range(n - 1, -1, -1):
    INVERSE[i] = INVERSE[i + 1] * (i + 1) % mod

def combination(n, r):
    return FACTORIAL[n] * INVERSE[n - r] % mod * INVERSE[r] % mod

A.sort()
sum_max = 0
for i in range(k - 1, n):
    sum_max = (sum_max + A[i] * combination(i, k - 1)) % mod

A.sort(reverse = True)
sum_min = 0
for i in range(k - 1, n):
    sum_min = (sum_min + A[i] * combination(i, k - 1)) % mod

print((sum_max - sum_min) % mod)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

def combination(n, r, mod):
    numerator, denominator = 1, 1
    for _ in range(r):
        numerator = numerator * n % mod
        denominator = denominator * r % mod
        n -= 1
        r -= 1
    return numerator * pow(denominator, mod - 2, mod) % mod

mod = 10**9 + 7


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

A.sort()
sum_max = 0
for i in range(k - 1, n):
    sum_max += A[i] * combination(i, k - 1, mod)

A.sort(reverse = True)
sum_min = 0
for i in range(k - 1, n):
    sum_min += A[i] * combination(i, k - 1, mod)

print((sum_max - sum_min) % mod)


Fixed submission:
import sys
input = sys.stdin.readline

mod = 10**9 + 7


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

FACTORIAL = [1] * (n + 1)
for i in range(1, n + 1):
    FACTORIAL[i] = FACTORIAL[i - 1] * i % mod
INVERSE = [1] * (n + 1)
INVERSE[n] = pow(FACTORIAL[n], mod - 2, mod)
for i in range(n - 1, -1, -1):
    INVERSE[i] = INVERSE[i + 1] * (i + 1) % mod

def combination(n, r):
    return FACTORIAL[n] * INVERSE[n - r] % mod * INVERSE[r] % mod

A.sort()
sum_max = 0
for i in range(k - 1, n):
    sum_max = (sum_max + A[i] * combination(i, k - 1)) % mod

A.sort(reverse = True)
sum_min = 0
for i in range(k - 1, n):
    sum_min = (sum_min + A[i] * combination(i, k - 1)) % mod

print((sum_max - sum_min) % mod)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
list_a = list(map(int, input().split()))

i = 1
while True:
    if i in list_a:
        index = list_a.index(i)
        list_a = list_a[index:]
        i += 1
    else:
        break

print(N - (i - 1) if i != 1 else -1)


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

count = 1
for i in list_a:
    if i == count:
        count += 1

print(N - (count - 1) if count != 1 else -1)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**9)


n, u, v = map(int, input().split())

GRAPH = {i:[] for i in range(1, n + 1)}
for _ in range(n - 1):
    a, b = map(int, input().split())
    GRAPH[a].append(b)
    GRAPH[b].append(a)

def dfs(now, prev = 0):
    PARENT[now] = prev
    DEPTH[now] = DEPTH[prev] + 1
    if len(GRAPH[now]) == 1:
        LEAVES.append((DEPTH[now], now))
    
    for w in GRAPH[now]:
        if w == prev:
            continue
        next = w
        dfs(next, now)

PARENT = {}
DEPTH = {0:-1}
LEAVES = []
dfs(v)

PATH = []
now = u
while True:
    PATH.append(now)
    if now == v:
        break
    now = PARENT[now]

LEAVES.sort(reverse = True)
for leaf in LEAVES:
    now = leaf[1]
    while True:
        if now in PATH:
            break
        now = PARENT[now]
    lca = now
    
    du, dv = DEPTH[u] - DEPTH[lca], DEPTH[lca]
    if du < dv:
        print(leaf[0] - 1)
        exit()


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


n, u, v = map(int, input().split())

GRAPH = {i:[] for i in range(1, n + 1)}
for _ in range(n - 1):
    a, b = map(int, input().split())
    GRAPH[a].append(b)
    GRAPH[b].append(a)

def dfs(now, prev = 0):
    PARENT[now] = prev
    DEPTH[now] = DEPTH[prev] + 1
    if len(GRAPH[now]) == 1:
        LEAVES.append((DEPTH[now], now))
    
    for w in GRAPH[now]:
        if w == prev:
            continue
        next = w
        dfs(next, now)

PARENT = {}
DEPTH = {0:-1}
LEAVES = []
dfs(v)

PATH = {i:False for i in range(1, n + 1)}
now = u
while True:
    PATH[now] = True
    if now == v:
        break
    now = PARENT[now]

LEAVES.sort(reverse = True)
for leaf in LEAVES:
    now = leaf[1]
    while True:
        if PATH[now]:
            break
        now = PARENT[now]
    lca = now
    
    du, dv = DEPTH[u] - DEPTH[lca], DEPTH[lca]
    if du < dv:
        print(leaf[0] - 1)
        exit()
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())

testimonies_all = []
for i in range(N):
    A = int(input())
    testimonies = []
    for j in range(A):
        x, y = map(int, input().split())
        testimonies.append((x, y))
    testimonies_all.append(testimonies)

import itertools
assumptions = list(itertools.product((0, 1), repeat = N))

answers = []
for i in assumptions:
    assumptions2 = []
    for j in range(N):
        assumptions2.append((j + 1, i[j]))
        if i[j] == 1:
            for k in testimonies_all[j]:
                assumptions2.append(k)
    assumptions2.sort()
    count = 0
    for j in range(len(assumptions2) - 1):
        if assumptions2[j][0] == assumptions2[j + 1][0] and assumptions2[j][1] != assumptions2[j + 1][1]:
            count += 1
    if count == 0:
        answers.append(sum(i))

print(max(answers))


Fixed submission:
N = int(input())

testimonies_all = []
for i in range(N):
    A = int(input())
    testimonies = []
    for j in range(A):
        x, y = map(int, input().split())
        testimonies.append((x, y))
    testimonies_all.append(testimonies)

import itertools
assumptions = list(itertools.product((0, 1), repeat = N))

answers = []
for i in assumptions:
    assumptions2 = []
    for j in range(N):
        assumptions2.append((j + 1, i[j]))
        if i[j] == 1:
            for k in testimonies_all[j]:
                assumptions2.append(k)
    assumptions2.sort()
    count = 0
    for j in range(len(assumptions2) - 1):
        if assumptions2[j][0] == assumptions2[j + 1][0] and assumptions2[j][1] != assumptions2[j + 1][1]:
            count += 1
            break
    if count == 0:
        answers.append(sum(i))

print(max(answers))
Buggy python submission (verdict: Time Limit Exceeded):
mod = 10**9 + 7


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


A = [format(a, "060b")[::-1] for a in A]

answer = 0
for i in range(60):
    x, y = 0, 0
    for a in A:
        if a[i] == "0":
            x += 1
        else:
            y += 1
    answer += (x * y) * 2**i
    answer %= mod


print(answer)


Fixed submission:
mod = 10**9 + 7


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


A = [format(a, "060b")[::-1] for a in A]

answer = 0
for i in range(60):
    x, y = 0, 0
    for a in A:
        if a[i] == "0":
            x += 1
        else:
            y += 1
    answer += (x * y) * 2**i
    answer %= mod


print(answer)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
import numpy as np


n, t = map(int, input().split())

DISHES = [(0, 0)]
for _ in range(n):
    a, b = map(int, input().split())
    DISHES.append((a, b))
DISHES.sort()

DP = np.zeros((n + 1, t + 1), dtype = int)
for i in range(1, n + 1):
    for j in range(1, t + 1):
        a, b = DISHES[i][0], DISHES[i][1]
        if j - a < 0:
            DP[i][j] = DP[i - 1][j]
        else:
            DP[i][j] = max(DP[i - 1][j], DP[i - 1][j - a] + b)

for k in range(1, n + 1):
    DP[k][t] = DP[k - 1][t - 1] + DISHES[k][1]

print(np.max(DP))


Fixed submission:
import sys
input = sys.stdin.readline


n, t = map(int, input().split())

DISHES = [(0, 0)]
for _ in range(n):
    a, b = map(int, input().split())
    DISHES.append((a, b))
DISHES.sort()

DP = [[0] * (t + 1) for _ in range(n + 1)]
for i in range(1, n + 1):
    a, b = DISHES[i]
    for j in range(1, t + 1):
        if j - a < 0:
            DP[i][j] = DP[i - 1][j]
        else:
            DP[i][j] = max(DP[i - 1][j], DP[i - 1][j - a] + b)

answer = 0
for k in range(1, n + 1):
    answer = max(answer, DP[k - 1][t - 1] + DISHES[k][1])

print(answer)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline


n, t = map(int, input().split())

DISHES = [(0, 0)]
for _ in range(n):
    a, b = map(int, input().split())
    DISHES.append((a, b))
DISHES.sort()

DP = [[0] * (t + 1) for _ in range(n + 1)]
for i in range(1, n + 1):
    for j in range(1, t + 1):
        a, b = DISHES[i][0], DISHES[i][1]
        if j - a < 0:
            DP[i][j] = DP[i - 1][j]
        else:
            DP[i][j] = max(DP[i - 1][j], DP[i - 1][j - a] + b)

answer = 0
for k in range(1, n + 1):
    happiness_k = DP[k - 1][t - 1] + DISHES[k][1]
    if happiness_k > answer:
        answer = happiness_k

print(answer)


Fixed submission:
import sys
input = sys.stdin.readline


n, t = map(int, input().split())

DISHES = [(0, 0)]
for _ in range(n):
    a, b = map(int, input().split())
    DISHES.append((a, b))
DISHES.sort()

DP = [[0] * (t + 1) for _ in range(n + 1)]
for i in range(1, n + 1):
    a, b = DISHES[i]
    for j in range(1, t + 1):
        if j - a < 0:
            DP[i][j] = DP[i - 1][j]
        else:
            DP[i][j] = max(DP[i - 1][j], DP[i - 1][j - a] + b)

answer = 0
for k in range(1, n + 1):
    answer = max(answer, DP[k - 1][t - 1] + DISHES[k][1])

print(answer)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
lengths = list(map(int, input().split()))
lengths.sort()

count = 0

for i in range(N - 2):
    a = lengths[i]
    for j in range(i + 1, N - 1):
        b = lengths[j]
        
        L, R = j, N
        while R - L > 1:
            C = (L + R) // 2
            c = lengths[C]
            if c < a + b:
                L = C
            else:
                R = C
        
        count += L - j

print(count)


Fixed submission:
import bisect


n = int(input())
L = list(map(int, input().split()))
L.sort()

ans = 0
for i in range(n - 2):
    for j in range(i + 1, n - 1):
        k = bisect.bisect_left(L, L[i] + L[j])
        ans += k - (j + 1)

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
def gcd(m, n):
    a, b = m, n
    r = a % b
    while r != 0:
        a, b = b, r
        r = a % b
    return b

def divisors(x):
    D = []
    for i in range(1, int(x**0.5) + 1):
        if x % i == 0:
            D.append(i)
            if i != x**0.5:
                D.append(x // i)
    return D


a, b = map(int, input().split())

CD = divisors(gcd(a, b))

ans = 1
for cd in CD:
    if len(divisors(cd)) == 2:
        ans += 1

print(ans)


Fixed submission:
def gcd(x, y):
    a, b = x, y
    r = a % b
    while r != 0:
        a, b = b, r
        r = a % b
    return b

def divisors(x):
    D = []
    for i in range(1, int(x**0.5) + 1):
        if x % i == 0:
            D.append(i)
            if i != x**0.5:
                D.append(x // i)
    D.sort()
    return D

def isprime(x):
    t = 1
    for i in range(2, int(x**0.5) + 1):
        if x % i == 0:
            t = 0
            break
    if t == 1 and x != 1:
        return True
    else:
        return False


a, b = map(int, input().split())

CD = divisors(gcd(a, b))

ans = 1
for cd in CD:
    if isprime(cd):
        ans += 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def gcd(m, n):
    a, b = m, n
    r = a % b
    while r != 0:
        a, b = b, r
        r = a % b
    return b

def divisors(x):
    D = []
    for i in range(1, int(x**0.5) + 1):
        if x % i == 0:
            D.append(i)
            if i != x**0.5:
                D.append(x // i)
    D.sort()
    return D


a, b = map(int, input().split())

CD = divisors(gcd(a, b))

ans = 1
for cd in CD:
    if len(divisors(cd)) == 2:
        ans += 1

print(ans)


Fixed submission:
def gcd(x, y):
    a, b = x, y
    r = a % b
    while r != 0:
        a, b = b, r
        r = a % b
    return b

def divisors(x):
    D = []
    for i in range(1, int(x**0.5) + 1):
        if x % i == 0:
            D.append(i)
            if i != x**0.5:
                D.append(x // i)
    D.sort()
    return D

def isprime(x):
    t = 1
    for i in range(2, int(x**0.5) + 1):
        if x % i == 0:
            t = 0
            break
    if t == 1 and x != 1:
        return True
    else:
        return False


a, b = map(int, input().split())

CD = divisors(gcd(a, b))

ans = 1
for cd in CD:
    if isprime(cd):
        ans += 1

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
A = list(map(int, input().split()))

for _ in range(m):
    i = A.index(max(A))
    A[i] /= 2
A = [int(a) for a in A]

print(sum(A))


Fixed submission:
n, m = map(int, input().split())
A = list(map(int, input().split()))

A = [a * (-1) for a in A]
import heapq
heapq.heapify(A)

for _ in range(m):
    a = heapq.heappop(A) * (-1)
    heapq.heappush(A, a // 2 * (-1))

print(abs(sum(A)))

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
A = list(map(int, input().split()))

A.sort()
for _ in range(m):
    A[-1] /= 2
    A.sort()
A = [int(a) for a in A]

print(sum(A))


Fixed submission:
n, m = map(int, input().split())
A = list(map(int, input().split()))

A = [a * (-1) for a in A]
import heapq
heapq.heapify(A)

for _ in range(m):
    a = heapq.heappop(A) * (-1)
    heapq.heappush(A, a // 2 * (-1))

print(abs(sum(A)))
Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(2 * 10**5)
def input():
    return sys.stdin.readline()


n, q = map(int, input().split())
GRAPH = [[] for _ in range(n + 1)]
COUNTS = [0] * (n + 1)

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

for _ in range(q):
    p, x = map(int, input().split())
    COUNTS[p] += x

def dfs(now, prev):
    for vertex in GRAPH[now]:
        if vertex == prev:
            continue
        else:
            next = vertex
        COUNTS[next] += COUNTS[now]
        dfs(next, now)
dfs(1, 0)

answer = " ".join(map(str, COUNTS[1:]))
print(answer)


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


n, q = map(int, input().split())
GRAPH = [[] for _ in range(n + 1)]
COUNTS = [0] * (n + 1)

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

for _ in range(q):
    p, x = map(int, input().split())
    COUNTS[p] += x

def dfs(now, prev):
    for vertex in GRAPH[now]:
        if vertex == prev:
            continue
        else:
            next = vertex
        COUNTS[next] += COUNTS[now]
        dfs(next, now)
dfs(1, 0)

answer = " ".join(map(str, COUNTS[1:]))
print(answer)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(2 * 10**5)


n, q = map(int, input().split())
GRAPH = [[] for _ in range(n + 1)]
COUNTS = [0] * (n + 1)

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

for _ in range(q):
    p, x = map(int, input().split())
    COUNTS[p] += x

def dfs(now, prev):
    for vertex in GRAPH[now]:
        if vertex == prev:
            continue
        else:
            next = vertex
        COUNTS[next] += COUNTS[now]
        dfs(next, now)
dfs(1, 0)

answer = " ".join(map(str, COUNTS[1:]))
print(answer)


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


n, q = map(int, input().split())
GRAPH = [[] for _ in range(n + 1)]
COUNTS = [0] * (n + 1)

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

for _ in range(q):
    p, x = map(int, input().split())
    COUNTS[p] += x

def dfs(now, prev):
    for vertex in GRAPH[now]:
        if vertex == prev:
            continue
        else:
            next = vertex
        COUNTS[next] += COUNTS[now]
        dfs(next, now)
dfs(1, 0)

answer = " ".join(map(str, COUNTS[1:]))
print(answer)