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

Fixed submission:
h,w=map(int,input().rstrip().split(' '))
A = list()
a = "#"*(w+2)
A.append(a)
for i in range(h):
  a = str(input().rstrip())
  a = "#"+a+"#"
  A.append(a)

a = "#"*(w+2)
A.append(a)
for a in A:
  print(a)

Buggy submission (verdict: Runtime Error):
h,w=map(int,input().rstrip().split(' '))
A = list()
a = "#"*(W+2)
A.append(a)
for i in range(h):
  a = str(input().rstrip())
  a = "#"+a+"#"
  A.append(a)

a = "#"*(W+2)
A.append(a)
for a in A:
  print(a)
Problem p03730 (python)

Fixed submission:
a,b,c = map(int,input().rstrip().split())
ans = False
for i in range(b):
  if(a*i%b==c and not(ans)):
    print("YES")
    ans = True
    
if(not(ans)):
  print("NO")

Buggy submission (verdict: Runtime Error):
a,b,c = map(int,input())
for i in range(b):
  if(a*i%b==c):
    print("YES")
    ans = True
    
if(!ans):print("NO")

Buggy submission (verdict: Runtime Error):
a,b,c = map(int,input())
for i in range(b):
  if(a*i%b==c):
    print("YES")
    exit()
    
print("NO")

Buggy submission (verdict: Runtime Error):
import sys
a,b,c = map(int,input())
for i in range(b):
  if(a*i%b==c):
    print("YES")
    sys.exit()
    
print("NO")
Problem p03738 (python)

Fixed submission:
# a,b = map(int,input().rstrip().split(' '))
a = int(input().rstrip())
b = int(input().rstrip())
if(a>b):print("GREATER")
elif(a<b):print("LESS")
else:print("EQUAL")

Buggy submission (verdict: Runtime Error):
a,b = map(int,input().rstrip().split(' '))
if(a>b):print("GREATER")
elif(a<b):print("LESS")
else:print("EQUAL")
Problem p03844 (python)

Fixed submission:
s = list(map(str,input().split()))
ans = 0
if s[1]=='+':
  ans = int(s[0])+int(s[2])
else:
  ans = int(s[0])-int(s[2])
print(ans)

Buggy submission (verdict: Runtime Error):
s = map(str,input().split())
ans = 0
if s[1]=='+':
  ans = int(s[0])+int(s[2])
else:
  ans = int(s[0])-int(s[2])
print(ans)
Problem p03962 (python)

Fixed submission:
a = set(map(int,input().split()))
print(len(a))

Buggy submission (verdict: Runtime Error):
a = set(map(int,input()))
print(len(a))
Problem p02579 (python)

Fixed submission:
#入力受け取りの高速化、しなくてもよい
#但し文字列のまま扱うときは注意、例えば . は46として、 # は35として扱われる
import sys
input=sys.stdin.buffer.readline
inputs=sys.stdin.buffer.readlines
#ここまでが高速化
h,w=map(int,input().split())
nx,ny=map(int,input().split())
gx,gy=map(int,input().split())
maze=inputs()
#0 based index (0始まり)にする
nx-=1;ny-=1;gx-=1;gy-=1
from collections import deque
#dqが徒歩、2が魔法での移動を入れる
dq=deque([(nx,ny)])
dq2=deque([])
#かかるコストを保存しておく
T=[[float('inf')]*w for  a in range(h)]
#初期値は0で、スタート地点に保存
T[nx][ny]=0
#徒歩でのx,yの差分
moves=((1,0),(-1,0),(0,1),(0,-1))
from itertools import product as pr
#魔法での移動の差分、tuple( (i,j) |-2<=i,j<=2かつ(i,j)!=(0,0)  )と等価
Mmoves=tuple(i for i in pr((-2,-1,0,1,2),repeat=2)if i!=(0,0) )

while dq:
    #先に徒歩で行けるだけ行きたいので、徒歩を使い切るまでは魔法で行った地点たちに手を出さない
    while dq:
        x,y=dq.popleft()
        for i,j in moves:
            nex,ney=x+i,y+j
            #盤面の外や壁にぶつかるのは除外
            if not(0<=nex<h and 0<=ney<w) or maze[nex][ney]==35:continue
            if T[x][y]<T[nex][ney]:
                #コストを更新できるなら改めて捜査
                T[nex][ney]=T[x][y]
                dq.append((nex,ney))
        for i,j in Mmoves:
            nex,ney=x+i,y+j
            if not(0<=nex<h and 0<=ney<w) or maze[nex][ney]==35:continue
            if T[x][y]+1<T[nex][ney]:
                T[nex][ney]=T[x][y]+1
                dq2.append((nex,ney))
    #徒歩で行ける地点を使い切ったので、今度は魔法で行ける地点を捜査
    dq,dq2=dq2,dq
if T[gx][gy]==float('inf'):print(-1)
else:print(T[gx][gy])

Buggy submission (verdict: Runtime Error):
h,w=map(int,input().split())
nx,ny=map(int,input().split())
gx,gy=map(int,input().split())
maze=inputs()
#0 based index (0始まり)にする
nx-=1;ny-=1;gx-=1;gy-=1
from collections import deque
#dqが徒歩、2が魔法での移動を入れる
dq=deque([(nx,ny)])
dq2=deque([])
#かかるコストを保存しておく
T=[[float('inf')]*w for  a in range(h)]
#初期値は0で、スタート地点に保存
T[nx][ny]=0
#徒歩でのx,yの差分
moves=((1,0),(-1,0),(0,1),(0,-1))
from itertools import product as pr
#魔法での移動の差分、tuple( (i,j) |-2<=i,j<=2かつ(i,j)!=(0,0)  )と等価
Mmoves=tuple(i for i in pr((-2,-1,0,1,2),repeat=2)if i!=(0,0) )

while dq:
    #先に徒歩で行けるだけ行きたいので、徒歩を使い切るまでは魔法で行った地点たちに手を出さない
    while dq:
        x,y=dq.popleft()
        for i,j in moves:
            nex,ney=x+i,y+j
            #盤面の外や壁にぶつかるのは除外
            if not(0<=nex<h and 0<=ney<w) or maze[nex][ney]==35:continue
            if T[x][y]<T[nex][ney]:
                #コストを更新できるなら改めて捜査
                T[nex][ney]=T[x][y]
                dq.append((nex,ney))
        for i,j in Mmoves:
            nex,ney=x+i,y+j
            if not(0<=nex<h and 0<=ney<w) or maze[nex][ney]==35:continue
            if T[x][y]+1<T[nex][ney]:
                T[nex][ney]=T[x][y]+1
                dq2.append((nex,ney))
    #徒歩で行ける地点を使い切ったので、今度は魔法で行ける地点を捜査
    dq,dq2=dq2,dq
if T[gx][gy]==float('inf'):print(-1)
else:print(T[gx][gy])
Problem p03337 (python)

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

Buggy submission (verdict: Runtime Error):
a,b=map(int,input().split())
print(max(A+B,A-B,A*B))
Problem p02783 (python)

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

if h%a == 0:
    print(int(h/a))
else:
    print(int(h/a)+1)

Buggy submission (verdict: Runtime Error):
import math

n, d, a = map(int,input().split())
xh = [[0 for i in range(2)] for j in range(n)]
for i in range(n):
    x,h = map(int,input().split())
    xh[i] = [x,h]

num = 0
xh.sort(key=lambda xh:xh[0])

dam = [0]*n
for i in range(n):
    res = xh[i][1]-dam[i]
    pos = xh[i][0]
    if res > 0:
        nat = math.ceil(res/a)
        num += nat
        for j in range(i+1,n):
            if xh[j][0]-d <= pos + d:
                dam[j] += a*nat
            else:
                break
    print(dam)
print(num)

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

h, n = map(int,input().split())
a = np.zeros(n, dtype = int)
b = np.zeros(n, dtype = int)
for i in range(n):
    a[i], b[i] = map(int,input().split())

dp = np.zeros(10001, dtype = int)

for i in range(10001):
    dp[i] = (dp[i-a]+b).min()

print(dp[h])

Buggy submission (verdict: Runtime Error):
h, n = map(int,input().split())
a = list(input().split())
for i in n:
    h -= int(a[i])

if h > 0:
    print('No')
else:
    print("Yes")
Problem p02787 (python)

Fixed submission:
import numpy as np

h, n = map(int,input().split())
a = np.zeros(n, dtype = int)
b = np.zeros(n, dtype = int)
for i in range(n):
    a[i], b[i] = map(int,input().split())

dp = np.zeros(10001, dtype = int)

for i in range(1,10001):
    dp[i] = (dp[i-a]+b).min()

print(dp[h])

Buggy submission (verdict: Runtime Error):
h, n = map(int,input().split())
a = []
b = []
for i in range(n):
    ai, bi = map(int,input().split())
    a.append(ai)
    b.append(bi)

dp = [0]*10001

for i in range(10001):
    dp[i] = (dp[i-a]+b).min()

print(dp[h])

Buggy submission (verdict: Runtime Error):
h, n = map(int,input().split())
a = []
b = []
for i in range(n):
    ai, bi = map(int,input().split())
    a.append(ai)
    b.append(bi)

m = 0
minc = 10**9
newi = -1
oldi = -2
cost = 0
while not newi == oldi:
    oldi = newi
    for i in range(len(n)):
        cost = b[i]*int((h-1)/a[i]+1)
        if cost < minc:
            newi = i
    h -= a[newi]*int((h-1)/a[newi])
    m += b[newi]*int((h-1)/a[newi])
    if newi == oldi:
        m += b[newi]

print(m)
Problem p02788 (python)

Fixed submission:
import math

n, d, a = map(int,input().split())
xh = [[0 for i in range(2)] for j in range(n)]
left = [i for i in range(n)]
for i in range(n):
    x,h = map(int,input().split())
    xh[i] = [x,h]

num = 0
xh.sort(key=lambda xh:xh[0])

for i in range(n):
    for j in range(left[i-1],i):
        if xh[j][0]+d >= xh[i][0]-d:
            left[i] = j
            break

dam = [0]*n

def dsum(k):
    if k == 0:
        return 0
    elif left[k] == 0:
        return dam[k-1]
    else:
        return dam[k-1]-dam[left[k]-1]

for i in range(n):
    res = xh[i][1]-dsum(i)
    nat = max([math.ceil(res/a),0])
    dam[i] = dam[i-1]+a*nat
    if res > 0:
        num += nat

print(num)

Buggy submission (verdict: Runtime Error):
import math

n, d, a = map(int,input().split())
xh = [[0 for i in range(2)] for j in range(n)]
rang = [0 for i in range(n)]
for i in range(n):
    x,h = map(int,input().split())
    xh[i] = [x,h]
    rang[i] = x

num = 0
xh.sort(key=lambda xh:xh[0])]

dam = [0]*n
for i in range(n):
    res = xh[i][1]-dam[i]
    pos = xh[i][0]
    if res > 0:
        nat = math.ceil(res/a)
        num += nat
        for j in range(i+1,n):
            if rang[j] <= pos + 2d:
                dam[j] += a*nat
            else:
                break
print(num)

Buggy submission (verdict: Runtime Error):
import math

n, d, a = map(int,input().split())
xh = []
for i in range(n):
    x,h = map(int,input().split())
    xh.append([x,h])

num = 0
xh.sort(key=lambda xh:xh[0])
l = len(xh)
dam = [0]*n

for i in range(n):
    res = xh[i][1]-dam[i]
    if res > 0:
        nat = math.ceil(res/a)
        pos = xh[i][0]+d
        num += nat
        for j in range(n):
            if math.fabs(xh[j][0]-pos) <= d:
                ###dam[j] += a*nat

print(num)
Problem p02801 (python)

Fixed submission:
c = input()
alp = 'abcdefghijklmnopqrstuvwxyz'

ans = alp[alp.find(c)+1]
print(ans)

Buggy submission (verdict: Runtime Error):
c = input()
alp = 'abcdefghijklmnopqrstuvwxyz'

ans = alp[s.find(c)+1]
print(ans)

Buggy submission (verdict: Runtime Error):
c = input()
alp = 'abcdefghijklmnopqrstuvwxyz'

ans = alp[s.find(c)+1]
Problem p02802 (python)

Fixed submission:
import numpy as np
n,m = map(int,input().split())

p = []
s = []
ans_a = np.zeros(n)
ans_p = np.zeros(n)

for i in range(m):
    pi ,si = map(str,input().split())
    p.append(int(pi))
    s.append(si)

for i in range(m):
    if s[i] == "AC":
        ans_a[p[i]-1] = 1
    elif ans_a[p[i]-1] == 0:
        ans_p[p[i]-1] += 1

for i in range(n):
    if ans_a[i] == 0:
        ans_p[i] = 0

print(int(np.sum(ans_a)),int(np.sum(ans_p)))

Buggy submission (verdict: Runtime Error):
import numpy as np
n,m = map(int,input().split())

p = []
s = []
ans_a = np.zeros(n+1)
ans_p = np.zeros(n+1)

for i in range(m):
    pi ,si = map(str,input().split())
    p.append(int(pi))
    s.append(si)

for i in range(m):
    if s[i] == "AC":
        ans_a[p[i]] = 1
    if s[i] == "WA" and ans_a[p[i]] == 0:
        ans_p[p[i]] += 1

print(int(np.sum(ans_a)), int(np.sum(ans_p))s)
Problem p02811 (python)

Fixed submission:
k, x = map(int,input().split())

if k*500 < x:
    print("No")
else:
    print("Yes")

Buggy submission (verdict: Runtime Error):
k, x = map(int,input().split())

if k*500 < X:
    print("No")
else:
    print("Yes")
Problem p02727 (python)

Fixed submission:
import heapq as hq

X, Y, A, B, C = map(int,input().split())
p = list(map(int,input().split())); p.sort(); p.reverse()
q = list(map(int,input().split())); q.sort(); q.reverse()
r = list(map(int,input().split())); r.sort(); r.reverse()

pq1 = []; hq.heapify(pq1) # 昇順
for i in range(X):
    hq.heappush(pq1, p[i])
for i in range(Y):
    hq.heappush(pq1, q[i])
pq2 = []; hq.heapify(pq2) # 降順で用いる
for i in range(C):
    hq.heappush(pq2, -r[i])
while (len(pq2) and pq1[0] < -pq2[0]):
    hq.heappush(pq1, -hq.heappop(pq2))
    hq.heappop(pq1) # 順序を変えてみた
print(sum(pq1))

Buggy submission (verdict: Runtime Error):
import heapq as hq

X, Y, A, B, C = map(int,input().split())
p = list(map(int,input().split())); p.sort(); p.reverse()
q = list(map(int,input().split())); q.sort(); q.reverse()
r = list(map(int,input().split())); r.sort(); r.reverse()

pq1 = []; hq.heapify(pq1) # 昇順
for i in range(X):
    hq.heappush(pq1, p[i])
for i in range(Y):
    hq.heappush(pq1, q[i])
pq2 = []; hq.heapify(pq2) # 降順で用いる
for i in range(C):
    hq.heappush(pq2, -r[i])
while (pq1[0] < -pq2[0]):
    hq.heappush(pq1, -hq.heappop(pq2))
    hq.heappop(pq1) # 順序を変えてみた
print(sum(pq1))

Buggy submission (verdict: Runtime Error):
import heapq as hq

X, Y, A, B, C = map(int,input().split())
p = list(map(int,input().split())); p.sort(); p.reverse()
q = list(map(int,input().split())); q.sort(); q.reverse()
r = list(map(int,input().split())); r.sort(); r.reverse()

pq1 = []; hq.heapify(pq1) # 昇順
for i in range(X):
    hq.heappush(pq1, p[i])
for i in range(Y):
    hq.heappush(pq1, q[i])
pq2 = []; hq.heapify(pq2) # 降順で用いる
for i in range(C):
    hq.heappush(pq2, -r[i])
while (pq1[0] < -pq2[0]):
    hq.heappush(pq1, -hq.heappop(pq2))
    hq.heappop(pq1) # 順序を変えてみた
print(sum(pq1))
Problem p02797 (python)

Fixed submission:
N, K, S = map(int,input().split())
A = [S] * K
if N > K:
    if S < 10 ** 9:
        A.extend([S + 1] * (N - K))
    else:
        A.extend([S - 1] * (N - K))

print(A[0], end = "")
for i in range(1,N):
    print(" ", end = "")
    print(A[i], end = "")

Buggy submission (verdict: Runtime Error):
N, K, S = map(int,input().split())
A = []

if K == N:
    A.extend([S] * N)
else:
    if S == 1:
        A.extend([1] * K)
        A.extend([2] * (N - K))
    else:
        for i in range((K + 1) // 2):
            A.extend([1, S - 1])
        if N % 2 == 0:
            A.append(1)
        x = S + 1 if S != 10 ** 9 else S - 2
        A.extend([x] * (N - K - 1))

print(A[0], end = "")
for i in range(1,N):
    print(" ", end = "")
    print(A[i], end = "")

Buggy submission (verdict: Runtime Error):
N, K, S = map(int,input().split())
A = []

if K == N:
    A.extend([S] * N)
else:
    if S == 1:
        A.extend([1] * K)
        A.extend([2] * (N - K))
    else:
        for i in range((K + 1) // 2):
            A.extend([1, S - 1])
        if N % 2 == 0:
            A.append(1)
        x = S + 1 if S != 10 ** 9 else S - 2
        A.extend([x] * (N - K - 1))

print(A[0], end = "")
for i in range(1,N):
    print(" " + str(A[i]), end = "")
Problem p02813 (python)

Fixed submission:
import itertools as it

N=int(input())
P=tuple(map(int,input().split()))
Q=tuple(map(int,input().split()))

l=[n for n in range(1,N+1)]
for i,t in enumerate(it.permutations(l)):
    if t==P:
        a=i+1
    if t==Q:
        b=i+1
ans=abs(b-a)

print(ans)

Buggy submission (verdict: Runtime Error):
import itertools as it

N=int(input())
P=tuple(map(int,input().split()))
Q=tuple(map(int,input().split()))

l=[n for n in range(1,N+1)]
for i,t in enumerate(it.permutations(l)):
    if t==P:
        a=i+1
    elif t==Q:
        b=i+1
ans=abs(b-a)

print(ans)
Problem p02845 (python)

Fixed submission:
mod=1000000007

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

ans=1
c1=c2=c3=0
for Ai in A:
    if c1==c2==c3==Ai:
        c1+=1
        ans=ans*3%mod
    elif c1==c2==Ai:
        c1+=1
        ans=ans*2%mod
    elif c2==c3==Ai:
        c2+=1
        ans=ans*2%mod
    elif c1==Ai:
        c1+=1
    elif c2==Ai:
        c2+=1
    elif c3==Ai:
        c3+=1
    else:
        ans=0
        break

print(ans)

Buggy submission (verdict: Runtime Error):
mod=10**9+7

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

if A.count(0)==1: # 1色のみ
    ans=3
elif A.count(0)==2: # 2色のみ
    ans=1
    c1=c2=0
    for Ai in A:
        if Ai==0:
            if c1==0:
                c1+=1
                ans=ans*3%mod
            elif c2==0:
                c2+=1
                ans=ans*2%mod
        else:
            if c1==c2==Ai:
                c1+=1
                ans=ans*2%mod
            else:
                if c1==Ai:
                    c1+=1
                elif c2==Ai:
                    c2+=1
elif A.count(0)==3: # 3色全部
    ans=1
    c1=c2=c3=0
    for Ai in A:
        if Ai==0:
            if c1==0:
                c1+=1
                ans=ans*3%mod
            elif c2==0:
                c2+=1
                ans=ans*2%mod
            elif c3==0:
                c3+=1
        else:
            if c1==c2==c3==Ai:
                c1+=1
                ans=ans*3%mod
            elif c1==c2==Ai or c1==c3==Ai:
                c1+=1
                ans=ans*2%mod
            elif c2==c3==Ai:
                c2+=1
                ans=ans*2%mod
            else:
                if c1==Ai:
                    c1+=1
                elif c2==Ai:
                    c2+=1
                elif c3==Ai:
                    c3+=1

print(ans)
Problem p02926 (python)

Fixed submission:
import math

N=int(input())
e=[]
for i in range(N):
    x,y=map(int,input().split())
    t=math.atan2(y,x)
    e.append((x,y,t));e.append((x,y,t+2*math.pi))

e.sort(key=lambda z:z[2])
# print(e) #DB

sqans=0
for i in range(N):
    dx=e[i][0];dy=e[i][1]
    sqans=max(sqans,dx**2+dy**2)
    for j in range(i+1,i+N):
        dx+=e[j][0];dy+=e[j][1]
        sqans=max(sqans,dx**2+dy**2)
ans=math.sqrt(sqans)
print(ans)

Buggy submission (verdict: Runtime Error):
import math

class Engine:
    def __init__(self,x,y,arg):
        self.x,self.y,self.arg=x,y,arg

    def __str__(self):
        return f'({self.x},{self.y}),arg={self.arg/math.pi*180}'
    
    def __eq__(self,other):
        return self.arg==other.arg
    
    def __lt__(self,other):
        return self.arg<other.arg

    def __gt__(self,other):
        return self.arg>other.arg

N=int(int(input()))

l=[]
for i in range(N):
    x,y=map(int,input().split())
    l.append(Engine(x,y,math.atan2(y,x)))
    l.append(Engine(x,y,math.atan2(y,x)+2*math.pi))
l.sort()

d=[[]for j in range(N)]

for i in range(N):
    e0=l[i]
    d[i].append(e0)
    for j in range (i+1,N+i):
        e=l[j]
        if e.arg>=e0.arg+math.pi:
            break
        else:
            d[i].append(e)

dis=[]
for i in range(N):
    x=sum(e.x for e in d[i])
    y=sum(e.y for e in d[i])
    dis.append(math.sqrt(x**2+y**2))

print(max(dis))
Problem p02936 (python)

Fixed submission:
from collections import deque

N,Q=map(int,input().split())
out=[set() for i in range(N+1)];out[0]=None # 頂点は1-initial
for j in range(N-1):
    a,b=map(int,input().split())
    out[a].add(b)
    out[b].add(a)
# print(out) #DB

a=[0 for i in range(N+1)];a[0]=None

for k in range(Q):
    p,x=map(int,input().split())
    a[p]+=x

# print(a) #DB

q=deque()
q.append(1)
while q:
    u=q.popleft()
    for v in out[u]:
        a[v]+=a[u]
        q.append(v)
        out[v].remove(u)
        # print(q) #DB
    out[u].clear()

print(*a[1:])

Buggy submission (verdict: Runtime Error):
def add(r):
    if r.next==[]:
        pass
    else:
        for v in r.next:
            v.val+=r.val
            add(v)

class Vertex:
    def __init__(self):
        self.val=0
        self.next=[]

N,Q=map(int,input().split())

V=[Vertex() for i in range(N)]

for i in range(N-1):
    a,b=map(int,input().split())
    V[a-1].next.append(V[b-1])

for i in range(Q):
    p,x=map(int,input().split())
    V[p-1].val+=x

add(V[0])

for v in V:
    print(v.val,end=' ')

Buggy submission (verdict: Runtime Error):
def root(V):
    for v in V:
        if v.isroot:
            return v
    return None

def add(r):
    if r.next==[]:
        pass
    else:
        for v in r.next:
            v.val+=r.val
            add(v)

class Vertex:
    def __init__(self):
        self.val=0
        self.next=[]
        self.isroot=True

N,Q=map(int,input().split())

V=[Vertex() for i in range(N)]

for i in range(N-1):
    a,b=map(int,input().split())
    V[a-1].next.append(V[b-1])
    V[b-1].isroot=False

for i in range(Q):
    p,x=map(int,input().split())
    V[p-1].val+=x

r=root(V)
add(r)

for v in V:
    print(v.val,end=' ')

Buggy submission (verdict: Runtime Error):
def root(V):
    for v in V:
        if v.isroot:
            return v
    return None

def add(r):
    if r.next==[]:
        pass
    else:
        for v in r.next:
            v.val+=r.val
            add(v)

class Vertex:
    def __init__(self):
        self.val=0
        self.next=[]
        self.isroot=True

N,Q=map(int,input().split())

V=[Vertex() for i in range(N)]

for i in range(N-1):
    a,b=map(int,input().split())
    V[a-1].next.append(V[b-1])
    V[b-1].isroot=False

for i in range(Q):
    p,x=map(int,input().split())
    V[p-1].val+=x

r=root(V)
add(r)

for v in V:
    print(v.val,end=' ')

Buggy submission (verdict: Runtime Error):
def root(V): # i(Root)
    for i in range(len(V)):
        if V[i][1]==[]:
            return i
    return -1

def add(V,r): # add recursively
    for l in V[r][2]:
        V[l][0]+=V[r][0]
        add(V,l)

N,Q=map(int,input().split())
V=[[0,[],[]] for i in range(N)] # value,prev[],next[]

for i in range(N-1):
    a,b=map(int,input().split())
    V[a-1][2].append(b-1) # append i(Next) to Prev.next
    V[b-1][1].append(a-1) # append i(Prev) to Next.prev

for i in range(Q):
    p,x=map(int,input().split())
    V[p-1][0]+=x

add(V,root(V))

for v in V:
    print(v[0],end=' ')
Problem p02991 (python)

Fixed submission:
from collections import deque

N,M=map(int,input().split())
a=[[[] for j in range(N)] for i in range(3)] # 0-indexのグラフが3つある
for i in range(M):
    u,v=map(lambda x:int(x)-1,input().split())
    a[0][u].append((1,v))
    a[1][u].append((2,v))
    a[2][u].append((0,v))
S,T=map(lambda x:int(x)-1,input().split())
# print(a)

r=[[-3 for j in range(N)] for i in range(3)]
q=deque()
q.append((0,S));r[0][S]=0
while q:
    ui,uj=q.popleft()
    for vi,vj in a[ui][uj]:
        if r[vi][vj]==-3:
            r[vi][vj]=r[ui][uj]+1
            q.append((vi,vj))
# print(r)
print(r[0][T]//3)

Buggy submission (verdict: Runtime Error):
from collections import deque

N,M=map(int,input().split())
a=[set() for i in range(N)] # 0-index
for i in range(M):
    u,v=map(lambda x:int(x)-1,input().split())
    a[u].add(v)
S,T=map(lambda x:int(x)-1,input().split())
# print(a)

r=[-1 for i in range(N)]
q0=deque();q1=deque();q2=deque()
q0.append(S);r[S]=0
while q0:
    u=q0.popleft()
    for v in a[u]:
        q1.append(v)
    for w in a[v]:
        q2.append(w)
    for x in a[w]:
        if r[x]==-1:
            r[x]=r[u]+1
            q0.append(x)
# print(r)
print(r[T])

Buggy submission (verdict: Runtime Error):
from collections import deque

N,M=map(int,input().split())
# 頂点の番号は0-initialのまま扱う
E=set() # 辺から成るset
for i in range(M):
    u,v=map(int,input().split())
    E.add((u,v))
S,T=map(int,input().split())

d1=[set() for i in range(N+1)] # d1[i]=頂点iからの距離が1であるような頂点vから成るset
for e in E:
    u=e[0];v=e[1]
    d1[u].add(v)

d2=[set() for i in range(N+1)] # d2[i]=頂点iからの距離が2であるような頂点vから成るset
for e in E:
    u=e[0];v=e[1]
    d2[u].update(d1[v])

d3=[set() for i in range(N+1)]
for e in E:
    u=e[0];v=e[1]
    d3[u].update(d2[v])
'''
print(e)
print(d1)
print(d2)
print(d3)
'''
#dis=[-1 for i in range(N+1)];dis[S]=0 # 頂点Sからの距離
dis={S:0}
# WFS
q=deque() # 遅ければlistで実装
q.append(S)
while q:
#    print(q) #DB
#    print(dis) #DB
    u=q.popleft()
    #vs=[v for v in d3[u] if dis[v]==-1]
    vs=[v for v in d3[u] if v not in dis]
    for v in vs:
        dis[v]=dis[u]+1
        q.append(v)

print(dis[T])
Problem p03006 (python)

Fixed submission:
from collections import Counter
import itertools as it

N=int(input())
l=[]
for i in range(N):
    l.append(tuple(map(int,input().split())))
l.sort()

if N==1:
    ans=1
else:
    d=[(c[1][0]-c[0][0],c[1][1]-c[0][1]) for c in it.combinations(l,2)]
    ans=N-max(dict(Counter(d)).values())

print(ans)

Buggy submission (verdict: Runtime Error):
from collections import Counter
import itertools as it

N=int(input())
l=[]
for i in range(N):
    l.append(tuple(map(int,input().split())))
l.sort()

d=[(c[1][0]-c[0][0],c[1][1]-c[0][1]) for c in it.combinations(l,2)]
ans=N-max(dict(Counter(d)).values())

print(ans)
Problem p03049 (python)

Fixed submission:
N=int(input())
ba=bx=xa=0
ans=0
for i in range(N):
    s=input()
    ans+=s.count('AB')
    if s.startswith('B') and s.endswith('A'):
        ba+=1
    elif s.startswith('B'):
        bx+=1
    elif s.endswith('A'):
        xa+=1

if ba==0:
    ans+=min(bx,xa)
elif ba>0:
    if bx+xa>0:
        ans+=ba+min(bx,xa)
    elif bx+xa==0:
        ans+=ba-1

print(ans)

Buggy submission (verdict: Runtime Error):
N=int(input())
ba=bx=xa=0
ans=0
for i in range(N):
    s=input()
    ans+=s.count('AB')
    if s.startswith('B') and s.endswith('A'):
        ba+=1
    elif s.startswith('B'):
        bx+=1
    elif s.endswith('A'):
        xa+=1

if ba==0:
    ans+=min(bx,xa)
elif ba>0:
    if bx+xa>0:
        ans+=ba+min(bx,xa)
    else bx+xa==0
        ans+=ba-1

print(ans)