Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
import numpy as np

N, Q = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(N-1)]
P = [list(map(int, input().split())) for _ in range(Q)]

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

dist = [-1 for _ in range(N)]
ans = [0 for _ in range(N)]
parent = [0 for _ in range(N)]
dist[0] = 0
d = deque([0])
while d:
    now = d.popleft()
    for i in range(len(link[now])):
        if dist[link[now][i]] == -1:
            dist[link[now][i]] = now + 1
            d.append(link[now][i])
            parent[link[now][i]] = now

P.sort()
for i in range(Q):
    ans[P[i][0] - 1] += P[i][1]

arg_dist = list(np.argsort(np.array(dist)))
for i in range(N):
    if i != 0:
        ans[arg_dist[i]] = ans[arg_dist[i]] + ans[parent[arg_dist[i]]]

for i in range(N):
    if i != N - 1:
        print(ans[i], end=" ")
    else:
        print(ans[i])


Fixed submission:
from collections import deque
import numpy as np

N, Q = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(N-1)]
P = [list(map(int, input().split())) for _ in range(Q)]

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


ans = [0 for _ in range(N)]
for i in range(Q):
  ans[P[i][0] - 1] += P[i][1]

dist = [-1 for _ in range(N)]
dist[0] = 0
d = deque([0])
while d:
    now = d.pop()
    for i in range(len(link[now])):
        if dist[link[now][i]] == -1:
            ans[link[now][i]] += ans[now]
            dist[link[now][i]] = 0
            d.append(link[now][i])

for i in range(N):
    if i != N - 1:
        print(ans[i], end=" ")
    else:
        print(ans[i])

Buggy python submission (verdict: Runtime Error):
from collections import deque
import numpy as np

N, Q = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(N-1)]
P = [list(map(int, input().split())) for _ in range(Q)]

dist = [-1 for _ in range(N)]
ans = [0 for _ in range(N)]
parent = [[] for _ in range(N)]
dist[0] = 0
d = deque([0])
while d:
    now = d.popleft()
    for i in range(N-1):
        if A[i][0] - 1 == now and dist[A[i][1] - 1] == -1:
            dist[A[i][1] - 1] = now + 1
            d.append(A[i][1] - 1)
            parent[A[i][1] - 1].append(dist[A[i][0] - 1])

        if A[i][1] - 1 == now and dist[A[i][0] - 1] == -1:
            dist[A[i][0] - 1] = now + 1
            d.append(A[i][0] - 1)
            parent[A[i][0] - 1].append(dist[A[i][1] - 1])

arg_dist = np.argsort(np.array(dist))

for i in range(Q):
    ans[P[i][0] - 1] += P[i][1]

for i in range(N):
    for j in range(len(parent[arg_dist[i]])):
        ans[arg_dist[i]] = ans[arg_dist[i]] + ans[parent[arg_dist[i]][j]]

for i in range(N):
    if i != N - 1:
        print(ans[arg_dist[i]], end=" ")
    else:
        print(ans[arg_dist[i]])


Fixed submission:
from collections import deque
import numpy as np

N, Q = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(N-1)]
P = [list(map(int, input().split())) for _ in range(Q)]

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


ans = [0 for _ in range(N)]
for i in range(Q):
  ans[P[i][0] - 1] += P[i][1]

dist = [-1 for _ in range(N)]
dist[0] = 0
d = deque([0])
while d:
    now = d.pop()
    for i in range(len(link[now])):
        if dist[link[now][i]] == -1:
            ans[link[now][i]] += ans[now]
            dist[link[now][i]] = 0
            d.append(link[now][i])

for i in range(N):
    if i != N - 1:
        print(ans[i], end=" ")
    else:
        print(ans[i])

Buggy python submission (verdict: Runtime Error):
from collections import deque
import numpy as np

N, Q = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(N-1)]
P = [list(map(int, input().split())) for _ in range(Q)]

dist = [-1 for _ in range(N)]
ans = [0 for _ in range(N)]
parent = [[] for _ in range(N)]
dist[0] = 0
d = deque([0])
while d:
    now = d.popleft()
    for i in range(N-1):
        if A[i][0] - 1 == now and dist[A[i][1] - 1] == -1:
            dist[A[i][1] - 1] = now + 1
            d.append(A[i][1] - 1)
            parent[A[i][1] - 1].append(dist[A[i][0] - 1])

        if A[i][1] - 1 == now and dist[A[i][0] - 1] == -1:
            dist[A[i][0] - 1] = now + 1
            d.append(A[i][0] - 1)
            parent[A[i][0] - 1].append(dist[A[i][1] - 1])

arg_dist = np.argsort(np.array(dist))

for i in range(Q):
    ans[P[i][0] - 1] += P[i][1]

for i in range(N):
    for j in range(len(parent[arg_dist[i]])):
        ans[arg_dist[i]] = ans[arg_dist[i]] + ans[parent[arg_dist[i]][j]]

for i in range(N):
    if i != N - 1:
        print(ans[arg_dist[i]], end=" ")
    else:
        print(ans[arg_dist[i]])


Fixed submission:
from collections import deque
import numpy as np

N, Q = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(N-1)]
P = [list(map(int, input().split())) for _ in range(Q)]

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


ans = [0 for _ in range(N)]
for i in range(Q):
  ans[P[i][0] - 1] += P[i][1]

dist = [-1 for _ in range(N)]
dist[0] = 0
d = deque([0])
while d:
    now = d.pop()
    for i in range(len(link[now])):
        if dist[link[now][i]] == -1:
            ans[link[now][i]] += ans[now]
            dist[link[now][i]] = 0
            d.append(link[now][i])

for i in range(N):
    if i != N - 1:
        print(ans[i], end=" ")
    else:
        print(ans[i])