Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p02642 (python)

Fixed submission:
from math import sqrt
from collections import Counter


def main():
    N = int(input())
    A = list(map(int, input().split()))

    ans = 0
    p = [0] * (10**6 + 1)
    c = Counter(A)

    for a in c.keys():
        tmp = a * 2
        while tmp <= 10**6:
            p[tmp] = 1
            tmp += a

    for a in A:
        if p[a] == 0 and c[a] == 1:
            ans += 1

    print(ans)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
from math import sqrt
from collections import Counter


def main():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort()

    ans = 0
    p = [0] * (10**6 + 1)
    c = Counter(A)

    for a in c.keys():
        tmp = a * 2
        if p[tmp] == 1:
            continue
        while tmp <= 10**6:
            p[tmp] = 1
            tmp += a

    for a in A:
        if p[a] == 0 and c[a] == 1:
            ans += 1

    print(ans)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
from math import sqrt
from collections import Counter


def main():
    N = int(input())
    A = list(map(int, input().split()))

    ans = 0
    p = [0] * (10**6 + 10)
    c = Counter(A)

    for a in A:
        tmp = a * 2
        if p[tmp] == 1:
            continue
        while tmp <= 10**6:
            p[tmp] = 1
            tmp += a

    for a in A:
        if p[a] == 0 and c[a] == 1:
            ans += 1

    print(ans)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
from math import sqrt
from collections import Counter


def main():
    N = int(input())
    A = list(map(int, input().split()))

    ans = 0
    p = [0] * (10**6 + 1)
    c = Counter(A)

    for a in A:
        tmp = a * 2
        if p[tmp] == 1:
            continue
        while tmp <= 10**6:
            p[tmp] = 1
            tmp += a

    for a in A:
        if p[a] == 0 and c[a] == 1:
            ans += 1

    print(ans)


if __name__ == "__main__":
    main()
Problem p02684 (python)

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

root = list()


def dfs(A, num, N, memo):
    if memo[A[num]-1] == 1:
        return root.index(A[num])
    else:
        root.append(A[num])
        memo[A[num]-1] = 1
        return dfs(A, A[num]-1, N, memo)


def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))

    memo = [0] * N
    start = dfs(A, 0, N, memo)

    if K <= len(root):
        print(root[K-1])
    else:
        index = (K - start) % (len(root)-start)
        loop = root[start:]
        print(loop[index-1])


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
root = list()


def dfs(A, num, N):
    if A[num] in root:
        return root.index(A[num])
    else:
        root.append(A[num])
        return dfs(A, A[num]-1, N)


def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))

    start = dfs(A, 0, N)

    if K <= len(root):
        print(root[K-1])
    else:
        index = (K - start) % (len(root)-start)
        loop = root[start:]
        print(loop[index-1])


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
root = list()


def dfs(A, num, N):
    if A[num] in root:
        return root.index(A[num])
    else:
        root.append(A[num])
        return dfs(A, A[num]-1, N)


def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))

    start = dfs(A, 0, N)

    if K <= len(root):
        print(root[K-1])
    else:
        index = (K - start) % (len(root)-start)
        print(root[index])


if __name__ == "__main__":
    main()
Problem p02866 (python)

Fixed submission:
if __name__ == "__main__":
    mod = 998244353

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

    if d[0] > 0:
        print('0')
        exit()

    if len(d) == 1:
        print('1')
        exit()

    d.sort()
    d.pop(0)
    if d[0] == 0:
        print('0')
        exit()

    tmp = 1
    ans = 1
    max_num = max(d)
    dic = dict()
    for i in range(n-1):
        if d[i] in dic:
            dic[d[i]] += 1
        else:
            dic[d[i]] = 1

    for i in range(1, max_num+1):
        if i in dic:
            count = dic[i]
        else:
            print('0')
            exit()
        if count == 0:
            print('0')
            exit()
        ans *= tmp ** count
        ans %= mod
        tmp = count

    print(ans)

Buggy submission (verdict: Runtime Error):
if __name__ == "__main__":
    mod = 998244353

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

    if d[0] > 0:
        print('0')
        exit()

    if len(d) == 1:
        print('1')
        exit()

    d.sort()
    d.pop(0)
    if d[0] == 0:
        print('0')
        exit()

    tmp = 1
    ans = 1
    max_num = max(d)
    dic = dict()
    for i in range(n-1):
        if d[i] in dic:
            dic[d[i]] += 1
        else:
            dic[d[i]] = 1

    for i in range(1, max_num+1):
        count = dic[i]
        if count == 0:
            print('0')
            exit()
        ans *= tmp ** count
        ans %= mod
        tmp = count

    print(ans)

Buggy submission (verdict: Runtime Error):
if __name__ == "__main__":
    mod = 998244353

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

    if d[0] > 0:
        print('0')
        exit()

    d.sort()
    d.pop(0)
    if d[0] == 0:
        print('0')
        exit()

    tmp = 1
    ans = 1
    max_num = max(d)
    dic = dict()
    for i in range(n-1):
        if d[i] in dic:
            dic[d[i]] += 1
        else:
            dic[d[i]] = 1

    for i in range(1, max_num+1):
        count = dic[i]
        if count == 0:
            print('0')
            exit()
        ans *= tmp ** count
        ans %= mod
        tmp = count

    print(ans)

Buggy submission (verdict: Runtime Error):
if __name__ == "__main__":
    mod = 998244353

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

    if d[0] > 0:
        print('0')
        exit()

    d.sort()
    d.pop(0)
    if d[0] == 0:
        print('0')
        exit()

    tmp = 1
    ans = 1
    max_num = max(d)
    dic = dict()
    for i in range(n-1):
        if d[i] in dic:
            dic[d[i]] += 1
        else:
            dic[d[i]] = 1

    for i in range(1, max_num+1):
        count = dic[i]
        if count == 0:
            print('0')
            exit()
        ans *= tmp ** count
        ans %= mod
        tmp = count

    print(ans)
Problem p02900 (python)

Fixed submission:
from math import sqrt


def IsPrime(num):
    if num < 2:
        return True
    elif num == 2:
        return True
    elif num % 2 == 0:
        return False

    sqrtNum = sqrt(num)
    for i in range(3, int(sqrtNum) + 1, 2):
        if num % i == 0:
            return False

    return True


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

    # divisors.sort()
    return divisors


if __name__ == "__main__":
    A, B = map(int, input().split())

    A_list = make_divisors(A)
    B_list = make_divisors(B)

    AB_list = set(A_list) & set(B_list)

    ans = list()
    for i in AB_list:
        if IsPrime(i):
            ans.append(i)

    print(len(ans))

Buggy submission (verdict: Runtime Error):
from math import gcd, sqrt


def IsPrime(num):
    if num < 2:
        return True
    elif num == 2:
        return True
    elif num % 2 == 0:
        return False

    sqrtNum = sqrt(num)
    for i in range(3, int(sqrtNum) + 1, 2):
        if num % i == 0:
            return False

    return True


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

    # divisors.sort()
    return divisors


if __name__ == "__main__":
    A, B = map(int, input().split())

    gcd = gcd(A, B)

    div = make_divisors(gcd)

    ans = list()
    for i in div:
        if IsPrime(i):
            ans.append(i)

    print(len(ans))
Problem p02909 (python)

Fixed submission:
if __name__ == "__main__":
    S = input()
    if S == 'Sunny':
        print('Cloudy')
    elif S == 'Cloudy':
        print('Rainy')
    else:
        print('Sunny')

Buggy submission (verdict: Runtime Error):
if __name__ == "__main__":
    N = int(input())
    S = input()

    dp = [[0] * 5005 for i in range(5005)]

    for i in range(N, 0, -1):
        for j in range(N, 0, -1):
            if S[i-1] != S[j-1]:
                dp[i][j] = 0
            else:
                dp[i][j] = dp[i+1][j+1] + 1

    result = 0
    for i in range(N):
        for j in range(N):
            if i >= j:
                continue
            now = min(dp[i][j], j-i)
            result = max([now, result])

    print(result)
Problem p02913 (python)

Fixed submission:
if __name__ == "__main__":
    N = int(input())
    S = input()

    dp = [[0] * 5005 for i in range(5005)]

    for i in range(N, 0, -1):
        for j in range(N, 0, -1):
            if S[i-1] != S[j-1]:
                dp[i][j] = 0
            else:
                dp[i][j] = dp[i+1][j+1] + 1

    result = 0
    for i in range(N):
        for j in range(N):
            if i >= j:
                continue
            now = min(dp[i][j], j-i)
            result = max([now, result])

    print(result)

Buggy submission (verdict: Runtime Error):
if __name__ == "__main__":
    N = int(input())
    S = input()

    dp = [[0] * 5001 for i in range(5001)]

    for i in range(N, 0, -1):
        for j in range(N, 0, -1):
            if S[i-1] != S[j-1]:
                dp[i][j] = 0
            else:
                dp[i][j] = dp[i+1][j+1] + 1

    result = 0
    for i in range(N):
        for j in range(N):
            if i >= j:
                continue
            now = min(dp[i][j], j-i)
            result = max([now, result])

    print(result)
Problem p02947 (python)

Fixed submission:
# -*- coding: utf-8 -*-

N = int(input())
s = [input() for i in range(N)]

output = 0
dic = dict()
for i in range(N):
    tmp = ''.join(sorted(s[i]))
    if tmp not in dic:
        dic[tmp] = 1
    else:
        output += dic[tmp]
        dic[tmp] += 1

print(output)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
from scipy.special import comb

N = int(input())
s = [input() for i in range(N)]

for i in range(N):
    s[i] = ''.join(sorted(s[i]))

unique_s = set(s)

output = 0
for k in unique_s:
     if s.count(k) != 1:
        output += int(comb(s.count(k), 2))

print(output)
Problem p02960 (python)

Fixed submission:
S = input()
N = len(S)
MOD = 10 ** 9 + 7

dp = [0] * 13 
dp[0] = 1
mul = 1
for i in range(0, N):
    next_dp = [0] * 13
    if S[N - i - 1] == '?':
        next_dp = [sum(dp[(j - k * mul) % 13] for k in range(10)) for j in range(13)]
    else:
        for j in range(13):
            k = int(S[N - i - 1])
            next_dp[(j + k * mul) % 13] = dp[j]
    dp = [d % MOD for d in next_dp]
    mul *= 10
    mul %= 13

print(dp[5])

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
# from tqdm import tqdm

S = input()
N = len(S)
MOD = 10 ** 9 + 7

dp = [0] * 13 
next_dp = [0] * 13
dp[0] = 1
mul = 1
for i in range(0,N) # tqdm(range(0, N)):
    if S[N - i - 1] == '?':
        for k in range(10):
            for j in range(13):
                next_dp[(j + k * mul) % 13] += dp[j]
                next_dp[(j + k * mul) % 13] %= MOD
    else:
        for j in range(13):
            k = int(S[N - i - 1])
            next_dp[(j + k * mul) % 13] = dp[j]
            next_dp[(j + k * mul) % 13] %= MOD
    dp = next_dp
    next_dp = [0] * 13
    mul *= 10
    mul %= 13

print(dp[5])
Problem p03059 (python)

Fixed submission:
# -*- coding: utf-8 -*-
A = list(map(int,input().split()))
print(A[2] // A[0] * A[1])

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import fractions
import copy
from functools import reduce

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

N = int(input())
A = list(map(int, str(input()).split()))

gcds = list()
for i in range(0,N):
    B = copy.deepcopy(A)
    B.pop(i)
    tmp = gcd_list(B)
    gcds.append(tmp)

print(max(gcds))
Problem p03161 (python)

Fixed submission:
from numba import njit


@njit
def main(N, K, h):
    dp = [10**10] * (N+1)
    dp[0] = 0

    for i in range(1, N):
        for j in range(min(i, K)):
            dp[i] = min(dp[i], dp[i-j-1]+abs(h[i]-h[i-j-1]))

    return dp[N-1]


if __name__ == "__main__":
    N, K = map(int, input().split())
    h = list(map(int, input().split()))
    print(main(N, K, h))

Buggy submission (verdict: Runtime Error):
from numba import njit


@njit
def main():
    N, K = map(int, input().split())
    h = list(map(int, input().split()))

    dp = [10**6] * (N+1)
    dp[0] = 0

    for i in range(1, N):
        for j in range(min(i, K)):
            dp[i] = min(dp[i], dp[i-j-1]+abs(h[i]-h[i-j-1]))

    print(dp[N-1])


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
from numba import njit


@njit
def main():
    N, K = map(int, input().split())
    h = list(map(int, input().split()))

    dp = [10**10] * (N+1)
    dp[0] = 0

    for i in range(1, N):
        for j in range(min(i, K)):
            dp[i] = min(dp[i], dp[i-j-1]+abs(h[i]-h[i-j-1]))

    print(dp[N-1])


if __name__ == "__main__":
    main()
Problem p03196 (python)

Fixed submission:
from math import sqrt


def Calc_Divisors(num):
    divisors = []
    for i in range(1, int(sqrt(num)) + 1):
        if num % i == 0:
            divisors.append(i)
            if i != num // i:
                divisors.append(num//i)
    divisors.sort()
    return divisors

if __name__ == "__main__":
    N, P = map(int, input().split())

    A = Calc_Divisors(P)

    ans = 1
    for i in A:
        tmp = P % (pow(i, N))
        if N > 40:
            print(ans)
            exit()
        if pow(i, N) > P:
            print(ans)
            exit()
        if tmp == 0:
            ans = i

    print(ans)

Buggy submission (verdict: Runtime Error):
from math import sqrt


def Calc_Divisors(num):
    divisors = []
    for i in range(1, int(sqrt(num)) + 1):
        if num % i == 0:
            divisors.append(i)
            if i != num // i:
                divisors.append(num//i)
    divisors.sort()
    return divisors


if __name__ == "__main__":
    N, P = map(int, input().split())

    A = Calc_Divisors(P)

    ans = 1
    for i in A:
        tmp = P % (i ** N)
        if tmp == 0:
            ans = i

    print(ans)
Problem p03559 (python)

Fixed submission:
from bisect import bisect_right

if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    c = list(map(int, input().split()))
    a = sorted(a)
    b = sorted(b)
    c = sorted(c)

    ans = 0

    # まずあるbを選んだ時のcの組み合わせの数のリストを作る。
    clist = [0] * n
    clist[n-1] = n - bisect_right(c, b[n-1])
    for i in range(n-2, -1, -1):
        clist[i] = n - bisect_right(c, b[i]) + clist[i+1]

    for aa in a:
        b_pos = bisect_right(b, aa)
        if b_pos != n:
            ans += clist[b_pos]

    print(ans)

Buggy submission (verdict: Runtime Error):
from bisect import bisect_right

if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    c = list(map(int, input().split()))
    a = sorted(a)
    b = sorted(b)
    c = sorted(c)

    ans = 0

    # まずあるbを選んだ時のcの組み合わせの数のリストを作る。
    clist = [0] * n
    clist[n-1] = n - bisect_right(c, b[n-1])
    for i in range(n-2, -1, -1):
        clist[i] = n - bisect_right(c, b[i]) + clist[i+1]

    for aa in a:
        b_pos = bisect_right(b, aa)
        ans += clist[b_pos]

    print(ans)
Problem p03627 (python)

Fixed submission:
if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))

    sidelist = dict()

    for aa in a:
        if aa not in sidelist:
            sidelist[aa] = 1
        else:
            sidelist[aa] += 1

    side_a = 0
    side_b = 0

    for s in sidelist:
        if sidelist[s] >= 4 and s > side_a:
            side_a = s
            side_b = s
        elif sidelist[s] >= 2:
            if s > side_a:
                side_b = side_a
                side_a = s
            elif s > side_b:
                side_b = s

    print(side_a*side_b)

Buggy submission (verdict: Runtime Error):
if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))

    sidelist = OrderedDict()

    for aa in a:
        if a.count(aa) >= 2:
            if aa not in sidelist:
                sidelist[aa] = 1
            else:
                sidelist[aa] += 1

    side_a = 0
    side_b = 0

    for s in sidelist:
        if sidelist[s] >= 4 and s > side_a:
            side_a = s
            side_b = s
        elif sidelist[s] >= 2:
            if side_a == 0:
                side_a = s
            elif side_b == 0:
                side_b = s
            elif s > side_a:
                side_b = side_a
                side_a = s
            elif s > side_b:
                side_b = s

    print(side_a*side_b)
Problem p03962 (python)

Fixed submission:
def main():
    c = list(map(int, input().split()))

    c_set = set(c)
    print(len(c_set))


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
3 3 33
Problem p02642 (python)

Fixed submission:
N = int(input())
A = [int(x) for x in input().split()]
m = max(A)+2
cnt = [0] * m
for a in A:
    cnt[a] += 1
    if cnt[a] == 1:
        for i in range(a+a, m, a):
            cnt[i] += 1
ans = 0
for a in A:
    ans += (cnt[a] == 1)
print(ans)

Buggy submission (verdict: Runtime Error):
from collections import deque
N = int(input())
A = sorted([int(x) for x in input().split()])
D = deque(A)
ans = 0
if A[0] == A[1]: 
    ans -= 1
while len(D):
    a = D.popleft()
    ans += 1
    D = deque(filter(lambda x: x%a != 0, D))
print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
A = sorted([int(x) for x in input().split()], reverse=True)
ans = 0
for i, ai in enumerate(A):
    f = 1
    for aj in A[i+1:]:
        if ai % aj == 0:
            f = 0
            break
    ans += f
ans -= (A[-1] == A[-2])
print(ans)
Problem p02684 (python)

Fixed submission:
N, K = [int(x) for x in input().split()]
A = [int(x)-1 for x in input().split()]
past = 0
history = [0]
s = set()
while True:
    na = A[past]
    if na in s: 
        break
    s.add(na)
    history.append(na)
    past = na
s = history.index(na)
e = len(history)
loop = e - s
if K <= s: # ループ前終了
    print(history[K]+1)
else:
    k = K - s
    print(history[s+(k%loop)]+1)

Buggy submission (verdict: Runtime Error):
N, K = [int(x) for x in input().split()]
A = [int(x)-1 for x in input().split()]
past = 0
history = [0]
while True:
    na = A[past]
    if na in history: 
        break
    history.append(na)
    past = na
s = history.index(na)
e = len(history) - s
loop = e - s
if K <= s: # ループ前終了
    print(history[K]+1)
else:
    k = K - s
    print(history[s+(k%loop)]+1)
Problem p02691 (python)

Fixed submission:
N = int(input())
A = [int(x) for x in input().split()]
L = [0] * 2 * N
R = [0] * 2 * N
for i in range(N):
    l = i + A[i]
    if l < N: L[l] += 1
    r = i - A[i]
    if r > 0: R[r] += 1
ans = 0 
for i in range(N):
    ans += L[i] * R[i]
print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
A = [int(x) for x in input().split()]
L = [0] * 2 * N
R = [0] * 2 * N
for i in range(N):
    l = i + A[i]
    if l < N: L[l] += 1
    r = i - A[i]
    R[r] += 1
ans = 0 
for i in range(N):
    ans += L[i] * R[i]
print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
A = [int(x) for x in input().split()]
L = [0] * 2 * N
R = [0] * 2 * N
for i in range(N):
    l = i + A[i]
    L[l] += 1
    r = i - A[i]
    R[r] += 1
ans = 0 
for i in range(N):
    ans += L[i] * R[i]
print(ans)
Problem p02795 (python)

Fixed submission:
H, W, N = [int(input()) for _ in range(3)]
print(min((N+H-1)//H, (N+W-1)//W))

Buggy submission (verdict: Runtime Error):
N = int(input())
H, W, N = [int(input()) for _ in range(N)]
print(min((N+H-1)//H, (W+W-1)//W))
Problem p02866 (python)

Fixed submission:
MOD = 998244353 
N = int(input())
D = [int(x) for x in input().split()]
zero = 0 
for d in D:
    zero += (d == 0)
if (D[0] != 0 or zero != 1):
    print(0)
else:
    lst = [0] * (N)
    for d in D:
        lst[d] += 1
    first0 = False
    for i in range(N):
        if (lst[i] != 0 and first0):
            print(0)
            exit()
        elif (lst[i] ==0):
            first0 = True
    ans = 1
    for i in range(N-1):
        ans *= lst[i] ** lst[i+1]
        ans %= MOD
    print(ans)

Buggy submission (verdict: Runtime Error):
MOD = 998244353 
N = int(input())
D = [int(x) for x in input().split()]
lst = [0] * (N)
for d in D:
    lst[d] += 1
if (lst[0] == 1):
    pos0 = lst.index(0)
    if (len(set(lst[pos0+1:]))>1):
        print(0)
    else:
        ans = 1
        for i in range(N-1):
            ans *= pow(lst[i], lst[i+1], MOD)
        print(ans)
else:
    print(0)
Problem p02900 (python)

Fixed submission:
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

from fractions import gcd
import math
A, B = [int(x) for x in input().split()]
N = gcd(A, B)
a = prime_factorize(N)
print(len(set(a))+1)

Buggy submission (verdict: Runtime Error):
from fractions import gcd
A, B = [int(x) for x in input().split()]
N = gcd(A, B)
p_list = set({})
for i in range(1, int(math.sqrt(N))+1):
    if (N != (N//i * i)):
        continue
    if (i == 1):
        p_list.add(i)
    else:
        is_prime = True
        for p in p_list:
            if (p != 1 and i == (i//p * p)):
                is_prime = False
        if (is_prime):
            p_list.add(i)
        j = N // i
        is_prime = True
        for p in p_list:
            if (p != 1 and j == (j//p * p)):
                is_prime = False
        if (is_prime):
            p_list.add(j)
print(len(p_list))
Problem p02936 (python)

Fixed submission:
N, Q = [int(x) for x in input().split()]
to = [[] for _ in range(N+1)]
for _ in range(N-1):
    a, b = [int(x)-1 for x in input().split()]
    to[b].append(a)
    to[a].append(b)
ans = [0] * N
for _ in range(Q):
    p, x = [int(x) for x in input().split()]
    p -= 1
    ans[p] += x

from collections import deque
q = deque()
q.append(0)
visited = [0] * N

while(q):
    v = q.pop()
    visited[v] = 1    
    for u in to[v]:
        if visited[u] == 1: continue
        ans[u] += ans[v]
        q.append(u)
print(' '.join([str(i) for i in ans]))

Buggy submission (verdict: Runtime Error):
N, Q = [int(x) for x in input().split()]
ab_list = [[int(x) for x in input().split()] for _ in range(N-1)]
px_list = [[int(x) for x in input().split()] for _ in range(Q)]
to = [[] for _ in range(N+1)]
for a, b in ab_list:
    a -= 1
    b -= 1
    to[b].append(a)
    to[a].append(b)
ans = [0] * N
for p, x in px_list:
    ans[p-1] += x

def dfs(v, p=-1):
    for u in to[v]:
        if (u == p): continue
        ans[u] += ans[v]
        dfs(u, v)        
dfs(0)
print(' '.join([str(i) for i in ans]))

Buggy submission (verdict: Runtime Error):
N, Q = [int(x) for x in input().split()]
ab_list = [[int(x) for x in input().split()] for _ in range(N-1)]
px_list = [[int(x) for x in input().split()] for _ in range(Q)]
to = [[] for _ in range(N+1)]
for a, b in ab_list:
    to[a].append(b)
subtree = [[i] for i in range(N+1)]
def dfs(r, i):
    if (len(to[i])):
        for j in to[i]:
            subtree[r].append(j)
            dfs(r, j)
    else:
        return
for i in range(1, N):
    dfs(i, i)

ans = [0 for _ in range(N)]
for p, x in px_list:
    for u in subtree[p]:
        ans[u-1] += x
print(' '.join([str(i) for i in ans]))