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

Fixed submission:
N,start = map(int,input().split())
D = list(map(int,input().split()))
input()
Q = list(map(int,input().split()))

Q = [(k-1,i) for i,k in enumerate(Q)]
Q.sort()

P = [start]
for d in D:
  a = P[-1]
  b = abs(a-d)
  P.append(min(a,b))

result = [None]*len(Q)

x = 1

for i,d in zip(reversed(range(len(D))), reversed(D)):
  if Q[-1][0] == i:
    result[Q[-1][1]] = P[i] >= x
    Q.pop()
    if not Q:
      break

  if abs(x-d) < x:
    x += d

for r in result:
  print('YES' if r else 'NO')

Buggy submission (verdict: Runtime Error):
x = 1

for i,d in zip(reversed(range(len(D))), reversed(D)):
  if Q[-1][0] == i:
    result[Q[-1][1]] = P[i] >= x
    Q.pop()
    if not Q:
      break

  if abs(x-d) < x:
    x += d

for r in result:
  print('YES' if r else 'NO')
Problem p02585 (python)

Fixed submission:
N, K = map(int, input().split())
P = [int(i) - 1 for i in input().split()]
Q = [int(i) for i in input().split()]
ans = - float('inf')
for i in range(N):
    now = i
    cnt = 0
    span = 0
    while True:
        span += 1
        cnt += Q[now]
        now = P[now]
        if now == i:
            break
    point = 0
    now = i
    for j in range(1, span + 1):
        point += Q[now]
        now = P[now]       
        if j > K:
            break
        value = point + (max(cnt, 0) * ((K - j) // span))
        ans = max(ans, value)
print(ans)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
P = [int(i) - 1 for i in input().split()]
Q = [int(i) for i in input().split()]
ans = - 10000000000
for i in range(N):
    tmp = []
    now = i
    cnt = 0
    for j in range(1, N + 1):
        now = P[now]
        cnt += Q[now]
        tmp.append(cnt)
        if now == i:
            break
    ans = max(ans, max(tmp[:K]))
    else:
        v = (tmp[-1] * (K // j))
        if K % j > 0:
            v += max(tmp[:(K % j)])
        ans = max(ans, v)
print(ans)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
P = [int(i) - 1 for i in input().split()]
Q = [int(i) for i in input().split()]
ans = - float('inf')
for i in range(N):
    tmp = []
    now = i
    cnt = 0
    for j in range(1, N + 1):
        now = P[now]
        cnt += Q[now]
        tmp.append(cnt)
        if now == i:
            break
    if tmp[-1] < 0:
        ans = max(ans, max(tmp))
    else:
        v = (tmp[-1] * (K // j)) + max(tmp[:K % j])
        ans = max(ans, v)
print(ans)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
P = [int(i) - 1 for i in input().split()]
Q = [int(i) for i in input().split()]
ans = - float('inf')
for i in range(N):
    tmp = []
    now = i
    cnt = 0
    for j in range(1, N + 1):
        now = P[now]
        cnt += Q[now]
        tmp.append(cnt)
        if now == i:
            break
    if tmp[-1] < 0:
        ans = max(ans, max(tmp))
    else:
        v = (tmp[-1] * (K // j)) + max(tmp[:K % j])
        ans = max(ans, v)
print(ans)
Problem p02616 (python)

Fixed submission:
def solve():
    can_positive = False
    if len(P) > 0:
        if k < n:
            can_positive = True
        else:
            can_positive = len(M) % 2 == 0
    else:
        can_positive = k % 2 == 0
    if not can_positive:
        return sorted(P + M, key=lambda x: abs(x))[:k]
    P.sort()
    M.sort(reverse=True)
    a = [P.pop()] if k % 2 else [1]
    while len(P) >= 2:
        a.append(P.pop() * P.pop())
    while len(M) >= 2:
        a.append(M.pop() * M.pop())
    return a[:1] + sorted(a[1:], reverse=True)[:(k - k%2) // 2]

n, k = map(int, input().split())
P, M = [], []
for a in map(int, input().split()):
    if a < 0:
        M.append(a)
    else:
        P.append(a)
ans, mod = 1, 10 ** 9 + 7
for a in solve():
    ans *= a
    ans %= mod
print(ans)

Buggy submission (verdict: Runtime Error):
mod = 10 ** 9 + 7
N, K = map(int, input().split())
A = [int(i) for i in input().split()]
A_minus = []
A_plus = []
for i in A:
    if i < 0:
        A_minus.append(i)
    elif i >= 0:
        A_plus.append(i)

len_plus = len(A_plus)
len_minus = len(A_minus)
plus = True
if (K > len_plus):
    if K % 2 == 1:
        if len_plus > 0:
                len_plus -= 1
                K -= 1
    K -= (len_plus // 2) * 2
    if K < len_minus:
        plus = False
ans = 1
if plus:
    A_plus.sort(reverse=True)
    A_minus.sort()
    for i in range(K):
        if ans < 0:
            ans *= A_minus.pop()
            continue
        if i < K - 1:
            if (len(A_plus) > 0) & (len(A_minus) > 0):
                if A_plus[0] < abs(A_minus[0]):
                    ans *= A_minus.pop()
                    continue
            if len(A_plus) > 0:
                ans *= A_plus.pop()
else:
    A_plus.sort()
    A_minus.sort(reverse=True)
    for i in range(K):
        if ans < 0:
            ans *= A_minus.pop()
            continue
        if i < K - 1:
            if (len(A_plus) > 0) & (len(A_minus) > 0):
                if A_plus[0] < abs(A_minus[0]):
                    ans *= A_minus.pop()
                    continue
            if len(A_plus) > 0:
                ans *= A_plus.pop()
print(ans % mod)
Problem p02618 (python)

Fixed submission:
D = int(input())
c_list = [int(i) for i in input().split()]
s_list = []
t_list = []
for _ in range(D):
    s = [int(i) for i in input().split()]
    s_list.append(s)
happy = 0
recent = [0 for _ in range(26)]
for _ in range(D):
    print(1)

Buggy submission (verdict: Runtime Error):
D = int(input())
c_list = [int(i) for i in input().split()]
s_list = []
t_list = []
for _ in range(D):
    s = [int(i) for i in input().split()]
    s_list.append(s)
for _ in range(D):
    t_list.append(int(input()))

t_list[d] = q
happy = 0
recent = [0 for _ in range(26)]
for _ in range(D):
    print(1)
Problem p02642 (python)

Fixed submission:
n = int(input())
a = [int(i) for i in input().split()]
table = [True for _ in range(2 * (10 ** 6))]
a.sort()
ans = 0
for i in range(n):
    if table[a[i]]:
        for j in range(a[i], 2 * (10 ** 6), a[i]):
            table[j] = False
        if i + 1 < n:
            if a[i + 1] != a[i]:
                ans += 1
        elif i + 1 == n:
            ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())
a = [int(i) for i in input().split()]
table = [True for _ in range(10 ** 6)]
a.sort()
ans = 0
for i in range(n):
    if table[a[i]]:
        for j in range(a[i], 10 ** 6, a[i]):
            table[j] = False
        if i + 1 < n - 1:
            if a[i + 1] != a[i]:
                ans += 1
        elif i + 1 == n - 1:
            ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())
a = [int(i) for i in input().split()]
table = [True for _ in range(10 ** 6)]
a.sort()
ans = 0
for i in range(n):
    if table[a[i]]:
        for j in range(a[i], 10 ** 6, a[i]):
            table[j] = False
        if i + 1 < n - 1:
            if a[i + 1] != a[i]:
                ans += 1
        elif i + 1 == n - 1:
            ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())
a = [int(i) for i in input().split()]
table = [True for _ in range(10 ** 6)]
a.sort()
ans = 0
for i in range(n):
    if table[a[i]]:
        for j in range(a[i], 10 ** 6, a[i]):
            table[j] = False
        if i + 1 < n:
            if a[i + 1] != a[i]:
                ans += 1
        elif i + 1 == n:
            ans += 1
print(ans)
Problem p02664 (python)

Fixed submission:
s = input()
ans = ''
for i in range(len(s)):
  s_i = s[i]
  if s_i == '?':
      ans += 'D'
      continue
  ans += s_i
print(ans)

Buggy submission (verdict: Runtime Error):
s = input()
ans = ''
for i in range(len(s)):
  s_i = s[i]
  if s_i = '?':
    if i < len(s):
      if s[i + 1] == 'D':
        ans += 'P'
        continue
    else:
      ans += 'D'
      continue
  ans += s_i
print(ans)
Problem p02684 (python)

Fixed submission:
n, k = map(int, input().split())
a = [int(i) - 1 for i in input().split()]
visit = {0}
tmp = 0
a_list = [0]
ans = True
for i in range(k):
    tmp = a[tmp]
    if tmp in visit:
        left = a_list.index(tmp)
        ans = False
        print(a_list[left + (k - left) % (i + 1 - left)] + 1)
        break
    else:
        visit.add(tmp)
        a_list.append(tmp)
if ans:
    print(tmp + 1)

Buggy submission (verdict: Runtime Error):
def calc(i):
    ans_list = [i]
    ans_set = set([i])
    count = 1
    i = a[i]
    while (i not in ans_set) & (count <= k):
        ans_list.append(i)
        ans_set.add(i)
        count += 1
        i = a[i]
    index = ans_list.index(i)
    ans_list.append(i)
    return count, ans_list

n, k = map(int, input().split())
a = [int(i) - 1 for i in input().split()]

count, ans_list = calc(0)
if count <= n:
    index = ans_list.index(ans_list[-1])
    k = k - index
    ans_list = ans_list[index + 1:]
    print(ans_list[k % len(ans_list) - 1] + 1)
else:
    print(ans_list[-1] + 1)

Buggy submission (verdict: Runtime Error):
def calc(i, k):
    ans_list = [i]
    ans_set = set([i])
    count = 1
    i = a[i]
    while (i not in ans_set) & (count < k):
        ans_list.append(i)
        ans_set.add(i)
        count += 1
        i = a[i]
    index = ans_list.index(i)
    ans_list.append(i)
    return count, ans_list

n, k = map(int, input().split())
a = [int(i) - 1 for i in input().split()]

count, ans_list = calc(0, k)
if count < k:
    index = ans_list.index(ans_list[-1])
    k = k - index
    ans_list = ans_list[index + 1:]
    print(ans_list[k % len(ans_list) - 1] + 1)
else:
    print(ans_list[-1] + 1)

Buggy submission (verdict: Runtime Error):
def calc(i, k):
    ans_list = [i]
    ans_set = set([i])
    count = 1
    i = a[i]
    while (i not in ans_set) & (count < k):
        ans_list.append(i)
        ans_set.add(i)
        count += 1
        i = a[i]
    index = ans_list.index(i)
    ans_list.append(i)
    return count, ans_list

n, k = map(int, input().split())
a = [int(i) - 1 for i in input().split()]

count, ans_list = calc(0, k)
if count < k:
    index = ans_list.index(ans_list[-1])
    k = k - index
    ans_list = ans_list[index + 1:]
    print(ans_list[k % len(ans_list) - 1] + 1)
else:
    print(ans_list[k] + 1)

Buggy submission (verdict: Runtime Error):
def calc(i, k):
    ans_list = [i]
    count = 1
    i = a[i]
    while (i not in ans_list) & (count < k):
        ans_list.append(i)
        count += 1
        i = a[i]
    index = ans_list.index(i)
    ans_list.append(i)
    return count, ans_list

n, k = map(int, input().split())
a = [int(i) - 1 for i in input().split()]

count, ans_list = calc(0, k)
if count < k:
    index = ans_list.index(ans_list[-1])
    k = k - index
    ans_list = ans_list[index + 1:]
    print(ans_list[k % len(ans_list) - 1] + 1)
else:
    print(ans_list[k] + 1)
Problem p02696 (python)

Fixed submission:
import math
a, b, n = map(int, input().split())
print(math.floor(a * min((b - 1), n) / b))

Buggy submission (verdict: Runtime Error):
a, b, n = map(int, input().split())
print(math.floor(a * min((b - 1), n) / b))
Problem p02702 (python)

Fixed submission:
s = input()[::-1]
p = 0
dp = [0] * 2019
dp[0] = 1
d = 1
for i in s:    
    p = (int(i) * d + (p)) % 2019
    dp[p] += 1
    d *= 10
    d = d % 2019
ans = 0
for i in dp:
    ans += i * (i - 1) // 2
print(ans)

Buggy submission (verdict: Runtime Error):
s = input()[::-1]
p = 0
dp = [0] * 2019
dp[0] = 1
for i in s:
    p = (p + (int(s) * (10 ** (i)))) % 2019
    dp[p] += 1
ans = 0
for i in dp:
    ans += i * (i - 1) // 2
print(ans)
Problem p02722 (python)

Fixed submission:
import math
import copy
def prime_factorize(n):
    a = [n]
    for i in range(2, math.floor(n ** 0.5) + 1):
        if n % i == 0:
            a.append(i)
            if n // i != i:
                a.append(n // i)
    return a

N = int(input())
prime_n = prime_factorize(N)
prime_n_1 = prime_factorize(N - 1)

ans = 0
for i in prime_n:
    n = copy.deepcopy(N)
    while (n % i) == 0:
            n = n // i
    if (n % i) == 1:
        ans += 1
ans += len(prime_n_1)
if N > 2:
    print(ans)
else:
    print(1)

Buggy submission (verdict: Runtime Error):
import math
def prime_factorize(n):
    a = [n]
    for i in range(2, math.floor(n ** 0.5) + 1):
        if n % i == 0:
            a.append(i)
            if n // i != i:
                a.append(n // i)
    return a

N = int(input())
prime_n = prime_factorize(N)
prime_n_1 = prime_factorize(N - 1)

ans = 0
for i in prime_n:
    n = copy.deepcopy(N)
    while (n % i) == 0:
            n = n // i
    if (n % i) == 1:
        ans += 1
ans += len(prime_n_1)
print(ans)

Buggy submission (verdict: Runtime Error):
def prime_factorize(n):
    a = [n]
    for i in range(2, math.floor(n ** 0.5) + 1):
        if n % i == 0:
            a.append(i)
            if n // i != i:
                a.append(n // i)
    return a

N = int(input())
prime_n = prime_factorize(N)
prime_n_1 = prime_factorize(N - 1)

ans = 0
for i in prime_n:
    n = copy.deepcopy(N)
    while (n % i) == 0:
            n = n // i
    if (n % i) == 1:
        ans += 1
ans += len(prime_n_1)
print(ans)
Problem p02756 (python)

Fixed submission:
import collections
s = input()
q = int(input())
back = 1
t = collections.deque()
for ss in s:
    t.append(ss)
for i in range(q):
    query = input().split()
    if query[0] == '1':
        back *= -1
    else:
        if query[1] == '1':
            if back == 1:
                t.appendleft(query[2])
            else:
                t.append(query[2])
        else:
            if back == 1:
                t.append(query[2])
            else:
                t.appendleft(query[2])
ans = ''
if back == -1:
    for i in range(len(t)):
        ans += t.pop()
else:
    for i in range(len(t)):
        ans += t.popleft()
print(ans)

Buggy submission (verdict: Runtime Error):
s = input()
q = int(input())
back = 1
for i in range(q):
    query = input().split()
    if query[0] == '1':
        back *= -1
    else:
        if query[1] == '1':
            if back == 1:
                s = query[2] + s
            else:
                s = s + (query[2]
        else:
            if back == 1:
                s = s + query[2]
            else:
                s = query[2] + s
if back == -1:
    s = s[-1::-1]
print(s)
Problem p02767 (python)

Fixed submission:
def calc(a):
    y = 0
    for xx in x:
        y += (a - xx) ** 2
    return y


n = int(input())
x = [int(i) for i in input().split()]
left = min(x)
right = max(x)
while (right - left > 1):
    l = calc(left)
    r = calc(right)
    if (l < r):
        right = (right + left) // 2
    else:
        left = (right + left) // 2
print(min(calc(left), calc(right)))

Buggy submission (verdict: Runtime Error):
def calc(a):
    y = 0
    for xx in x:
        y += (a - xx) ** 2
    return y


n = int(input())
x = [int(i) for i in input().split()]
left = min(x)
right = max(x)
while (right - left > 1):
    l = calc(left)
    r = calc(right)
    if (l < r):
        right = (right + left) // 2
    else:
        left = (right + left) // 2
print(min(r, l))

Buggy submission (verdict: Runtime Error):
def calc(a):
    y = 0
    for xx in x:
        y += (a - xx) ** 2
    return y


n = int(input())
x = [int(i) for i in input().split()]
left = min(x)
right = max(x)
while (right - left > 1):
    l = calc(left)
    r = calc(right)
    if (l < r):
        right = (right + left) // 2
    else:
        left = (right + left) // 2
print(r)
Problem p02780 (python)

Fixed submission:
from itertools import accumulate
n, k = map(int, input().split())
p = list(map(int, input().split()))
if (n != k):
    pp = list(accumulate(p))
    ppp = [pp[i + k] - pp[i] + k for i in range(len(p) - k)]
    print((max(ppp)) / 2)
else:
    print((sum(p) + n) / 2)

Buggy submission (verdict: Runtime Error):
from itertools import accumulate
n, k = map(int, input().split())
p = list(map(int, input().split()))
pp = list(accumulate(p))
ppp = [pp[i + k] - pp[i] + k for i in range(len(p) - k)]
print((max(ppp)) / 2)
Problem p02788 (python)

Fixed submission:
import math
from bisect import bisect_right

n, d, a = map(int, input().split())
x_list = []
max_x = 0
for _ in range(n):
    x, h = map(int, input().split())
    x -= 1
    x_list.append([x, h])
    max_x = max(max_x, x)
x_list.sort()
xx = [x[0] for x in x_list]
hh = [x[1] for x in x_list]
ans = 0
accum = [0 for _ in range(n)]
for index, [x, h] in enumerate(x_list):
    if index != 0:
        accum[index] += accum[index - 1]
    cnt = max(math.ceil((hh[index] - accum[index]) / a),  0)
    ans += cnt
    index_right = bisect_right(xx, xx[index] + (2 * d))
    accum[index] += cnt * a
    if index_right < n:
        accum[index_right] -= cnt * a
print(ans)

Buggy submission (verdict: Runtime Error):
import math
from bisect import bisect_left

n, d, a = map(int, input().split())
x_list = []
max_x = 0
for _ in range(n):
    x, h = map(int, input().split())
    x -= 1
    x_list.append([x, h])
    max_x = max(max_x, x)
x_list.sort()
xx = [x[0] for x in x_list]
ans = 0
for index_left, [x, h] in enumerate(c):
    if h > 0:
        ans += math.ceil(h / a)
        index_right = bisect_left(xx, x + (d * 2))
        for i in range(index_left, min(index_right + 1, n)):
            x_list[i][1] -= math.ceil(h / a) * a
print(ans)
Problem p02846 (python)

Fixed submission:
t1, t2 = map(int, input().split())
a1, a2 = map(int, input().split())
b1, b2 = map(int, input().split())
first = a1 * t1 - b1 * t1
second = (a2 * t2 - b2 * t2)
last = second + first
if first * last > 0:
    ans = 0
elif (abs(last) == 0):
    ans = 'infinity'
else:
    s = abs(first) // abs(last)
    t = abs(first) % abs(last)
    ans = 2 * s
    if t != 0:
        ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
ans = ''
if first * last > 0:
    ans = 0
elif (abs(last) == 0):
    ans = 'infinity'
elif abs(first) * 2 < abs(second):
    ans = 1
else:
    if abs(first) % abs(last) == 0:
        ans = (abs(first) // abs(last)) * 2
    elif abs(first) * 2 == abs(last):
        ans = 2
    else:
        ans = math.floor(2 * abs(first) / abs(last))

Buggy submission (verdict: Runtime Error):
t1, t2 = map(int, input().split())
a1, a2 = map(int, input().split())
b1, b2 = map(int, input().split())
first = a1 * t1 - b1 * t1
second = (a2 * t2 - b2 * t2)
last = second + first
if first * last > 0:
    ans = 0
elif (abs(last) == 0):
    ans = 'infinity'
else:
    ans = math.ceil(abs(first) / abs(last))
print(ans)
Problem p02882 (python)

Fixed submission:
import math
a, b, x = map(int, input().split())
if (a ** 2 * b - x) > 0:
    t = (a ** 3) / (2 * (a ** 2 * b - x))
    if t >= (a / b):
        print(90 - (math.atan(t) / (math.pi)) * 180)
    else:
        print((math.atan(a * b * b / (2 * x)) / math.pi) * 180)
else:
    print(0)

Buggy submission (verdict: Runtime Error):
import math
a, b, x = map(int, input().split())
t = (a ** 3) / (2 * (a ** 2 * b - x))
if t >= (a / b):
    print(90 - (math.atan(t) / (math.pi)) * 180)
else:
    print(90 - (math.atan(x / (a * b * b)) / math.pi) * 180)
Problem p02900 (python)

Fixed submission:
import fractions

def return_prime(aa):
    a_list = [1]
    for i in range(2, int(aa ** 0.5) + 2):
        while aa % i == 0:
            a_list.append(i)
            aa = aa // i
    if aa != 1:
        a_list.append(aa)
    return a_list

a, b = map(int, input().split())
a_list = return_prime(fractions.gcd(a, b))
print(len(set(a_list)))

Buggy submission (verdict: Runtime Error):
import fractions

def return_prime(aa):
    a_list = [1]
    if aa % 2 == 0:
        a_list.append(2)
    prime_list = list(range(3, aa + 1, 2))
    for d in prime_list:
        prime_list = [x for x in prime_list if (x == d or x % d != 0)]
    for p in prime_list:
        if aa % p == 0:
            a_list.append(p)
    return a_list

a, b = map(int, input().split())
a_list = return_prime(fractions.gcd(a, b))
print(len(set(a_list)))
Problem p02913 (python)

Fixed submission:
n = int(input())
s = input()
i = 0
j = 0
r = 0
while i < n:
    if s[i:j] in s[j:]:
        r = max(r, j - i)
        j += 1
    else:
        i += 1
print(r)

Buggy submission (verdict: Runtime Error):
n = int(input())
s = input()

ans = 0
for j in range(ans + 1, n):
    for i in range(j + ans):
        if (s[i:i+ans+1] == s[j:j+ans+1]):
            while (i + ans < n) & (i + ans < j):
                if s[i+ans] == s[j+ans]:
                    ans += 1
                else:
                    break
            else:
                break
print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())
s = input()

ans = 0
for j in range(n):
    for i in range(j + ans):
        if (s[i:i+ans+1] == s[j:j+ans+1]):
            while (i + ans < n) & (i + ans < j):
                if s[i+ans] == s[j+ans]:
                    ans += 1
            else:
                break
print(ans)
Problem p02936 (python)

Fixed submission:
n, q = map(int, input().split())
graph = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)
point_list = [0] * n
for _ in range(q):
    p, x = map(int, input().split())
    p -= 1
    point_list[p] += x

from collections import deque
d = deque()
d.append(0)
visited = set([])

while d:
    x = d.popleft()
    for child in graph[x]:
        if child not in visited:
            point_list[child] += point_list[x]
            d.append(child)
        visited.add(x)
print(*point_list)

Buggy submission (verdict: Runtime Error):
def count(x):
    if len(child_list[x]) > 0:
        for index in child_list[x]:
            total_list[index] += total_list[x]
            count(index)

n, q = map(int, input().split())
child_list = [[] for _ in range(n)]
position_list = [10 * 6] * n
position_list[0] = 1
input_list = []
for _ in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    if a < b:
        a, b = b, a
    input_list.append([b, a])
input_list.sort()
for a, b in input_list:
    if position_list[a] < position_list[b]:
        a, b = b, a
    position_list[a] = position_list[b] + 1
    child_list[b].append(a)
total_list = [0] * n
for _ in range(q):
    p, x = map(int, input().split())
    total_list[p - 1] += x

count(0)
print(*total_list)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
n, q = map(int, input().split())
child_dict = defaultdict(set([]))
value_dict = defaultdict(lambda: 0)
for _ in range(n - 1):
    a, b = map(int, input().split())
    child_dict[a].add(b)
import copy
for _ in range(q):
    c, d = map(int, input().split())
    value_dict[c] += d
    child = child_dict[c]
    while len(child) > 0:
        k = []
        for i in child:
            value_dict[i] += d
            k.extend(child_dict[i])
        child = copy.deepcopy(k)
ans = [value_dict[i] for i in range(1 ,n+1)]
print(*ans)
Problem p02960 (python)

Fixed submission:
mod = 10 ** 9 + 7

s = input()
dp = [0] * 13
dp[0] = 1
for i in range(len(s)):
    dp2 = [0] * 13
    for j in range(10):
        if (s[i] == '?') | (s[i] == str(j)):
            for k in range(13):
                dp2[(10 * k + j) % 13] += dp[k]
                dp2[(10 * k + j) % 13] %= mod
    dp = dp2
print(dp[5])

Buggy submission (verdict: Runtime Error):
s = input()
count = 0
for i in range(n - 1):
    if s[i:i+2] == "??":
        count += 1
ans = count * 7
print(ans)