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


def main():
    N, Q = map(int, input().split())
    link = [[] for _ in range(N)]
    a = []
    b = []
    a_append = a.append
    b_append = b.append
    for i in range(N-1):
        x, y = map(int, input().split())
        a_append(x)
        b_append(y)
    cost = [0] * N
    for _ in range(Q):
        p, x = map(int, input().split())
        cost[p-1] += x

    for i in range(N-1):
        ai = a[i]-1
        bi = b[i]-1
        ci = cost[i]
        link[ai].append((bi, ci))
        link[bi].append((ai, ci))

    def dfs(v, c, p):
        for nv in link[v]:
            nv0 = nv[0]
            if nv0 != p:
                cost[nv0] += c
                dfs(nv0, cost[nv0], v)

    dfs(0, cost[0], -1)

    print(*cost)


if __name__ == '__main__':
    main()


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


def main():
    N, Q = map(int, input().split())
    link = [[] for _ in range(N)]
    a = []
    b = []
    a_append = a.append
    b_append = b.append
    for i in range(N-1):
        x, y = map(int, input().split())
        a_append(x)
        b_append(y)
    cost = [0] * N
    for _ in range(Q):
        p, x = map(int, input().split())
        cost[p-1] += x

    for i in range(N-1):
        ai = a[i]-1
        bi = b[i]-1
        ci = cost[i]
        link[ai].append((bi, ci))
        link[bi].append((ai, ci))

    def dfs(v, c, p):
        for nv in link[v]:
            nv0 = nv[0]
            if nv0 != p:
                cost[nv0] += c
                dfs(nv0, cost[nv0], v)

    dfs(0, cost[0], -1)

    print(*cost)


if __name__ == '__main__':
    main()

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


def main():
    N, Q = map(int, input().split())
    link = [[] for _ in range(N)]
    a = []
    b = []
    a_append = a.append
    b_append = b.append
    for i in range(N-1):
        x, y = map(int, input().split())
        a_append(x)
        b_append(y)
    cost = [0] * N
    for _ in range(Q):
        p, x = map(int, input().split())
        cost[p-1] += x

    for i in range(N-1):
        link[a[i]-1].append([b[i]-1, cost[i]])
        link[b[i]-1].append([a[i]-1, cost[i]])

    def dfs(v, c, p):
        for nv in link[v]:
            nv0 = nv[0]
            if nv0 != p:
                cost[nv0] += c
                dfs(nv0, cost[nv0], v)

    dfs(0, cost[0], -1)

    print(*cost)


if __name__ == '__main__':
    main()


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


def main():
    N, Q = map(int, input().split())
    link = [[] for _ in range(N)]
    a = []
    b = []
    a_append = a.append
    b_append = b.append
    for i in range(N-1):
        x, y = map(int, input().split())
        a_append(x)
        b_append(y)
    cost = [0] * N
    for _ in range(Q):
        p, x = map(int, input().split())
        cost[p-1] += x

    for i in range(N-1):
        ai = a[i]-1
        bi = b[i]-1
        ci = cost[i]
        link[ai].append((bi, ci))
        link[bi].append((ai, ci))

    def dfs(v, c, p):
        for nv in link[v]:
            nv0 = nv[0]
            if nv0 != p:
                cost[nv0] += c
                dfs(nv0, cost[nv0], v)

    dfs(0, cost[0], -1)

    print(*cost)


if __name__ == '__main__':
    main()