Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
n = int(input())
a = list(map(int,input().split()))
if a[-1] >= a[-2]:
    a.append(float('inf'))
else:
    a.append(-float('inf'))
ans = 1
i = 0
while i < n-1:
    if a[i] < a[i+1] > a[i+2]:
        ans += 1
        i += 2
    elif a[i] > a[i+1] < a[i+2]:
        ans += 1
        i += 2
    else:
        i += 1
print(ans)

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

ans = 1
status = 0
prev = a[0]

for i in a:
    diff = i - prev

    if diff == 0:
        pass
    elif diff > 0:
        if status >= 0:
            status = diff
        else:
            ans += 1
            status = 0
    else:
        if status <= 0:
            status = diff
        else:
            ans += 1
            status = 0

    prev = i

print(ans)

Buggy python submission (verdict: Runtime Error):
n = int(input())
l = list(map(int,input().split()))
if l[-1] > l[-2]:
    l += [float('Inf')]
else:
    l += [-float('Inf')]
ans = 1
i = 1
while i <= n - 1:
    if l[i-1] <= l[i] and l[i] <= l[i+1]:
        i += 1
    elif l[i-1] >= l[i] and l[i] >= l[i+1]:
        i += 1
    else:
        #print(l[i-1],l[i],l[i+1])
        ans += 1
        i += 2
        
print(ans)

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

ans = 1
status = 0
prev = a[0]

for i in a:
    diff = i - prev

    if diff == 0:
        pass
    elif diff > 0:
        if status >= 0:
            status = diff
        else:
            ans += 1
            status = 0
    else:
        if status <= 0:
            status = diff
        else:
            ans += 1
            status = 0

    prev = i

print(ans)
Buggy python submission (verdict: Runtime Error):
w,a,b = map(int,input().split())
ans = 0
if b > a:
    if a + w < b:
        ans = b - (a + w)
else:
    if b + w <= a:
        ans = a - (b + w)

print(ans)


Fixed submission:
A,B = map(str,input().split())
if A == "H":
  A = 1
else:
  A = -1
if B == "H":
  B = 1
else:
  B = -1

if A*B > 0:
  print("H")
else:
  print("D")
Buggy python submission (verdict: Runtime Error):
def dfs(v,been):
    if len(been) == n:
        ans.append(1)
        #print(been)
        return
    for i in graph[v]:
        if i in been:
            pass
        else:
            been.add(i)
            dfs(i,been)
            been.remove(i)
    return

n,m = map(int,input().split())
graph = [[] for _ in range(m+1)]
for _ in range(m):
    a,b = map(int,input().split())
    graph[a].append(b)
    graph[b].append(a)
#print(graph)

ans = []

dfs(1,set([1]))
print(sum(ans))

Fixed submission:
def dfs(v,been):
    if len(been) == n:
        ans.append(1)
        #print(been)
        return
    for i in graph[v]:
        if i in been:
            pass
        else:
            been.add(i)
            dfs(i,been)
            been.remove(i)
    return

n,m = map(int,input().split())
graph = [[] for _ in range(n+1)]
for _ in range(m):
    a,b = map(int,input().split())
    graph[a].append(b)
    graph[b].append(a)
#print(graph)

ans = []

dfs(1,set([1]))
print(sum(ans))
Buggy python submission (verdict: Runtime Error):
K,S = map(int,input().split())
count = 0
for i in range(K+1):
    for j in range(K+1):
        for k in range(K+1):
            if i+j+k == S:
                count += 1
print(count)


Fixed submission:
S = str(input())
print(S[:5]+" "+S[6:13]+" "+S[14:])
Buggy python submission (verdict: Time Limit Exceeded):
K,S = map(int,input().split())
count = 0
for i in range(K+1):
    for j in range(K+1):
        for k in range(K+1):
            if i+j+k == S:
                count += 1
print(count)


Fixed submission:
K,S = map(int,input().split())
count = 0
for i in range(K+1):
    for j in range(K+1):
        k = S - i -j
        if 0<= k <= K:
            
            count += 1
print(count)
Buggy python submission (verdict: Runtime Error):
N = int(input())
Problems = list(map(int,input().split()))
results = []
M = int(input())
for i in range(M):
  	temPro = Problems
    p,t = map(int,input().split())
    temPro[p-1] = t

    print(sum(temPro))


Fixed submission:
N = int(input())
Problems = list(map(int,input().split()))
M = int(input())
for i in range(M):
    p,t = map(int,input().split())
    k = Problems[p-1]
    Problems[p-1] = t
    print(sum(Problems))
    Problems[p-1] = k

Buggy python submission (verdict: Runtime Error):
N = int(input())
Problems = list(map(int,input().split()))
M = int(input())
for i in range(M):
    p,t = map(int,input().split())
    Problems[p] = t
    print(sum(Problems))


Fixed submission:
N = int(input())
Problems = list(map(int,input().split()))
M = int(input())
for i in range(M):
    p,t = map(int,input().split())
    k = Problems[p-1]
    Problems[p-1] = t
    print(sum(Problems))
    Problems[p-1] = k
Buggy python submission (verdict: Runtime Error):
c = str(input())
if c in [a,i,u,e,o]:
    print("vowel")
else:
    print("consonant")


Fixed submission:
c = str(input())
if c in ["a","i","u","e","o"]:
    print("vowel")
else:
    print("consonant")

Buggy python submission (verdict: Runtime Error):
c = str(input())
if c in {a,i,u,e,o}:
    print("vowel")
else:
    print("consonant")


Fixed submission:
c = str(input())
if c in ["a","i","u","e","o"]:
    print("vowel")
else:
    print("consonant")
Buggy python submission (verdict: Runtime Error):
a,b,x = map(int,input().split())

print(b // x - a //x)

Fixed submission:
A,B,C = map(str,input().split())
print("A"+B[0]+"C")
Buggy python submission (verdict: Time Limit Exceeded):
a,b,x = map(int,input().split())
count = 0
for i in range(a,b+1):
  if i % x == 0:
    count += 1
print(count)

Fixed submission:
a,b,x = map(int,input().split())
c = (b) // x - (a-1) //x
print(c)
Buggy python submission (verdict: Runtime Error):
h,w = map(int,input().split())
maps = []

for i in range(h):
    maps.append(list(map(str,input().split())))
for i in range(h):
    for j in range(w):
        if maps[i][j] == "snuke":
            print('{}{}'.format(chr(row.index('snuke')+65),i+1))
            exit()

Fixed submission:
h,w = map(int,input().split())
maps = []
alphabet = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
for i in range(h):
    maps.append(list(map(str,input().split())))
for i in range(h):
    for j in range(w):
        if maps[i][j] == "snuke":
            print(alphabet[j]+str(i+1))
            exit()

Buggy python submission (verdict: Runtime Error):
h,w = map(int,input().split())
maps = []

for i in range(h):
    maps.append(list(map(int,input().split())))
for i in range(h):
    for j in range(w):
        if maps[i][j] == "snuke":
            print(i+1)
            exit()

Fixed submission:
h,w = map(int,input().split())
maps = []
alphabet = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
for i in range(h):
    maps.append(list(map(str,input().split())))
for i in range(h):
    for j in range(w):
        if maps[i][j] == "snuke":
            print(alphabet[j]+str(i+1))
            exit()

Buggy python submission (verdict: Runtime Error):
h,w = map(int,input().split())
maps = []
for i in range(h):
    maps.append(list(map(int,input().split())))
for i in range(h):
    for j in range(w):
        if maps[i][j] == "snuke":
            print('{}{}'.format(chr(row.index('snuke')+65),i+1))
            exit()

Fixed submission:
h,w = map(int,input().split())
maps = []
alphabet = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
for i in range(h):
    maps.append(list(map(str,input().split())))
for i in range(h):
    for j in range(w):
        if maps[i][j] == "snuke":
            print(alphabet[j]+str(i+1))
            exit()

Buggy python submission (verdict: Runtime Error):
h,w = map(int,input().split())
maps = []
for i in range(h):
    maps.append(list(map(int,input().split())))
for i in range(h):
    for j in range(w):
        if maps[i][j] == "snuke":
            print(chr(ord('A') + w) + str(h + 1))
            exit()

Fixed submission:
h,w = map(int,input().split())
maps = []
alphabet = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
for i in range(h):
    maps.append(list(map(str,input().split())))
for i in range(h):
    for j in range(w):
        if maps[i][j] == "snuke":
            print(alphabet[j]+str(i+1))
            exit()
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a,b = map(int,input().split())
for _ in range(n-1):
    c,d = map(int,input().split())
    deltaA = 0
    deltaB = d/c * (a + deltaA) - b 
    #print(deltaA,deltaB)
    while deltaB < 0 or int(deltaB) != deltaB:
        deltaA += 1
        deltaB = d/c * (a + deltaA) - b 
        #print(deltaA,deltaB)
    a += int(deltaA)
    b += int(deltaB)
#print(a,b)
print(a+b)

Fixed submission:
n = int(input())
a,b = 1,1
for _ in range(n):
    x,y = map(int,input().split())
    k = -min(-a//x,-b//y)
    a = k*x
    b = k*y
print(a+b)
Buggy python submission (verdict: Runtime Error):
N = int(input())
n = list(map(int,input().split()))
ans  = 0
for i in range(N):
    if n[n[i]-1] == i:
        ans += 1 
print(ans)

Fixed submission:
s= str(input())
print(s[0:4]+" "+s[4:])
Buggy python submission (verdict: Runtime Error):
N = int(input())
n = list(map(int,input().split()))
ans  = 0
for i in range(N):
    if n[n[i]] == i:
        ans += 1 
print(ans)

Fixed submission:
N = int(input())
n = list(map(int,input().split()))
ans  = 0
for i in range(N):
    if n[n[i]-1] == i+1:
        ans += 1 
print(ans // 2)
Buggy python submission (verdict: Runtime Error):
a=int(input())
b=int(input())
c=int(input())
print((a+b)*h/2)


Fixed submission:
a = int(input())
b = int(input())
h = int(input())
print(int((a+b)*h/2))

Buggy python submission (verdict: Runtime Error):
a=int(input())
b=int(input())
c=int(input())
print((a+b)*h//2)


Fixed submission:
a = int(input())
b = int(input())
h = int(input())
print(int((a+b)*h/2))
Buggy python submission (verdict: Runtime Error):
s = str(input())
n = 0
w = 0
s = 0
e = 0
for i in range(len(s)):
    if s[i] == "N":
        n += 1
    elif s[i] == "W":
        w += 1
    elif s[i] == "S":
        s += 1
    else:
        e += 1
if (n == 0 and s > 0) or (s == 0 and n > 0) or (e == 0 and w > 0) or (w == 0 and e > 0):
    print("No")
else:
    print("No")

Fixed submission:
k = str(input())


n = 0
w = 0
s = 0
e = 0
for i in range(len(k)):
    if k[i] == "N":
        n += 1
    elif k[i] == "W":
        w += 1
    elif k[i] == "S":
        s += 1
    else:
        e += 1
if (n == 0 and s > 0) or (s == 0 and n > 0) or (e == 0 and w > 0) or (w == 0 and e > 0):
    print("No")
else:
    print("Yes")
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = [int(input()) for _ in range(n)]
os = [s[i] for i in range(0,n,2)]
j = sorted(s)
ans = 0
for i in os:
    if j.index(i) % 2 == 1:
        ans += 1
print(ans)

Fixed submission:
n = int(input())
s = [int(input()) for _ in range(n)]
j = sorted(s)
ans = len(set(s[::2]) ^ set(j[::2])) // 2
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = [int(input()) for _ in range(n)]
os = [s[i] for i in range(0,n,2)]
j = sorted(s)
ans = 0
for i in os:
    if j.index(i) % 2 == 1:
        ans += 1
print(ans)

Fixed submission:
n = int(input())
s = [int(input()) for _ in range(n)]
j = sorted(s)
ans = len(set(s[::2]) ^ set(j[::2])) // 2
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = [int(input()) for _ in range(n)]
j = sorted(s)
ans = 0
for i in range(0,n,2):
    if j.index(s[i]) % 2 == 1:
        ans += 1
print(ans)

Fixed submission:
n = int(input())
s = [int(input()) for _ in range(n)]
j = sorted(s)
ans = len(set(s[::2]) ^ set(j[::2])) // 2
print(ans)
Buggy python submission (verdict: Runtime Error):
from fractionsimport gcd
n,x=map(int,input().split())
print(3*(n-gcd(n,x)))


Fixed submission:
N = int(input())
kushi = sorted(list(map(int,input().split())))
sum = 0
for i in range(2*N):
    if i % 2 == 0:
        sum += kushi[i]
print(sum)
Buggy python submission (verdict: Runtime Error):
from fractions
n,x=map(int,input().split())
print(3*(n-fractions.gcd(n,x)))


Fixed submission:
from fractions import gcd
n,x=map(int,input().split())
print(3*(n-gcd(n,x)))

Buggy python submission (verdict: Runtime Error):
import math
n,x=map(int,input().split())
print(3*(n-math.gcd(n,x)))


Fixed submission:
from fractions import gcd
n,x=map(int,input().split())
print(3*(n-gcd(n,x)))

Buggy python submission (verdict: Runtime Error):
import math
n,x=map(int,input().split())
print(4*(n-math.gcd(n,x)))

Fixed submission:
from fractions import gcd
n,x=map(int,input().split())
print(3*(n-gcd(n,x)))

Buggy python submission (verdict: Runtime Error):
import math
n,x=map(int,input().split())
print(4*(n-gcd(n,x)))


Fixed submission:
from fractions import gcd
n,x=map(int,input().split())
print(3*(n-gcd(n,x)))
Buggy python submission (verdict: Runtime Error):
d, s, t = [int(i) for i in input().split()]
if s * t >= d:
  	print("Yes")
else
	print("No")

Fixed submission:
d, s, t = [int(i) for i in input().split()]
if s * t >= d:
  	print("Yes")
else:
	print("No")
Buggy python submission (verdict: Runtime Error):
n = input()
total = 0
for char in n:
	total = (total + int(char)) % 9
if !total:
	print("Yes")
else:
  	print("No")

Fixed submission:
num = input()
total = 0
for char in num:
    total = (total + int(char)) % 9
if total == 0:
    print("Yes")
else:
    print("No")
Buggy python submission (verdict: Time Limit Exceeded):
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    return divisors

n = int(input())
ans = 0
for i in range(1, n):
    ans += len(make_divisors(i))
print(ans)

Fixed submission:
n = int(input())
ans = 0
for i in range(1, n):
    ans += (n-1)//i
print(ans)
Buggy python submission (verdict: Runtime Error):
n = int(input())
c = sorted([int(_) for _ in input().split()], reverse=True)
ans = 0
mod = 10**9+7
for i in range(n):
    ans += (c[i] * (pow(2, n-1, mod) + pow(2, n-2, mod) * i)) % mod
    ans %= mod
ans *= pow(2, n, mod)
ans %= mod
print(ans)


Fixed submission:
import sys


n = int(input())
c = sorted([int(_) for _ in input().split()], reverse=True)
mod = 10**9+7
ans = 0
if n == 1:
    ans += c[0] * 2
    ans %= mod
    print(ans)
    sys.exit()
for i in range(n):
    ans += (c[i] * (pow(2, n-1, mod) + pow(2, n-2, mod) * i)) % mod
    ans %= mod
ans *= pow(2, n, mod)
ans %= mod
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n, k = [int(_) for _ in input().split()]
r_s, s_s, p_s = [int(_) for _ in input().split()]
t = input()

s = ''
for i in range(k):
    s += t[i]

for start in range(k):
    c = 0
    for i in range(start + k, n, k):
        if t[i] == t[i - k]:
            c += 1
            if c % 2 == 0:
                s += t[i]
        else:
            s += t[i]
            c = 0

ans = 0
for i in range(len(s)):
    if s[i] == 'r':
        ans += p_s
    elif s[i] == 's':
        ans += r_s
    else:
        ans += s_s
print(ans)

Fixed submission:
n, k = [int(_) for _ in input().split()]
r_s, s_s, p_s = [int(_) for _ in input().split()]
t = input()

s = ''
for i in range(k):
    s += t[i]

for start in range(k):
    c = 0
    for i in range(start + k, n, k):
        if t[i] == t[i - k]:
            c += 1
            if c % 2 == 0:
                s += t[i]
        else:
            s += t[i]
            c = 0

ans = 0
for i in range(len(s)):
    if s[i] == 'r':
        ans += p_s
    elif s[i] == 's':
        ans += r_s
    else:
        ans += s_s
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
n_list = [[] for _ in range(n+1)]
l_list = [0 for _ in range(n-1)]
for i in range(n-1):
    a,b = [int(_) for _ in input().split()]
    n_list[a].append((b,i))
    n_list[b].append((a,i))
# print(n_list)
k = 0
for i in range(n+1):
    k = max(k, len(n_list[i]))
# print(k)
c_list = []
for i in range(1, k+1):
    c_list.append(i)
# print(c_list)
todo = [(1,0)]
seen = [1]
while len(todo) != 0:
    p_from, c_from = todo.pop()
    i = 0
    for t in n_list[p_from]:
        p, l = t
        if p in seen:
            continue
        else:
            while c_list[i] == c_from:
                i += 1
            color = c_list[i]
            i += 1
            todo.append((p,color))
            l_list[l] = color
            seen.append(p)
    # print(todo)
    # print(seen)
# print(l_list)
print(k)
for i in l_list:
    print(i)

Fixed submission:
n = int(input())
n_list = [[] for _ in range(n+1)]
l_list = [0 for _ in range(n-1)]
for i in range(n-1):
    a,b = [int(_) for _ in input().split()]
    n_list[a].append((b,i))
    n_list[b].append((a,i))
# print(n_list)
k = 0
for i in range(n+1):
    k = max(k, len(n_list[i]))
# print(k)
c_list = []
for i in range(1, k+1):
    c_list.append(i)
# print(c_list)
todo = [(1,0,0)]
seen = [1]
while len(todo) != 0:
    p_from, c_from, before = todo.pop()
    i = 0
    for t in n_list[p_from]:
        p, l = t
        if p == before:
            continue
        else:
            while c_list[i] == c_from:
                i += 1
            color = c_list[i]
            i += 1
            todo.append((p,color,p_from))
            l_list[l] = color
            seen.append(p)
        # print(todo)
    # print(seen)
# print(l_list)
print(k)
for i in l_list:
    print(i)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
l = [int(_) for _ in input().split()]
ans = 0
L = [0 for _ in range(1,2000+2)]
s = [0 for _ in range(0,2000+2)]
for k in range(0, n):
    L[l[k]-1] += 1
for x in range(0,2001):
    s[x+1] = s[x] + L[x]

for i in range(0, n-1):
    for j in range(i+1, n):
        exc = 0
        m = abs(l[i] - l[j])
        M = l[i] + l[j]
        if l[i] > m:
            exc += 1
        if l[j] > m:
            exc += 1
        ans += s[M] - s[m] - L[M-1] - exc
print(ans//3)


Fixed submission:
n = int(input())
l = [int(_) for _ in input().split()]
ans = 0
L = [0 for _ in range(1,2000+2)]
s = [0 for _ in range(0,2000+2)]
for k in range(0, n):
    L[l[k]-1] += 1
for x in range(0,2001):
    s[x+1] = s[x] + L[x]
 
for i in range(0, n-1):
    for j in range(i+1, n):
        exc = 0
        m = abs(l[i] - l[j])
        M = l[i] + l[j]
        if l[i] > m:
            exc += 1
        if l[j] > m:
            exc += 1
        ans += s[M] - s[m] - L[M-1] - exc
print(ans//3)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
l = [int(_) for _ in input().split()]
ans = 0
L = [0 for _ in range(1,4000+1)]
s = [0 for _ in range(0,4000+1)]
for k in range(0, n):
    L[l[k]-1] += 1
for x in range(0,4000):
    s[x+1] = s[x] + L[x]

for i in range(0, n-1):
    for j in range(i+1, n):
        exc = 0
        m = abs(l[i] - l[j])
        M = l[i] + l[j]
        if l[i] > m:
            exc += 1
        if l[j] > m:
            exc += 1
        ans += s[M] - s[m] - L[M-1] - exc
print(ans//3)



Fixed submission:
n = int(input())
l = [int(_) for _ in input().split()]
ans = 0
L = [0 for _ in range(1,2000+2)]
s = [0 for _ in range(0,2000+2)]
for k in range(0, n):
    L[l[k]-1] += 1
for x in range(0,2001):
    s[x+1] = s[x] + L[x]
 
for i in range(0, n-1):
    for j in range(i+1, n):
        exc = 0
        m = abs(l[i] - l[j])
        M = l[i] + l[j]
        if l[i] > m:
            exc += 1
        if l[j] > m:
            exc += 1
        ans += s[M] - s[m] - L[M-1] - exc
print(ans//3)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
l = [int(_) for _ in input().split()]
ans = 0
L = [0 for _ in range(1,2000+1)]
s = [0 for _ in range(0,2000+1)]
for k in range(0, n):
    L[l[k]-1] += 1
for x in range(0,2000):
    s[x+1] = s[x] + L[x]

for i in range(0, n-1):
    for j in range(i+1, n):
        exc = 0
        m = abs(l[i] - l[j])
        M = l[i] + l[j]
        if l[i] > m:
            exc += 1
        if l[j] > m:
            exc += 1
        ans += s[M] - s[m] - L[M-1] - exc
print(ans//3)



Fixed submission:
n = int(input())
l = [int(_) for _ in input().split()]
ans = 0
L = [0 for _ in range(1,2000+2)]
s = [0 for _ in range(0,2000+2)]
for k in range(0, n):
    L[l[k]-1] += 1
for x in range(0,2001):
    s[x+1] = s[x] + L[x]
 
for i in range(0, n-1):
    for j in range(i+1, n):
        exc = 0
        m = abs(l[i] - l[j])
        M = l[i] + l[j]
        if l[i] > m:
            exc += 1
        if l[j] > m:
            exc += 1
        ans += s[M] - s[m] - L[M-1] - exc
print(ans//3)
Buggy python submission (verdict: Time Limit Exceeded):
n, q = [int(_) for _ in input().split()]
w = {}
add = {}
seen = {}
ans_d = {}
for i in range(1, n + 1):
    w[i] = []
    seen[i] = 0
    add[i] = 0
    ans_d[i] = 0
for i in range(n - 1):
    a, b = [int(_) for _ in input().split()]
    w[a].append(b)
    w[b].append(a)
for i in range(q):
    p, x = [int(_) for _ in input().split()]
    add[p] += x

que = [1]
ans_d[1] = add[1]
seen[1] = 1
while len(que) != 0:
    v = que.pop()
    for node in w[v]:
        if seen[node] == 0:
            ans_d[node] = ans_d[v] + add[node]
            que.append(node)
            seen[node] = 1

ans_l = []
for i in range(1, n + 1):
    ans_l.append(str(ans_d[i]))
print(' '.join(ans_l))

Fixed submission:
n, q = [int(_) for _ in input().split()]
w = {}
add = {}
seen = {}
ans_d = {}
for i in range(1, n + 1):
    w[i] = []
    seen[i] = 0
    add[i] = 0
    ans_d[i] = 0
for i in range(n - 1):
    a, b = [int(_) for _ in input().split()]
    w[a].append(b)
    w[b].append(a)
for i in range(q):
    p, x = [int(_) for _ in input().split()]
    add[p] += x

que = [1]
ans_d[1] = add[1]
seen[1] = 1
while len(que) != 0:
    v = que.pop()
    for node in w[v]:
        if seen[node] == 0:
            ans_d[node] = ans_d[v] + add[node]
            que.append(node)
            seen[node] = 1

ans_l = []
for i in range(1, n + 1):
    ans_l.append(str(ans_d[i]))
print(' '.join(ans_l))

Buggy python submission (verdict: Time Limit Exceeded):
n, q = [int(_) for _ in input().split()]
w = {}
add = {}
seen = {}
ans_d = {}
for i in range(1, n + 1):
    w[i] = []
    seen[i] = 0
    add[i] = 0
    ans_d[i] = 0
for i in range(n - 1):
    a, b = [int(_) for _ in input().split()]
    w[a].append(b)
    w[b].append(a)
for i in range(q):
    p, x = [int(_) for _ in input().split()]
    add[p] += x

que = [1]
ans_d[1] = add[1]
seen[1] = 1
while len(que) != 0:
    v = que.pop()
    for node in w[v]:
        if seen[node] == 0:
            ans_d[node] = ans_d[v] + add[node]
            que.append(node)
            seen[node] = 1

ans_l = []
for i in range(1, n + 1):
    ans_l.append(str(ans_d[i]))
print(' '.join(ans_l))

Fixed submission:
n, q = [int(_) for _ in input().split()]
w = {}
add = {}
seen = {}
ans_d = {}
for i in range(1, n + 1):
    w[i] = []
    seen[i] = 0
    add[i] = 0
    ans_d[i] = 0
for i in range(n - 1):
    a, b = [int(_) for _ in input().split()]
    w[a].append(b)
    w[b].append(a)
for i in range(q):
    p, x = [int(_) for _ in input().split()]
    add[p] += x

que = [1]
ans_d[1] = add[1]
seen[1] = 1
while len(que) != 0:
    v = que.pop()
    for node in w[v]:
        if seen[node] == 0:
            ans_d[node] = ans_d[v] + add[node]
            que.append(node)
            seen[node] = 1

ans_l = []
for i in range(1, n + 1):
    ans_l.append(str(ans_d[i]))
print(' '.join(ans_l))