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

Fixed submission:
h, w = (int(i) for i in input().split()) 
s = []

for i in range(h):
  tmp = input()
  s.append(tmp)

#左にいくつあるか
l = [[0 for i in range(w)] for j in range(h)]

for i in range(h):
  for j in range(w):
    if s[i][j] == "#":
      l[i][j] = 0
    else:
      if j == 0:
        l[i][j] = 1
        continue
      l[i][j] = l[i][j-1] + 1
#print(l)
      
#上
d = [[0 for i in range(w)] for j in range(h)]

for j in range(w):
  for i in range(h):
    if s[i][j] == "#":
      d[i][j] = 0
    else:
      if i == 0:
        d[i][j] = 1
        continue
      d[i][j] = d[i-1][j] + 1  
#print(d)      

#右にいくつあるか
r = [[0 for i in range(w)] for j in range(h)]

for i in range(h):
  for j in range(w)[::-1]:
#    print(i,j)
    if s[i][j] == "#":
      r[i][j] = 0
    else:
      if j == w-1:
#        print("aa")
        r[i][j] = 1
        continue
      r[i][j] = r[i][j+1] + 1
      
#print(r)

#下
u = [[0 for i in range(w)] for j in range(h)]

for j in range(w):
  for i in range(h)[::-1]:
    if s[i][j] == "#":
      u[i][j] = 0
    else:
      if i == h-1:
        u[i][j] = 1
        continue
      u[i][j] = u[i+1][j] + 1

#print(u)


#足し合わせ
for i in range(h):
  for j in range(w):
    l[i][j] += r[i][j] + u[i][j] + d[i][j] -3

unko = []
for i in range(h):
  mm = max(l[i])
  unko.append(mm)

print(max(unko))

Buggy submission (verdict: Runtime Error):
h, w = (int(i) for i in input().split()) 
s = []

for i in range(h):
  tmp = input()
  s.append(tmp)

#左にいくつあるか
l = [[0 for i in range(w)] for j in range(h)]

for i in range(h):
  for j in range(w):
    if s[i][j] == "#":
      l[i][j] = 0
    else:
      if j == 0:
        l[i][j] = 1
        continue
      l[i][j] = l[i][j-1] + 1
#print(l)
      
#上
d = [[0 for i in range(w)] for j in range(h)]

for j in range(w):
  for i in range(h):
    if s[i][j] == "#":
      d[i][j] = 0
    else:
      if i == 0:
        d[i][j] = 1
        continue
      d[i][j] = d[i-1][j] + 1  
#print(d)      

#右にいくつあるか
r = [[0 for i in range(w)] for j in range(h)]

for i in range(h):
  for j in range(w)[::-1]:
#    print(i,j)
    if s[i][j] == "#":
      r[i][j] = 0
    else:
      if j == w-1:
#        print("aa")
        r[i][j] = 1
        continue
      r[i][j] = r[i][j+1] + 1
      
#print(r)

#下
u = [[0 for i in range(w)] for j in range(h)]

for j in range(w):
  for i in range(h)[::-1]:
    if s[i][j] == "#":
      u[i][j] = 0
    else:
      if i == h-1:
        u[i][j] = 1
        continue
      u[i][j] = u[i+1][j] + 1

#print(u)


#足し合わせ
for i in range(h):
  for j in range(w):
    l[i][j] += r[i][j] + u[i][j] + d[i][j] -3

   
import numpy as np
l = np.array(l)
#print(l)
print(l.max())
Problem p03071 (python)

Fixed submission:
a,b = (int(i) for i in input().split()) 
if a == b:
  print(a + b)
else:
  s = max(a, b)
  print(s + s -1)

Buggy submission (verdict: Runtime Error):
a,b = (int(i) for i in input().split()) 
if a == b:
  print(a + b):
else:
  s = max(a, b)
  print(s + s -1)
Problem p03328 (python)

Fixed submission:
a,b = (int(i) for i in input().split())
tower = [sum(range(1, i+1)) for i in range(1,1000)]
print(tower[b-a-1]-b)

Buggy submission (verdict: Runtime Error):
a,b = (int(i) for i in input().split())
def ssum(n):
  if n == 1:
    return 1
  else:
    return n + ssum(n-1)
print(ssum(b-a)-b)
Problem p02885 (python)

Fixed submission:
w, k = map(int, input().split())
x = w - (k*2)
if 0 <= x:
    print(x)
else:
    print(0)

Buggy submission (verdict: Runtime Error):
w ,k = map(int, input().split())
x = w - (k*2)
if 0 <= x:
  print(x)
 else:
  print(0)
Problem p02888 (python)

Fixed submission:
from bisect import bisect_left

N = int(input())
L = list(map(int, input().split()))

L.sort()
ans = 0
for i in range(N - 2):
    for j in range(i + 1, N - 1):
        ans += bisect_left(L, L[i] + L[j]) - j - 1
print(ans)

Buggy submission (verdict: Runtime Error):
n=int(input())
l=sorted([int(i) for i in input().split()],reverse=True)

ans=0
for i in range(n-2):
    c=l[i]
    x=0
    for j in range(i+1,n-1):
        b=l[j]
        for k in range(j+1,n):
            a=l[k]
            if c<(a+b):
                co+=1
            else:
                if k==j+1:
                    x=1
                break
        if x==1:
            break

print(ans)
Problem p03030 (python)

Fixed submission:
n = int(input())
a = []
for i in range(n):
    s, p = input().split()
# pを数値に
    p = int(p)
# i,s,pの並びでlistを入れる
    a.append([i+1, s, p])

# 点数の高い順に並べる
a.sort(key=lambda x:x[2], reverse=True)
# レストラン名の辞書順に並べる
a.sort(key=lambda x:x[1])
# それぞれの市について、辞書順に点数の高い順に並べる
for i in range(n):
    print(a[i][0])

Buggy submission (verdict: Runtime Error):
n = int(input())
a = []
for i in range(n):
    s, p = input().split()
# pを数値に
    p = int(p)
# i,s,pの並びで入れる
    a.append([i+1, s, p])

# 点数の高い順に並べる
a.sort(key=lambda x:x[2], reverse=True)
# レストラン名の辞書順に並べる
a.sort(key=lambda x:x[1])
# それぞれの市について、辞書順に点数の高い順に並べる
for i in range(n):
    print(book[i][0])
Problem p03032 (python)

Fixed submission:
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = 0

for l in range(k + 1):
	for r in range(k - l + 1):
		hand = []
		if l + r > n:
			break
        #handに左からl個
		hand = v[: l]
        #右からr個の要素を追加
		hand.extend(v[n - r:])
        #現時点でのhandの和をhand_sum
		hand_sum = sum(hand)  
		hand.sort()
        #hand_sum中の負の要素を詰めていく
		for i in range(min(k - l - r, l + r)):
			if hand[i] < 0:
			  hand_sum -= hand[i]
		ans = max(ans, hand_sum)

print(ans)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = 0

for l in range(k + 1):
	for r in range(k - l + 1):
		hand = []
        if l + r > n:
          break
		#handに左からl個
		hand = v[: l]
        #右からr個の要素を追加
		hand.extend(v[n - r:])
        #現時点でのhandの和をhand_sum
		hand_sum = sum(hand)  
		hand.sort()
        #hand_sum中の負の要素を詰めていく
		for i in range(min(k - l - r, l + r)):
			if hand[i] < 0:
			  hand_sum -= hand[i]
		ans = max(ans, hand_sum)

print(ans)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = 0

for l in range(k + 1):
	for r in range(k - l + 1):
		hand = []
		#handに左からl個
		hand = v[: l]
        #右からr個の要素を追加
		hand.extend(v[n - r:])
        #現時点でのhandの和をhand_sum
		hand_sum = sum(hand)  
		hand.sort()
        #hand_sum中の負の要素を詰めていく
		for i in range(min(k-l-r,l + r)):
			if hand[i] < 0:
			  hand_sum -= hand[i]
		ans = max(ans, hand_sum)

print(ans)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = 0

for l in range(k + 1):
	for r in range(k - l + 1):
		hand = []
		#handに左からl個
		hand = v[: l]
        #右からr個の要素を追加
		hand.extend(v[n - r:])
        #現時点でのhandの和をhand_sum
		hand_sum = sum(hand)  
		hand.sort()
        #hand_sum中の負の要素を詰めていく
		for i in range(min(hand.sort())):
			if hand[i] < 0:
			  hand_sum -= hand[i]
		ans = max(ans, hand_sum)

print(ans)
Problem p03071 (python)

Fixed submission:
a , b = map(int,input().split())
count = 0

if a > b:
  count = a
  a = a - 1
  if a > b or a == b:
    count = count + a
elif b > a:
  count = b
  b = b - 1
  if b > a or a == b:
    count = count + b
else:
  count = a
  count = count + b

print(count)

Buggy submission (verdict: Runtime Error):
a = int(input())
b = int(input())
count = 0

if a > b:
  count = a
  a = a - 1
  if a > b or a == b:
    count = count + a
elif b > a:
  count = b
  b = b - 1
  if b > a or a == b:
    count = count + b
else:
  count = a
  count = count + b

print(count)
Problem p03076 (python)

Fixed submission:
a =int(input())
b =int(input())
c =int(input())
d =int(input())
e =int(input())
sample_list = []
sample_list.append(a)
sample_list.append(b)
sample_list.append(c)
sample_list.append(d)
sample_list.append(e)
x = []
for i in sample_list:
    if i % 10 == 0:
        x.append(0)
    else:
        x.append(10-(i%10))
print(sum(sample_list)+sum(x)-max(x))

Buggy submission (verdict: Runtime Error):
a =int(input())
b =int(input())
c =int(input())
d =int(input())
e =int(input())

sample_list = list(a,b,c,d,e)
z= sum(sample_list)
for f in sample_list:
    g = list(10 - (a % 10))

h = max(g)
i = sum(g) - h

print(z-i)
Problem p03049 (python)

Fixed submission:
import sys

input = sys.stdin.readline

N = int(input())
A, B, AB = 'A', 'B', 'AB'
endA, startB, startB_endA = 0, 0, 0
ans = 0

for _ in range(N):
    S = input().rstrip()

    if S[0] == B:
        startB += 1

        if S[-1] == A:
            startB -= 1
            startB_endA += 1

    elif S[-1] == A:
        endA += 1
    

    for i in range(1,len(S)):
        if S[i-1] + S[i] == AB:
            ans += 1


if startB_endA == 0:
    ans += min(endA, startB)

elif endA == 0 and startB == 0:
    ans += max(0, startB_endA - 1)

else:
    ans += startB_endA + min(endA, startB)

print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
A, B, AB = 'A', 'B', 'AB'
endA, startB = 0, 0
ans = 0

for _ in range(N):
    S = input()

    if S[0] == B:
        startB += 1

        if S[-1] == A:
            endA += 1
            startB_endA += 1

    elif S[-1] == A:
        endA += 1
    

    for i in range(1,len(S)):
        if S[i-1] + S[i] == AB:
            ans += 1

ans += min(endA, startB)

print(ans)
Problem p03060 (python)

Fixed submission:
N = int(input())

V = list(map(int, input().split()))
C = list(map(int, input().split()))

ans = 0


for v, c in zip(V, C):

    if v > c:

        ans += (v-c)

print(ans)

Buggy submission (verdict: Runtime Error):
import numpy as np
import itertools

N = int(input())

V = np.array(list(map(int, input().split())))
C = np.array(list(map(int, input().split())))

ans = -1

for condition in itertools.product(range(2),repeat=N):

    cond_nparr = np.array(condition)

    x = np.sum(V[cond_nparr]) - np.sum(C[cond_nparr])

    ans = max(ans, x)

print(ans)
Problem p03085 (python)

Fixed submission:
d = {'A':'T', 'T':'A', 'G':'C', 'C':'G'}

print(d[input()])

Buggy submission (verdict: Runtime Error):
N, Q = map(int, input().split())
S = input()
L = len(S)
target = 'AC'
shiftID = len(target) - 1

found_id = []

for i in range(L-shiftID):
    if S[i:i+len(target)] == target:
        found_id.append(i)


for _ in range(Q):
    l, r = map(int, input().split())
    l -= 1
    r -= 1

    ans = 0
    for i in found_id:

        if l <= i and i+shiftID <= r:
            ans += 1
        elif r < i + shiftID:
            break

    print(ans)
Problem p03101 (python)

Fixed submission:
H, W = map(int, input().split())

h, w = map(int, input().split())

tot = H * W

rows = h * W
cols = H * w
common = h * w

print(tot - rows - cols + common)

Buggy submission (verdict: Runtime Error):
num_shops, target_drinks = map(int, input().split())
sell = {}

for _ in range(num_shops):
    a,b = map(int, input().split())

    try:
        sell[a] += b
    except:
        sell[a] = b

ans, bought = 0, 0

for price in sorted(sell):

    selling = sell[price]

    if bought + selling < target_drinks:
        ans += price * selling    
        bought += selling
    else:
        ans += price * (target_drinks - bought)
        break

print(ans)
Problem p03111 (python)

Fixed submission:
#%%
import itertools

#%%

N, A, B, C = map(int, input().split())
materials = [int(input()) for _ in range(N)]



iterobj = itertools.product(range(4),repeat=N)

ans = 10**8

for use in iterobj:

    if 1 in use and 2 in use and 3 in use:

        synth = [0,0,0,0]

        for m, u in  zip(materials, use):
            synth[u] += m

        mp = 10 * (use.count(1) + use.count(2) + use.count(3) - 3)

        mp += abs(A-synth[1]) + abs(B-synth[2]) + abs(C-synth[3])

        ans = min(ans, mp)

print(ans)

Buggy submission (verdict: Runtime Error):
#%%
import sys
INPUT = sys.stdin.readline

def SINGLE_INT(): return int(INPUT())
def MULTIPLE_INT_LIST(): return list(map(int, INPUT().split()))
def MULTIPLE_INT_MAP(): return map(int, INPUT().split())
def SINGLE_STRING(): return INPUT()
def MULTIPLE_STRING(): return INPUT().split()

#%%

N, A, B, C = MULTIPLE_INT_MAP()
materials = MULTIPLE_INT_LIST()
INF = 10 ** 9

#%%

def dfs(cursor, a, b, c):  # cursor:カーソル a,b,c:現在の竹の長さ
    if cursor == N:  # cursorが最後まで行ったら終了する。
        return abs(a - A) + abs(b - B) + abs(c - C) - 30 if min(a, b, c) != 0 else INF
    # なぜ30を減じているのかというと、
    # dfs(0,0,0,0)で始まる以上、最初に選ばれるa,b,cを決定する際にもコストが10増加してしまうからである。

    # a,b,cのいずれかに1本も竹を利用していない場合は不適であるため、三項演算子を利用してその場合のコストをINFとする。

    # 再帰(4**N)
    # カーソルの当たっている竹に対して、(A or B or Cに合成する) or (合成しない)の場合に分ける
    no_compound = dfs(cursor+1, a, b, c)
    compound_to_a = dfs(cursor+1, a + materials[cursor], b, c) + 10
    compound_to_b = dfs(cursor+1, a, b + materials[cursor], c) + 10
    compound_to_c = dfs(cursor+1, a, b, c + materials[cursor]) + 10

    return min(no_compound, compound_to_a, compound_to_b, compound_to_c)

print(dfs(0, 0, 0, 0))
Problem p03206 (python)

Fixed submission:
D = int(input())

s = 'Christmas'

for _ in range(25-D):
    s += ' Eve'

print(s)

Buggy submission (verdict: Runtime Error):

import sys
INPUT = sys.stdin.readline

def SINGLE_INT(): return int(INPUT())
def MULTIPLE_INT_LIST(): return list(map(int, INPUT().split()))
def MULTIPLE_INT_MAP(): return map(int, INPUT().split())
def SINGLE_STRING(): return INPUT()
def MULTIPLE_STRING(): return INPUT().split()

N, K = MULTIPLE_INT_MAP()
trees = [int(input()) for _ in range(N)]
trees.sort()
ans = float('inf')

# for i in range(0, N-K+1):
#     ans = min(ans, trees[i+K-1] - trees[i])
#     if ans == 0:
#         break

# print(ans)

print(min(trees[i+K-1]-trees[i] for i in range(N-K+1)))
Problem p03448 (python)

Fixed submission:
a = int(input())
b = int(input())
c = int(input())
x = int(input())

n = 0
for fiveHundred in range(a+1):
    for hundred in range(b+1):
        for fifty in range(c+1):
            if x == 500*fiveHundred + 100*hundred + 50*fifty:
                n +=1

print(n)

Buggy submission (verdict: Runtime Error):
a = int(input())
b = int(input())
c = int(input())
x = int(input())

#%%

n = 0
for fiveHundred in a:
    for hundred in b:
        for fifty in c:
            if x == 500*fiveHundred + 100*hundred + 50*fifty:
                n +=1

print(n)
Problem p03470 (python)

Fixed submission:
n = int(input())
print(len(set(map(int, [input() for i in range(n)]))))

Buggy submission (verdict: Runtime Error):
n = input()
a = np.array(list(map(int, input().split())))

print(len(np.unique(a)))

Buggy submission (verdict: Runtime Error):
import numpy as np

a = list(map(int, input().split()))
arr = np.array(a[1:])

print(len(np.unuque(arr)))
Problem p03471 (python)

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

def helper(n,y):

    for ten in range(n,-1,-1):
        for five in range(n-ten, -1,-1):


            one = n-ten-five
            sum = 10000*ten + 5000*five + 1000*one

            if y == sum:
                return('{} {} {}'.format(ten,five,one))


    return('-1 -1 -1')

print(helper(n,y))

Buggy submission (verdict: Runtime Error):
n, y = map(int, input().split())


def helper(n,y):
    for ten in range(y,-1,-1):

        for five in range(y-ten, -1,-1):
            
            sum = 10000*ten + 5000*five + 1000*one

            if n < sum:
                continue

            if n == sum:
                return('{} {} {}'.format(ten,five,n-ten-five))

            else:
                return ('-1 -1 -1')

    return('-1 -1 -1')

print(helper(n,y))

Buggy submission (verdict: Runtime Error):
n, y = map(int, input().split())



def helper(n,y):
    for ten in range(y,-1,-1):

        for five in range(y-ten, -1,-1):
            
            sum = 10000*ten + 5000*five + 1000*one

            if n < sum:
                continue

            if n == sum:
                return('{} {} {}'.format(ten,five,one))

            else:
                return ('-1 -1 -1')

    return('-1 -1 -1')

print(helper(n,y))

Buggy submission (verdict: Runtime Error):
n, y = map(int(input().split()))


def helper(n,y):
    for ten in range(y,-1,-1):

        for five in range(y-ten, -1,-1):
            
            sum = 10000*ten + 5000*five + 1000*one

            if n < sum:
                continue

            if n == sum:
                return('{} {} {}'.format(ten,five,one))

            else:
                return ('-1 -1 -1')

    return('-1 -1 -1')

print(helper(n,y))

Buggy submission (verdict: Runtime Error):
n, y = map(int(input()))


def helper(n,y):
    for ten in range(y,-1,-1):

        for five in range(y-ten, -1,-1):
            
            sum = 10000*ten + 5000*five + 1000*one

            if n < sum:
                continue

            if n == sum:
                return('{} {} {}'.format(ten,five,one))

            else:
                return ('-1 -1 -1')

    return('-1 -1 -1')

print(helper(n,y))
Problem p03478 (python)

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

cursum = 0

for x in range(1, n+1):
    y = sum(map(int, str(x)))
    if a <= y and y <= b:
        cursum += x

print(cursum)

Buggy submission (verdict: Runtime Error):
n,a,b = map(int, input().split())

cursum = 0

for n in range(1, l[0]+1):
    x = sum(map(int, str(n)))
    if a <= x and x <= b:
        cursum += n

print(cursum)

Buggy submission (verdict: Runtime Error):
l = list(map(int, input().split()))

cursum = 0

for n in range(1, l[0]+1):
    x = sum(map(int, str(n)))
    if a <= x and x <= b:
        cursum += n

print(cursum)
Problem p02900 (python)

Fixed submission:
# from math import *
from fractions import *

def next_int():
    return int(input())

def next_ints():
    return list(map(int, input().split()))

a, b = next_ints()
n = gcd(a, b)
ans = 1
i = 2
while i*i <= n:
    if n%i == 0:
        ans += 1
        while n%i == 0:
            n //= i
    i += 1
if n != 1:
    ans += 1
print(ans)

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

def next_int():
    return int(input())

def next_ints():
    return list(map(int, input().split()))

a, b = next_ints()
n = gcd(a, b)
ans = 1
i = 2
while i*i <= n:
    if n%i == 0:
        ans += 1
        while n%i == 0:
            n //= i
    i += 1
if n != 1:
    ans += 1
print(int(ans), end="")

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

def next_int():
    return int(input())

def next_ints():
    return list(map(int, input().split()))

a, b = next_ints()
n = gcd(a, b)
ans = 1
i = 2
while i*i <= n:
    if n%i == 0:
        ans += 1
        while n%i == 0:
            n //= i
    i += 1
if n != 1:
    ans += 1
print(ans,end="")

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

def next_int():
    return int(input())

def next_ints():
    return list(map(int, input().split()))

a, b = next_ints()
n = gcd(a, b)
ans = 1
i = 2
while i*i <= n:
    if n%i == 0:
        ans += 1
        while n%i == 0:
            n //= i
    i += 1
if n != 1:
    ans += 1
print(ans)
Problem p03455 (python)

Fixed submission:
a, b = map(int, input().split())
if a * b % 2 == 0:print("Even")
if a * b % 2 != 0:print("Odd")

Buggy submission (verdict: Runtime Error):
a = int(input())
b = int(input())
if (2 % a * b == 0): 
    print("Even")
if (2 % a * b != 0):
    print("0dd")

Buggy submission (verdict: Runtime Error):
a = int(input())
b = int(input())
if (2 % a * b == 0) print("Even")
if (2 % a * b != 0) print("0dd")
Problem p02885 (python)

Fixed submission:
A,B = [int(c) for c in input().strip().split(' ')]
if A>2*B:
    print(A-2*B)
else:
    print(0)

Buggy submission (verdict: Runtime Error):
A,B = [int(c) for c in input().strip().split('')]
if 2*B>A:
  print(0)
else:
  print(A-2*B)
Problem p02885 (python)

Fixed submission:
a,b = map(int,input().split())
print(max(0,a-2*b))

Buggy submission (verdict: Runtime Error):
input()
a,b=map(int,input().split())
print(int(max(0,a-2*b)))

Buggy submission (verdict: Runtime Error):
gomi=input()
tako=[int(i) for i in input().split()]
print(int((sum(tako)**2-sum([i**2 for i in tako]))/2))
Problem p02886 (python)

Fixed submission:
gomi=input()
tako=[int(i) for i in input().split()]
print(int((sum(tako)**2-sum([i**2 for i in tako]))/2))

Buggy submission (verdict: Runtime Error):
gomi=input()
tako= [i for i in input().split()]
print((sum(tako)**2-sum([i**2 for i in tako]))/2)
Problem p02936 (python)

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(1000000)

N,Q = map(int,input().split())
to = [[] for _ in range(N+1)]
ans = [0 for _ in range(N+1)]

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

def dfs(v, p):
    for u in to[v]:
        if u == p:
            continue
        ans[u] += ans[v]
        dfs(u, v)
dfs(1, -1)
print(*ans[1:])

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000)

N,Q = map(int,input().split())
to = [[] for _ in range(N+1)]
ans = [0 for _ in range(N+1)]

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

def dfs(v, p):
    for u in to[v]:
        if u == p:
            continue
        ans[u] += ans[v]
        dfs(u, v)
dfs(1, -1)
print(*ans[1:])

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
#sys.setrecursionlimit(100000)
N,Q = map(int,input().split())
to = [[] for _ in range(N+1)]
ans = [0 for _ in range(N+1)]

for _ in range(N-1):
    a,b=map(int,input().split())
    to[a].append(b)
    to[b].append(a)
for _ in range(Q):
    px = list(map(int,input().split()))
    ans[px[0]] += px[1]

def dfs(v, p):
    for u in to[v]:
        if u == p:
            continue
        ans[u] += ans[v]
        dfs(u, v)
dfs(1, -1)
print(*ans[1:])
Problem p02949 (python)

Fixed submission:
import sys
input = sys.stdin.readline

sys.setrecursionlimit(10000000)

N,M,P=map(int,input().split())
INF = 1000000000
edge = []
to = [[] for _ in range(N)]
rto = [[] for _ in range(N)]
reachable_from1 = [False]*N
reachable_fromN = [False]*N
for i in range(M):
    a,b,c = map(int,input().split())
    a-=1
    b-=1
    c=-c
    to[a].append(b)
    rto[b].append(a)
    edge.append([a,b,c])

def dfs(v):
    if reachable_from1[v] == True:
        return
    reachable_from1[v] = True
    for u in to[v]:
        dfs(u)

def rdfs(v):
    if reachable_fromN[v] == True:
        return
    reachable_fromN[v] = True
    for u in rto[v]:
        rdfs(u)

dfs(0)
rdfs(N-1)
ok = [False]*N
for i in range(N):
    ok[i] = reachable_from1[i]&reachable_fromN[i]

d = [INF]*N
d[0] = 0
update = True
step=0
while update:
    update = False
    for i in range(M):
        a,b,c=edge[i]
        if not(ok[a]): continue
        newD = d[a]+c+P
        if newD < d[b]:
            d[b] = newD
            update = True
    step+=1
    if step > N:
        print(-1)
        sys.exit()
print(max(0,-d[N-1]))

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

sys.setrecursionlimit(10000000)

N,M,P=map(int,input().split())
INF = 1000000000
edge = []
to = [[] for _ in range(N)]
rto = [[] for _ in range(N)]
reachable_from1 = [False]*N
reachable_fromN = [False]*N
for i in range(M):
    a,b,c = map(int,input().split())
    a-=1
    b-=1
    c=-c
    to[a].append(b)
    rto[b].append(a)
    edge.append([a,b,c])

def dfs(v):
    if reachable_from1[v] == True:
        return
    reachable_from1[v] = True
    for u in to[v]:
        dfs(u)

def rdfs(v):
    if reachable_fromN[v] == True:
        return
    reachable_fromN[v] = True
    for u in rto[v]:
        rdfs(u)

dfs(0)
rdfs(N-1)
ok = [False]*N
for i in range(N):
    ok[i] = reachable_from1[i]&reachable_fromN[i]

d = [INF]*N
d[0] = 0
update = True
step=0
while update:
    update = False
    for i in range(N):
        a,b,c=edge[i]
        if not(ok[a]): continue
        newD = d[a]+c+P
        if newD < d[b]:
            d[b] = newD
            update = True
    step+=1
    if step > N:
        print(-1)
        sys.exit()
print(max(0,-d[N-1]))