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

Fixed submission:
n=int(input())
A=list(map(int,input().split()))
  
cnt=[abs(i-(n-i-1)) for i in range(n)]
if sorted(A)==sorted(cnt):
  print((2**(n//2))%(10**9+7))
else:
  print(0)

Buggy submission (verdict: Runtime Error):
n=int(input())
A=list(map(int,input().split()))
  
cnt=[0]*n
for a in A:
  cnt[a]+=1
for i in range(len(cnt)):
  if i==0 and cnt[i]>1:
    print(0)
    exit()
  elif cnt[i]>3:
    print(0)
    exit()
print(int((2**(n/2))%(10**9+7)))
Problem p03944 (python)

Fixed submission:
w,h,n = map(int,input().split())
x1 = 0
x2 = w
y3 = 0
y4 = h

for i in range(n):
    x,y,a = map(int,input().split())
    if a == 1:
        x1 = max(x1,x)
    elif a == 2:
        x2 = min(x2,x)
    elif a == 3:
        y3 = max(y3,y)
    else:
        y4 = min(y4,y)
X = x2 - x1
Y = y4 - y3
s = X * Y
if X < 0 or Y < 0:
    print(0)
else:
    print(s)

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

x1,x2,y1,y2=0,w,0,h
for i in range(n):
  x,y,a=map(int,input().split())
  if a==1:
      x1=max(x1,x)
  elif a==2:
      x2=min(x2,x)
  elif a==3:
      y1=max(y1,y)
  else:
      y2=min(y2,y)
        
s=(x2-x1)*(y4-y3)
if s<0:
  print(0)
else:
  print(s)
Problem p02639 (python)

Fixed submission:
x = list(map(lambda x:int(x), input().split()))
#x = list()

for i in range(len(x)):
  if x[i] == 0:
    print(i+1)
    break

Buggy submission (verdict: Runtime Error):
x = list(map(lambda x:int(x), input().split()))
X = x[0]
N = x[1]
p = []
if N != 0:
  p = list(map(lambda x:int(x), input().split()))

near = [False]*102
#print(near)
#print(near[200])

for i in p:
  near[i] = True
  
for i in range(0,100):
  if not near[X-i]:
    print(X-i)
    break
  elif not near[X+i]:
    print(X+i)
    break
Problem p02675 (python)

Fixed submission:
x = int(input())

if x % 10 == 3:
  print("bon")
elif x % 10 == 0 or x % 10 == 1 or x % 10 == 6 or x % 10 == 8:
  print("pon")
else:
  print("hon")

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

if x % 10 == 3:
  print("bon")
elif x % 10 == 0 or x % 10 == 1 or x % 10 == 6 or x % 10 == 8:
  print("pon")
else:
  print("hon")
Problem p02993 (python)

Fixed submission:
x = input()
x = str(x)
f = True

for i in range(1,4):
  if(x[i-1] == x[i]):
    f = False
    break
   
if f:
  print("Good")
else:
  print("Bad")

Buggy submission (verdict: Runtime Error):
x = input()
x = str(x)
f = True

for i in range(1,3)
	if(x[i-1] == x[i])
  		f = False
    	break
   
if f:
  print("Good")
else:
  print("Bad")
Problem p03457 (python)

Fixed submission:
n = input()
n = int(n)

points = []
points.append(("0", "0", "0"))
for i in range(n):
  points.append(input().split())

for i in range(len(points)-1):
  dt = int(points[i+1][0]) - int(points[i][0])
  dx = int(points[i+1][1]) - int(points[i][1])
  dy = int(points[i+1][2]) - int(points[i][2])
  
  if dt < abs(dx)+abs(dy):
    print("No")
    exit()
  elif (dt - ( abs(dx)+abs(dy) ) ) % 2 == 1:
    print("No")
    exit()

print("Yes")

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

points = []
points.append(("0", "0", "0"))
for i in range(n):
  points.append(input().split())

for i in range(len(points)-1):
  dt = int(points[i+1][0]) - int(points[i][0])
  dx = inat(points[i+1][1]) - int(points[i][1])
  dy = int(points[i+1][2]) - int(points[i][2])
  
  if dt < abs(dx)+abs(dy):
    print("No")
    exit()
  elif (dt - ( abs(dx)+abs(dy) ) ) % 2 == 1:
    print("No")
    exit()

print("Yes")
Problem p02658 (python)

Fixed submission:
n = int(input())
lst = list(map(int, input().strip().split()))[:n]
mx = 1000000000000000000
if 0 in lst:
    print(0)
else:
    cur = lst[0]
    for x in lst[1:]:
        cur = cur * x
        if cur > mx:
            cur = -1
            break
    print(cur)

Buggy submission (verdict: Runtime Error):
n = int(raw_input())
lst = list(map(int, raw_input().strip().split()))[:n]
mx = 1000000000000000000
if 0 in lst:
    print(0)
else:
    cur = lst[0]
    for x in lst[1:]:
        cur = cur * x
        if cur > mx:
            cur = -1
            break
    print(cur)
Problem p02747 (python)

Fixed submission:
S=input()

i=0
flag=0
while i<len(S) and len(S)%2==0:
    if S[i] == 'h' and S[i+1] == 'i':
        i=i+2
    else:
        flag=1
        i=len(S)

if flag==0 and len(S)%2==0:
    print('Yes')
else:
    print('No')

Buggy submission (verdict: Runtime Error):
S=input()

i=0
flag=0
while i<len(S) and len(S)>=2:
    if S[i] == 'h' and S[i+1] == 'i':
        i=i+2
    else:
        flag=1
        i=len(S)

if flag==0 and len(S)>=2:
    print('Yes')
elif flag==1 or len(S)<2:
    print('No')

Buggy submission (verdict: Runtime Error):
S=input()

i=0
flag=0
while i<len(S):
    if S[i] == 'h' and S[i+1] == 'i':
        i=i+2
    else:
        flag=1
        i=len(S)

if flag==0:
    print('Yes')
elif flag==1:
    print('No')

Buggy submission (verdict: Runtime Error):
S=input()

i=0
flag=0
while i<len(S):
    if S[i] == 'h' and S[i+1] == 'i':
        i=i+2
    else:
        print('No')
        flag=1
        i=len(S)

if flag==0:
    print('Yes')
Problem p03378 (python)

Fixed submission:
# coding: utf-8
# Your code here!

N,M,X=map(int,input().split())
Mlist=list(map(int,input().split()))
Nlist=[0]*N
for a in Mlist:
  Nlist[a]=1

a=sum(Nlist[X:])
b=sum(Nlist[:X])
print(min([a,b]))

Buggy submission (verdict: Runtime Error):
N,M,X=map(int,input().split())
Mlist=list(map(int,input().split()))
Nlist=[0]*N
for a in Mlist:
  Nlist[a]=1

a=Nlist[X:]
b=Nlist[:X]
print(min([a,b])
Problem p03543 (python)

Fixed submission:
s=input()
if len(set(s[1:]))==1 or len(set(s[:-1]))==1:
  print('Yes')
else:
  print('No')

Buggy submission (verdict: Runtime Error):
if len(sed(list(input())))<=2:
  print('Yes')
else:
  print('No')
Problem p03544 (python)

Fixed submission:
n=int(input())
rec=[2,1]
if n>=2:
    for i in range(2,n+1):
        rec.append(rec[i-2]+rec[i-1])
print(rec[n])

Buggy submission (verdict: Runtime Error):

n=int(input())
rec=[2,1]
if n>=2:
    for i in range(2,n):
        rec.append(rec[i-2]+rec[i-1])
print(rec[n])

Buggy submission (verdict: Runtime Error):
n=int(input())
rec=[2,1]
for i in range(2,n):
  rec.append(rec[i-2]+rec[i-1])
print(rec[n])
Problem p03672 (python)

Fixed submission:
def chunked(iterable, n):
    return [iterable[x:x + n] for x in range(0, len(iterable), n)]

s=input()
for i in range(1,len(s)):
    a=s[:(-1*i)]
    if len(a)>1:
        li=chunked(a,(len(a)+1)//2)
        
        if li[0]==li[1]:
            print(len(a))
            quit()

Buggy submission (verdict: Runtime Error):

Beta
  paiza.IO
新規コード
一覧
ウェブ開発  
New!
日本語  サインアップ ログイン
Python3 Split Button!	
Enter a title here
Main.py 
Success  

  
1
2
3
4
5
6
7
8
9
10
11
12
13
def chunked(iterable, n):
    return [iterable[x:x + n] for x in range(0, len(iterable), n)]
s=input()
for i in range(1,len(s)):
    a=s[:(-1*i)]
    if len(a)>1:
        li=chunked(a,(len(a)+1)//2)
        
        if li[0]==li[1]:
            print(len(a))
            quit()
    
  実行 (Ctrl-Enter) Split Button!             
出力
入力
コメント 0
(0.04 sec)
 
2
Problem p02691 (python)

Fixed submission:
from collections import defaultdict
n = int(input())
a = list(map(int, input().split()))
ci = defaultdict(int)
cj = defaultdict(int)
for i in range(n):
    ci[a[i]+i] += 1
    cj[i-a[i]] += 1
cnt = 0
for k in ci.keys():
    cnt += ci[k] * cj[k]
print(cnt)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
n = int(input())
a = np.array(list(map(int, input().split())))
ci = defaultdict(int)
cj = defaultdict(int)
for i in range(n):
    ci[a[i]+i] += 1
    cj[i-a[i]] += 1
cnt = 0
for k in ci.keys():
    cnt += ci[k] * cj[k]
print(cnt)

Buggy submission (verdict: Runtime Error):
# 愚直な方法: O(n^2)
n = int(input())
a = np.array(list(map(int, input().split())))
cnt = 0
for i in range(1, n+1):
    for j in range(i+1, n+1):
        if abs(i-j) == (a[i-1]+a[j-1]):
            cnt += 1
print(cnt)
Problem p02694 (python)

Fixed submission:
import math

x = int(input())
m = 100
i = 0.01
y = 0
flag = True
while flag:
    m = math.floor(m*(1+i))
    y += 1
    if m >= x:
        break
print(y)

Buggy submission (verdict: Runtime Error):
x = int(input())
m = 100
i = 0.01
y = 0
flag = True
while flag:
    m = math.floor(m*(1+i))
    y += 1
    if m >= x:
        break
print(y)
Problem p02705 (python)

Fixed submission:
import math
r = int(input())
print(2 * math.pi * r)

Buggy submission (verdict: Runtime Error):
r = int(input())
print(2 * math.pi * r)
Problem p02756 (python)

Fixed submission:
from collections import deque
s = deque(input())
n = int(input())
qs = [list(input().split()) for i in range(n)]
pre = deque()
suf = deque()
frag = True
for q in qs:
    if len(q) == 1:
        frag = not frag
    else:
        if (frag, q[1]) == (True, "1") or (frag, q[1]) == (False, "2"):
            pre.appendleft(q[2])
        else:
            suf.append(q[2])
if frag is True:
    print("".join(list(pre)+list(s)+list(suf)))
else:
    suf.reverse()
    s.reverse()
    pre.reverse()
    print("".join(list(suf)+list(s)+list(pre)))

Buggy submission (verdict: Runtime Error):
from collections import deque
s = deque(input())
n = int(input())
qs = [list(input().split()) for i in range(n)]
pre = deque()
suf = deque()
frag = True
for q in qs:
    if len(q) == 1:
        frag = not frag
    else:
        if (frag, q[1]) == (True, "1") or (frag, q[1]) == (False, "2"):
            pre.appendleft(q[2])
        else:
            suf.append(q[2])
if frag is True:
    print("".join(pre+s+suf))
else:
    suf.reverse()
    s.reverse()
    pre.reverse()
    print("".join(suf+s+pre))
Problem p02772 (python)

Fixed submission:
N = int(input())
s = list(map(int, input().split()))
cond = True
for i in s:
  if i%2 == 0:
    if i%3 == 0 or i%5 == 0:
      continue
    else:
      cond = False
      break
if cond is True:
  print("APPROVED")
else:
  print("DENIED")

Buggy submission (verdict: Runtime Error):
N = int(input())
s = [int(input()) for _ in range(N)]
cond = True
for i in s:
  if i%2 == 0:
    if i%3 == 0 or i%5 == 0:
      continue
    else:
      cond = False
      break
if cond is True:
  print("APPROVED")
else:
  print("DENIED")
Problem p03611 (python)

Fixed submission:
from collections import Counter
n=int(input())
a=list(map(int,input().split()))
for i in range(n):
    a.append(a[i]-1)
    a.append(a[i]+1)
c=Counter(a).most_common()
print(c[0][1])

Buggy submission (verdict: Runtime Error):
from collections import Counter
n=int,input()
a=list(map(int,input().split()))
for i in range(n):
    a.append(a[i]-1)
    a.append(a[i]+1)
c=Counter(a).most_common()
print(c[0][1])

Buggy submission (verdict: Runtime Error):
n=int,input()
a=list(map(int,input().split()))
m=max(a)
n=min(a)
for i in range(n,m+1):
    pre=0
    for j in range(len(a)):
        if i-1<=a[j]<=i+1:
            pre+=1
    ans=max(ans,pre)
    
print(ans)
Problem p03633 (python)

Fixed submission:
import fractions
n=int(input())
t=[int(input()) for _ in range(n)]

ans=t[0]

def lcm(x,y):
    return (x*y)//fractions.gcd(x,y)

for i in range(1,n):
    ans=lcm(ans,t[i])
print(ans)

Buggy submission (verdict: Runtime Error):
import math
n=int(input())
t=[int(input()) for _ in range(n)]

ans=t[0]

def lcm(x,y):
    return (x*y)//math.gcd(x,y)

for i in range(1,n):
    ans=lcm(ans,t[i])
print(ans)

Buggy submission (verdict: Runtime Error):
import math as mt
n=int(input())
t=[int(input()) for _ in range(n)]

ans=t[0]
def lcm(x,y):
    return (x*y)//mt.gcd(x,y)
for i in range(1,n):
    ans=lcm(ans,t[i])
print(ans)
Problem p02646 (python)

Fixed submission:
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    A, V = map(int, input().split())
    B, W = map(int, input().split())
    T = int(input())

    if W >= V:
        print('NO')
    elif abs((A - B) / (W - V)) <= T:
        print('YES')
    else:
        print('NO')

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    A, V = map(int, input().split())
    B, W = map(int, input().split())
    T = int(input())

    if A == B:
        print('YES')
    elif W > V:
        print('NO')
    elif (A - B) % (W - V) == 0 and (A - B) // (W - V) <= T:
        print('YES')
    else:
        print('NO')

if __name__ == '__main__':
    main()
Problem p02678 (python)

Fixed submission:
import sys
from collections import deque
def input(): return sys.stdin.readline().rstrip()

def main():
    N, M = map(int, input().split())
    AB = [list(map(int, input().split())) for _ in range(M)]

    root = [[] for _ in range(N + 1)]
    for a, b in AB:
        root[a].append(b)
        root[b].append(a)

    que = deque()
    que.append(1)
    front = [-1] * (N + 1)
    front[1] = 1
    while que:
        point = que.popleft()
        for i in root[point]:
            if front[i] > 0:
                continue
            front[i] = point
            que.append(i)

    if min(front[1:]) < 0:
        print('No')
    else:
        print('Yes')
        for i in range(2, N + 1):
            print(front[i])

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
from collections import deque
def input(): return sys.stdin.readline().rstrip()

def main():
    N, M = map(int, input().split())
    AB = [list(map(int, input().split())) for _ in range(M)]

    root = [[] for _ in range(M + 1)]
    for a, b in AB:
        root[a].append(b)
        root[b].append(a)

    que = deque()
    que.append(1)
    front = [-1] * (N + 1)
    front[1] = 1
    while que:
        point = que.popleft()
        for i in root[point]:
            if front[i] > 0:
                continue
            front[i] = point
            que.append(i)

    if min(front[2:]) < 0:
        print('No')
    else:
        print('Yes')
        for i in range(2, N + 1):
            print(front[i])

if __name__ == '__main__':
    main()
Problem p02780 (python)

Fixed submission:
import sys
from functools import lru_cache
def input(): return sys.stdin.readline().rstrip()

sys.setrecursionlimit(10 ** 9)

@lru_cache(maxsize=None)
def calc(n):
    if n == 1:
        return 1
    else:
        return n + calc(n - 1)

def main():
    N, K = map(int, input().split())
    P = tuple(map(int, input().split()))

    l = 0
    sum_v = 0
    max_v = 0
    max_i = 0
    ind = 0
    for i in range(N-K+1):
        while l < K:
            sum_v += P[ind]
            ind += 1
            l += 1
        if sum_v > max_v:
            max_v = sum_v
            max_i = i
        l -= 1
        sum_v -= P[i]
    print(sum([calc(p) / p for p in P[max_i:max_i+K]]))

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
from functools import lru_cache
def input(): return sys.stdin.readline().rstrip()

def calc(n):
    if n == 1:
        return 1
    else:
        return n + calc(n - 1)

def main():
    N, K = map(int, input().split())
    P = tuple(map(int, input().split()))

    l = 0
    sum_v = 0
    max_v = 0
    max_i = 0
    ind = 0
    for i in range(N-K+1):
        while l < K:
            sum_v += P[ind]
            ind += 1
            l += 1
        if sum_v > max_v:
            max_v = sum_v
            max_i = i
        l -= 1
        sum_v -= P[i]
    print(sum([calc(p) / p for p in P[max_i:max_i+K]]))

if __name__ == '__main__':
    main()
Problem p02802 (python)

Fixed submission:
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    n, m = map(int, input().split())
    ac = set()
    wa = [0] * (n+1)

    for _ in range(m):
        p, s = input().split()
        p = int(p)
        if p in ac:
            continue
        if s == 'AC':
            ac.add(p)
        elif s == 'WA':
            wa[p] += 1
    wa = [j for i, j in enumerate(wa) if i in ac]
    print(len(ac), sum(wa))

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    n, m = map(int, input().split())
    ac = set()
    wa = [0] * (m+1)

    for _ in range(m):
        p, s = input().split()
        p = int(p)
        if p in ac:
            continue
        if s == 'AC':
            ac.add(p)
        elif s == 'WA':
            wa[p] += 1
    print(len(ac), sum(wa))

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    n, m = map(int, input().split())
    ps = [input() for _ in range(m)]

    ac = set()
    wa = [0] * m

    for i in ps:
        p, s = i.split()
        p = int(p)
        if s == 'AC':
            ac.add(p)
        elif p not in ac and s == 'WA':
            wa[p] += 1
    print(len(ac), sum(wa))

if __name__ == '__main__':
    main()
Problem p02843 (python)

Fixed submission:
x = int(input())
v = x // 100
modv = x % 100
if v > 0 and (modv == 0 or modv / v <= 5):
    print(1)
else:
    print(0)

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

if (x % 100) / (x // 100) > 5:
    print(0)
else:
    print(1)
Problem p02844 (python)

Fixed submission:
n,s = [input() for i in range(2)]
c = 0
for i in range(1000):
    if i < 10:
        a1 = '0'
        a2 = '0'
        a3 = str(i)
    elif i < 100:
        a1 = '0'
        a2 = str(i)[0]
        a3 = str(i)[1]
    else:
        a1 = str(i)[0]
        a2 = str(i)[1]
        a3 = str(i)[2]
    p1 = s.find(a1)
    if p1 >= 0:
        p2 = s.find(a2, p1 + 1)
        if p2 >= 0:
            if s.find(a3, p2 + 1) >= 0:
                c += 1
print(c)

Buggy submission (verdict: Runtime Error):
n,s = [input() for i in range(2)]
c = 0
for i in range(1000):
    if i < 10:
        a1 = '0'
        a2 = '0'
        a3 = i
    elif i < 100:
        a1 = '0'
        a2 = str(i)[0]
        a3 = str(i)[1]
    else:
        a1 = str(i)[0]
        a2 = str(i)[1]
        a3 = str(i)[2]
    p1 = s.find(a1)
    if p1 >= 0:
        p2 = s.find(a2, p1 + 1)
        if p2 >= 0:
            if s.find(a3, p2 + 1) >= 0:
                c += 1
print(c)
Problem p02850 (python)

Fixed submission:
import sys
from collections import deque
def input(): return sys.stdin.readline().rstrip()

def main():
    N = int(input())
    ab = [tuple(map(int, input().split())) for _ in range(N-1)]

    graph = [set() for _ in range(N+1)]
    for a, b in ab:
        graph[a].add(b)
        graph[b].add(a)

    d = deque()
    d.append(1)

    ans = [-1] * (N+1)

    parent = [-1] * (N+1)
    parent[1] = 1

    while d:
        p = d.popleft()
        c = 0
        for g in graph[p]:
            if parent[g] > 0 or g == parent[g]:
                continue
            c += 1
            if c == ans[p]:
                c += 1
            ans[g] = c

            parent[g] = p
            d.append(g)

    print(max(ans))
    for _, b in ab:
        print(ans[b])

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    N = int(input())
    ab = [map(int, input().split()) for _ in range(N-1)]

    graph = [set() for _ in range(N)]
    for a, b in ab:
        graph[a].add(b)

    ans = [-1] * (N-1)
    def dfs(i, color):
        if i == N:
            return
        ans[i-1] = color

        c = 1
        for g in graph[i]:
            if c == color:
                c += 1
            dfs(g, c)
            c += 1

    dfs(1, 1)
    print(max(ans))
    print("\n".join(map(str, ans)))

if __name__ == '__main__':
    main()
Problem p02861 (python)

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

def main():
    n = int(input())
    xy = [tuple(map(int, input().split())) for _ in range(n)]
    dis = [[None for _ in range(n)] for _ in range(n)]
    sum_v = 0
    p = 1
    for i in range(n):
        p *= i + 1
        for j in range(n):
            if i == j or dis[i][j] is not None:
                continue
            d = math.sqrt((xy[i][0] - xy[j][0]) ** 2 + (xy[i][1] - xy[j][1]) ** 2)
            dis[i][j] = d
            dis[j][i] = d
            sum_v += d
    print(sum_v * 2 / n)

if __name__ == '__main__':
    main()

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

def main():
    n = int(input())
    xy = [tuple(map(int, input().split())) for _ in range(n)]
    checked = [[None for _ in range(n)] for _ in range(n)]
    sum_v = 0
    p = 1
    for i in range(n):
        p *= i + 1
        for j in range(n):
            if i == j or dis[i][j] is not None:
                continue
            checked[i][j] = 1
            checked[j][i] = 1
            sum_v += math.sqrt((xy[i][0] - xy[j][0]) ** 2 + (xy[i][1] - xy[j][1]) ** 2)
    print(sum_v * 2 / n)

if __name__ == '__main__':
    main()
Problem p02921 (python)

Fixed submission:
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    s, t = input(), input()
    print(sum([1 for i in range(3) if s[i] == t[i]]))

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    n = int(input())
    h = map(int, input().split())

    ans, bef, cnt = 0, 0, 0

    for i in h:
        if i <= bef:
            cnt += 1
        else:
            ans = max(ans, cnt)
            cnt = 0
        bef = i
    print(max(ans, cnt))

if __name__ == '__main__':
    main()
Problem p02936 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 9)
def input(): return sys.stdin.readline().rstrip()

def main():
    N, Q = map(int, input().split())
    ki = [[] for _ in range(N + 1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        ki[a].append(b)
        ki[b].append(a)
    PX = [tuple(map(int, input().split())) for _ in range(Q)]

    ans = [0 for _ in range(N + 1)]
    for p, x in PX:
        ans[p] += x
    visited = set()
    def dfs(i):
        visited.add(i)
        for j in ki[i]:
            if j in visited:
                continue
            ans[j] += ans[i]
            dfs(j)
    dfs(1)
    print(*ans[1:])

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10 ** 20)
def input(): return sys.stdin.readline().rstrip()

def main():
    N, Q = map(int, input().split())
    ki = [[] for _ in range(N + 1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        ki[a].append(b)
    PX = [tuple(map(int, input().split())) for _ in range(Q)]

    ans = [0 for _ in range(N + 1)]
    def dfs(i, n):
        ans[i] += n
        for j in ki[i]:
            dfs(j, n)

    for p, x in PX:
        dfs(p, x)
    print(*ans[1:])

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    N, Q = map(int, input().split())
    ki = [[] for _ in range(N + 1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        ki[a].append(b)
    PX = [tuple(map(int, input().split())) for _ in range(Q)]

    ans = [0 for _ in range(N + 1)]
    def dfs(i, n):
        ans[i] += n
        for j in ki[i]:
            dfs(j, n)

    for p, x in PX:
        dfs(p, x)
    print(*ans[1:])

if __name__ == '__main__':
    main()
Problem p02958 (python)

Fixed submission:
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    n = int(input())
    p = list(map(int, input().split()))

    c = 0
    for i in range(n):
        if (i + 1) != p[i]:
            c += 1
    if c <= 2:
        print('YES')
    else:
        print('NO')

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    n = int(input())
    p = [*map(int, input().split())]

    c = 0
    for i in range(n):
        if (i + 1) != p[i]:
            c += 1
    if c <= 2:
        print('YES')
    else:
        print('NO')

if __name__ == '__main__':
    main()