Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
move=((1,0),(0,1),(-1,0),(0,-1))

#深さ優先探索
def dfs(used,mat,node,nums):
    y,x=node
    color=mat[y][x]
    if used[y][x]==1 or color=='!':
        return
    
    used[y][x]=1
    nums[color]+=1
    for i,j in move:
        if mat[y+i][x+j]!=color and mat[y+i][x+j] != '!':
            dfs(used,mat,(y+i,x+j),nums)


H,W=list(map(int,input().split()))

grid=['!'*(W+2)]
used=[[0 for _ in range(W+2)] for _ in range(H+2)]

for i in range(H):
    grid.append('!'+input()+'!')
grid.append('!'*(W+2))

ans=0
for i in range(H+2):
    for j in range(W+2):
        nums={'#':0,'.':0}
        dfs(used,grid,(i,j),nums)
        ans+=nums['#']*nums['.']
print(ans)

Fixed submission:
import sys
sys.setrecursionlimit(1000000)

move=((1,0),(0,1),(-1,0),(0,-1))

#深さ優先探索
def dfs(used,mat,node,nums):
    y,x=node
    color=mat[y][x]
    if used[y][x]==1 or color=='!':
        return
    
    used[y][x]=1
    nums[color]+=1
    for i,j in move:
        if mat[y+i][x+j]!=color and mat[y+i][x+j] != '!':
            dfs(used,mat,(y+i,x+j),nums)


H,W=list(map(int,input().split()))

grid=['!'*(W+2)]
used=[[0 for _ in range(W+2)] for _ in range(H+2)]

for i in range(H):
    grid.append('!'+input()+'!')
grid.append('!'*(W+2))

ans=0
for i in range(H+2):
    for j in range(W+2):
        nums={'#':0,'.':0}
        dfs(used,grid,(i,j),nums)
        ans+=nums['#']*nums['.']
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N,P=list(map(int,input().split()))

ans=1
if N==1:
    ans=P
else:
    for i in range(1,P):
        if P % (i**N)==0:
            ans=i
        elif P < i**N:
            break
print(ans)

Fixed submission:
N,P=list(map(int,input().split()))

ans=1
if N==1:
    ans=P
elif N>10**6:
    pass
else:
    for i in range(1,P):
        if P % (i**N)==0:
            ans=i
        elif P < i**N:
            break
print(ans)
Buggy python submission (verdict: Runtime Error):
print(int(input())+int(input())//2)

Fixed submission:
X,Y=list(map(int,input().split()))
print(X+Y//2)
Buggy python submission (verdict: Time Limit Exceeded):
def gcd(N,M):
    while True:
        if min(N,M)==0:
            return max(N,M)
        elif N>M:
            N%=M
        elif M>N:
            M%=N
def lcm(N,M):
    return N*M//gcd(N,M)

N,M=list(map(int,input().split()))
S=input()
T=input()

L=lcm(N,M)

for i in range((N*M)//L):
    if S[i*L//M]!=T[i*L//N]:
        L=-1
        break
print(L)

Fixed submission:
def gcd(N,M):
    while True:
        if min(N,M)==0:
            return max(N,M)
        elif N>=M:
            N%=M
        elif M>N:
            M%=N
def lcm(N,M):
    return N*M//gcd(N,M)

N,M=list(map(int,input().split()))
S=input()
T=input()

L=lcm(N,M)

for i in range((N*M)//L):
    if S[i*L//M]!=T[i*L//N]:
        L=-1
        break
print(L)
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

mod=10**9+7

def inverse(r):
    tmp=mod
    ks=[]
    while True:
        ks.append(tmp//r)
        tmp%=r
        t=tmp
        tmp=r
        r=t
        if r==0:
            break

    x=np.array([[1,0],[0,1]])
    for k in ks:
        x=np.array([[0,1],[1,-k]]).dot(x)

    if x[0,1]<0:
        return x[0,1]+mod
    else:
        return x[0,1]
        

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

fact=[1]
for i in range(1,N+1):
    fact.append(fact[i-1]*i%mod)

ifact=[0]
for i in range(1,N+1):
    ifact.append((ifact[i-1]+inverse(i))%mod)

ans=0
for j in range(1,N+1):
    ans+=(ifact[j]+ifact[N-j+1]-1)*A[j-1]
    ans%=mod
    

ans*=fact[N]
ans%=mod
print(ans)

Fixed submission:
import itertools
mod=10**9+7

def inverse(r,inv):
    return -(mod//i)*inv[mod%i]%mod
        

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

fact=[1,1]
inv=[0,1]
for i in range(2,N+1):
    fact.append(fact[i-1]*i%mod)
    inv.append(inverse(i,inv)%mod)
ifact = list(itertools.accumulate(inv))

ans=0
for j in range(1,N+1):
    ans+=(ifact[j]+ifact[N-j+1]-1)*A[j-1]*fact[N]%mod
    ans%=mod
    
ans%=mod
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N, X = [int(x) for x in input().split(' ')]

city = [abs(int(x) - X) for x in input().split(' ')]
max_fact = city[0]

for x in city:
    while x!=0:
        if x > max_fact:
            x -= max_fact
        elif x == max_fact:
            break
        else:
            tmp = max_fact
            max_fact = x
            x = tmp
    if max_fact==1:
        break

print(max_fact)

Fixed submission:
N, X = [int(x) for x in input().split(' ')]

city = [abs(int(x) - X) for x in input().split(' ')]
max_fact = city[0]

for x in city:
    while x!=0:
        x %= max_fact
        if x > 0:
            tmp = max_fact
            max_fact = x
            x = tmp
    if max_fact==1:
        break

print(max_fact)
Buggy python submission (verdict: Runtime Error):
H,W=list(map(int,input().split()))
A=[]
for i in range(H):
    A.append(list(map(int,input().split())))
sum_=0
ans=[]
for i,aline in enumerate(A):
    for j in range(W-1):
        if aline[j]%2==1:
            aline[j+1]+=1
            sum_+=1
            ans.append('{0} {1} {2} {3}'.format(i+1,j+1,i+1,j+2))
for i,aline in enumerate(A[:-1]):
    if aline[W-1]%2==1:
        A[i+1][W-1]+=1
        sum_+=1
        ans.append(('{0} {1} {2} {3}'.format(i+1,j+1,i+2,j+1)))
print(sum_)
for x in ans:
    print(x)

Fixed submission:
H,W=list(map(int,input().split()))
A=[]
for i in range(H):
    A.append(list(map(int,input().split())))
sum_=0
ans=[]
for i,aline in enumerate(A):
    for j in range(W-1):
        if aline[j]%2==1:
            aline[j+1]+=1
            sum_+=1
            ans.append('{0} {1} {2} {3}'.format(i+1,j+1,i+1,j+2))
for i,aline in enumerate(A[:-1]):
    if aline[W-1]%2==1:
        A[i+1][W-1]+=1
        sum_+=1
        ans.append(('{0} {1} {2} {3}'.format(i+1,W,i+2,W)))
print(sum_)
for x in ans:
    print(x)

Buggy python submission (verdict: Runtime Error):
H,W=list(map(int,input().split()))
A=[]
for i in range(H):
    A.append(list(map(int,input().split())))
sum_=0
ans=[]
for i,aline in enumerate(A):
    for j in range(W-1):
        if aline[j]%2==1:
            aline[j+1]+=1
            sum+=1
            ans.append('{0} {1} {2} {3}'.format(i+1,j+1,i+1,j+2))
for i,aline in enumerate(A[:-1]):
    if aline[W-1]%2==1:
        A[i+1][W-1]+=1
        sum_+=1
        ans.append(('{0} {1} {2} {3}'.format(i+1,j+1,i+2,j+1)))
print(sum_)
for x in ans:
    print(x)

Fixed submission:
H,W=list(map(int,input().split()))
A=[]
for i in range(H):
    A.append(list(map(int,input().split())))
sum_=0
ans=[]
for i,aline in enumerate(A):
    for j in range(W-1):
        if aline[j]%2==1:
            aline[j+1]+=1
            sum_+=1
            ans.append('{0} {1} {2} {3}'.format(i+1,j+1,i+1,j+2))
for i,aline in enumerate(A[:-1]):
    if aline[W-1]%2==1:
        A[i+1][W-1]+=1
        sum_+=1
        ans.append(('{0} {1} {2} {3}'.format(i+1,W,i+2,W)))
print(sum_)
for x in ans:
    print(x)

Buggy python submission (verdict: Runtime Error):
H,W=list(map(int,input().split()))
A=[]
for i in range(H):
    A.append(list(map(int,input().split())))

for i,aline in enumerate(A):
    for j in range(W-1):
        if aline[j]%2==1:
            aline[j+1]+=1
            print('{0} {1} {2} {3}'.format(i+1,j+1,i+1,j+2))
for i,aline in enumerate(A[:-1]):
    if aline[W-1]%2==1:
        A[i+1][W-1]+=1
        print('{0} {1} {2} {3}'.format(i+1,j+1,i+2,j+1))

Fixed submission:
H,W=list(map(int,input().split()))
A=[]
for i in range(H):
    A.append(list(map(int,input().split())))
sum_=0
ans=[]
for i,aline in enumerate(A):
    for j in range(W-1):
        if aline[j]%2==1:
            aline[j+1]+=1
            sum_+=1
            ans.append('{0} {1} {2} {3}'.format(i+1,j+1,i+1,j+2))
for i,aline in enumerate(A[:-1]):
    if aline[W-1]%2==1:
        A[i+1][W-1]+=1
        sum_+=1
        ans.append(('{0} {1} {2} {3}'.format(i+1,W,i+2,W)))
print(sum_)
for x in ans:
    print(x)
Buggy python submission (verdict: Runtime Error):
S=0
N=int(input())
tmp=N
while tmp!=0:
    S+=N%10
    tmp//=10
if N%S==0:
    print('Yes')
else:
    print('No')

Fixed submission:
S=0
N=int(input())
tmp=N
while tmp!=0:
    S+=tmp%10
    tmp//=10
if N%S==0:
    print('Yes')
else:
    print('No')
Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())

sum_=0
tmp=[0 for _ in range(20)]
A =list(map(int,input().split()))
i=0
biA=[[] for _ in range(N)]
ans=0
for j in range(N):
    k=0
    while A[j]!=0:
        biA[j].append(A[j]%2)
        A[j]//=2
        k+=1
    for k,x in enumerate(biA[j]):
        tmp[k]+=x
    while 2 in tmp:
        for k,x in enumerate(biA[i]):
            tmp[k]-=x
        i+=1
    ans+=j-i+1
print(ans)

Fixed submission:
N=int(input())

sum_=0
A =list(map(int,input().split()))
j=0
ans=0
sum_=0
for i in range(N):
    k=0
    while j<N and sum_+A[j]==sum_^A[j]:
        sum_+=A[j]
        ans+=j-i+1
        j+=1
    sum_-=A[i]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
N=int(input())

sum_=0
tmp=np.array([0 for _ in range(20)])
A =list(map(int,input().split()))
i=0
biA=np.array([[0 for _ in range(20)] for _ in range(N)])
ans=0
for j in range(N):
    k=0
    while A[j]!=0:
        biA[j][k]=A[j]%2
        A[j]//=2
        k+=1
    tmp+=biA[j]
    while 2 in tmp:
        tmp-=biA[i]
        i+=1
    ans+=j-i+1
print(ans)

Fixed submission:
N=int(input())

sum_=0
A =list(map(int,input().split()))
j=0
ans=0
sum_=0
for i in range(N):
    k=0
    while j<N and sum_+A[j]==sum_^A[j]:
        sum_+=A[j]
        ans+=j-i+1
        j+=1
    sum_-=A[i]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())

arr=[]
for x in map(int,input().split()):
    tmp=[]
    i=0
    while x!=0:
        if x%2==1:
            tmp.append(i)
        i+=1
        x//=2
    arr.append(tmp)

ans=0
for i in range(N):
    tmp=1
    tarr=arr[i]
    for k in range(i+1,N):
        for x in tarr:
            if x in arr[k]:
                tarr=-1
                break
        if tarr==-1:
            break
        else:
            tmp+=1
            tarr+=arr[k]
    ans+=tmp
print(ans)

Fixed submission:
N=int(input())

sum_=0
A =list(map(int,input().split()))
j=0
ans=0
sum_=0
for i in range(N):
    k=0
    while j<N and sum_+A[j]==sum_^A[j]:
        sum_+=A[j]
        ans+=j-i+1
        j+=1
    sum_-=A[i]
print(ans)
Buggy python submission (verdict: Runtime Error):
arr = [0 for _ in range(1001)]
arr[1] = 1
for i in range(2,32):
    if arr[i]==1:
        continue
    tmp=i
    while i <=1000:
        arr[tmp]=1
        tmp*=i

for x in range(int(input()),0,-1):
    if arr[x]==1:
        print(x)
        break

Fixed submission:
arr = [0 for _ in range(1001)]
arr[1] = 1
for i in range(2,32):
    if arr[i]==1:
        continue
    tmp=i*i
    while tmp <=1000:
        arr[tmp]=1
        tmp*=i

for x in range(int(input()),0,-1):
    if arr[x]==1:
        print(x)
        break
Buggy python submission (verdict: Time Limit Exceeded):
s=input()
K=int(input())

dic={}
for i in range(len(s)):
    dic[s[i:]]=1
arr=sorted(dic)
dic={}
for x in arr:
    for i in range(0,len(x)):
        tmp = x[:i+1]
        if dic.get(tmp,0)==0:
            K-=1
            dic[tmp]=1
        else:
            continue
        if K==0:
            print(x[:i+1])
            break

Fixed submission:
s=input()
K=int(input())

dic={}
for i in range(len(s)):
    dic[s[i:]]=1
arr=sorted(dic)
dic={}

for x in arr:
    for i in range(0,len(x)):
        tmp = x[:i+1]
        if dic.get(tmp,0)==0:
            K-=1
            dic[tmp]=1
        else:
            continue
        if K==0:
            print(x[:i+1])
            break
    if K<0:
        break

Buggy python submission (verdict: Time Limit Exceeded):
s=input()
K=int(input())

dic={}
for i in range(len(s)):
    dic[s[i:]]=1
arr=sorted(dic)
dic={}
for x in arr:
    for i in range(0,len(x)):
        tmp = x[:i+1]
        if dic.get(tmp,0)==0:
            K-=1
            dic[tmp]=1
        else:
            continue
        if K==0:
            print(x[:i+1])

Fixed submission:
s=input()
K=int(input())

dic={}
for i in range(len(s)):
    dic[s[i:]]=1
arr=sorted(dic)
dic={}

for x in arr:
    for i in range(0,len(x)):
        tmp = x[:i+1]
        if dic.get(tmp,0)==0:
            K-=1
            dic[tmp]=1
        else:
            continue
        if K==0:
            print(x[:i+1])
            break
    if K<0:
        break
Buggy python submission (verdict: Runtime Error):
K=int(input())

print(A+B+C+(2**K-1)*max(A,B,C))

Fixed submission:
A,B,C=list(map(int,input().split(' ')))
K=int(input())

print(A+B+C+(2**K-1)*max(A,B,C))

Buggy python submission (verdict: Runtime Error):
A,B,C=list(map(int,input()))
K=int(input())

print(A+B+C+(2**K-1)*max(A,B,C))

Fixed submission:
A,B,C=list(map(int,input().split(' ')))
K=int(input())

print(A+B+C+(2**K-1)*max(A,B,C))
Buggy python submission (verdict: Runtime Error):
C *= 2
ans = 0

if A >= C and B >= C:
    ans += max(X, Y) * C
elif C >= A+B:
    ans += A*X + B*Y
elif C >= max(A,B):
    if X>Y:
        ans += Y*C + (X-Y)*A
    else:
        ans += X*C + (Y-X)*B
else:
    if A >= C:
        ans += X*C
        if Y-X>0:
            ans+=Y*B
    else:
        ans += Y*C
        if X-Y:
            ans+=X*A

print(ans)

Fixed submission:
A, B, C, X, Y = list(map(int, input().split()))
C *= 2
ans = 0

if A >= C and B >= C:
    ans += max(X, Y) * C
elif C >= A+B:
    ans += A*X + B*Y
elif C >= max(A,B):
    if X>Y:
        ans += Y*C + (X-Y)*A
    else:
        ans += X*C + (Y-X)*B
else:
    if A >= C:
        ans += X*C
        if Y-X>0:
            ans+=(Y-X)*B
    else:
        ans += Y*C
        if X-Y>0:
            ans+=(X-Y)*A

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
N, C = list(map(int,input().split()))

sushi = [[0,0]]
for _ in range(N):
    sushi.append(list(map(int,input().split())))
sushi.append([C,0])
sushi = np.array(sushi)

sum_c = 0
sum_d = 0
ans = 0
for f in range(N+1):
    sum_c += sushi[f,1]
    sum_d = sushi[f,0]
    tmp_c = sum_c
    tmp_d = sum_d
    for b in range(1,N+2-f):
        tmp_c += sushi[-b,1]
        tmp_d = min(sum_d, C-sushi[-b,0])*2 + max(sum_d, C-sushi[-b,0])
        if ans < tmp_c-tmp_d:
            ans = tmp_c-tmp_d

print(ans)

Fixed submission:
N, C = list(map(int,input().split()))

sushi_front = [0]
sushi_back = [0]
sushi_front2 = [0]
sushi_back2 = [0]
sushi = []

c= 0
d = 0
max_ = 0
max_2 = 0
for _ in range(N):
    sushi.append(list(map(int,input().split())))
    
    c += sushi[-1][1]
    d = sushi[-1][0]
    max_ = max(max_,c-d)
    max_2 = max(max_2, c-2*d)
    sushi_front.append(max_)
    sushi_front2.append(max_2)

sushi.reverse()
c = 0
d = 0
max_ = 0
max_2 = 0
for x in sushi:
    
    c += x[1]
    d = C -x[0]
    max_ = max(max_,c-d)
    max_2 = max(max_2, c-2*d)
    sushi_back.append(max_)
    sushi_back2.append(max_2)

ans = 0
for i in range(N+1):
    ans = max(ans,sushi_front[i]+sushi_back2[N-i],sushi_front2[i]+sushi_back[N-i])
print(ans)
Buggy python submission (verdict: Runtime Error):
ans = ''
for i in range(3):
    ans += input().split()[i]

print(ans)

Fixed submission:
ans = ''
for i in range(3):
    ans += input()[i]

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
Q = int(input())

p = np.array([i for i in range(2*10**5+1)])
p[1]=0
for i in range(2,10**5):
    p[2*i::i] = 0
p = p[p!=0]

dic = {i:1 for i in p}
a = []
for i in range(len(p)):
    if dic.get((p[i]+1)//2,0)==1:
        a.append(p[i])
p=np.array(a)
for _ in range(Q):
    l,r = list(map(int,input().split()))
    tmp = p[l<=p]
    tmp = tmp[tmp<=r]
    print(len(tmp))

Fixed submission:
import numpy as np
Q = int(input())

p = np.array([i for i in range(2*10**5+1)])
p[1]=0
for i in range(2,10**5):
    p[2*i::i] = 0
p = p[p!=0]

dic = {i:1 for i in p}
d = {}
for i in range(len(p)):
    if dic.get((p[i]+1)//2,0)==1:
        d[p[i]] = 1
sum_=0
arr = [0 for i in range(10**5+2)]
for i in range(10**5+2):
    arr[i] = sum_
    if d.get(i,0)==1:
        sum_+=1
for _ in range(Q):
    l,r = list(map(int,input().split()))
    print(arr[r+1]-arr[l])

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
Q = int(input())

p = np.array([i for i in range(2*10**5+1)])
p[1]=0
for i in range(2,10**5):
    p[2*i::i] = 0
p = p[p!=0]

dic = {i:1 for i in p}

for _ in range(Q):
    l,r = list(map(int,input().split()))
    tmp = p[l<=p]
    tmp = tmp[tmp<=r]
    print([dic.get((x+1)//2,0)==1 for x in tmp].count(True))

Fixed submission:
import numpy as np
Q = int(input())

p = np.array([i for i in range(2*10**5+1)])
p[1]=0
for i in range(2,10**5):
    p[2*i::i] = 0
p = p[p!=0]

dic = {i:1 for i in p}
d = {}
for i in range(len(p)):
    if dic.get((p[i]+1)//2,0)==1:
        d[p[i]] = 1
sum_=0
arr = [0 for i in range(10**5+2)]
for i in range(10**5+2):
    arr[i] = sum_
    if d.get(i,0)==1:
        sum_+=1
for _ in range(Q):
    l,r = list(map(int,input().split()))
    print(arr[r+1]-arr[l])

Buggy python submission (verdict: Runtime Error):
import numpy as np
Q = int(input())

p = np.array([i for i in range(2*10**5+1)])
p[1]=0
for i in range(2,10**5):
    p[2*i::i] = 0
p = p[p!=0]

dic = {i:1 for i in p}

for _ in range(Q):
    l,r = list(map(int,input().split()))
    tmp = p[l<=p]
    tmp = tmp[tmp<=r]
    print(len(tmp[[dic.get((x+1)//2,0)==1 for x in tmp]]))
    

Fixed submission:
import numpy as np
Q = int(input())

p = np.array([i for i in range(2*10**5+1)])
p[1]=0
for i in range(2,10**5):
    p[2*i::i] = 0
p = p[p!=0]

dic = {i:1 for i in p}
d = {}
for i in range(len(p)):
    if dic.get((p[i]+1)//2,0)==1:
        d[p[i]] = 1
sum_=0
arr = [0 for i in range(10**5+2)]
for i in range(10**5+2):
    arr[i] = sum_
    if d.get(i,0)==1:
        sum_+=1
for _ in range(Q):
    l,r = list(map(int,input().split()))
    print(arr[r+1]-arr[l])
Buggy python submission (verdict: Runtime Error):
def sum_(a):
    ans=0
    while a!=0:
        ans+=a%10
        a//=10
    return ans

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

arr=[0 for i in range(36)]
for i in range(1,N+1):
    arr[sum_(i)]+=i

print(sum(arr[A:B+1]))

Fixed submission:
def sum_(a):
    ans=0
    while a!=0:
        ans+=a%10
        a//=10
    return ans

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

arr=[0 for i in range(37)]
for i in range(1,N+1):
    arr[sum_(i)]+=i

print(sum(arr[A:B+1]))
Buggy python submission (verdict: Runtime Error):
A,B,C=list(map(int,input()))
if A==B:
    print(C)
elif B==C:
    print(A)
else:
    print(B)

Fixed submission:
A,B,C=list(map(int,input().split()))
if A==B:
    print(C)
elif B==C:
    print(A)
else:
    print(B)
Buggy python submission (verdict: Runtime Error):
import numpy as np

def search(now,r,dist,ans):
    if len(r)==0:
        return ans
    min_=float('inf')
    for i,x in enumerate(r):
        if i < len(r)-1:
            min_=min(min_,search(x,r[:i]+r[i+1:],dist,ans+dist[now][x]))
        else:
            min_=min(min_, search(x,r[:i],dist,ans+dist[now][x]))
    return min_

N,M,R=list(map(int,input().split()))
r=list(map(int,input().split()))
r=[x-1 for x in r]
dist=[[float('inf') for _ in range(N)] for _ in range(N)]
for i in range(M):
    A,B,C=list(map(int,input().split()))
    dist[A-1][B-1]=C
    dist[B-1][A-1]=C
for i in range(N):
    dist[i][i]=0
dist=np.array(dist)

ans=float('inf')
for i in range(N):
    for j in range(N):
        dist[i][j]=min(dist[i]+dist[j])

ans=float('inf')
for i,x in enumerate(r):
    if i < len(r)-1:
        ans=min(ans, search(x,r[:i]+r[i+1:],dist,0))
    else:
        ans=min(ans, search(x,r[:i],dist,0))
print(ans//1)

Fixed submission:
N=int(input())
ans=0
dic={}
for i in range(N):
    a=int(input())
    if dic.get(a,0)==0:
        ans+=1
        dic[a]=1
    else:
        ans-=1
        dic[a]=0
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
buttun=[0]
for i in range(N):
    buttun.append(int(input()))

light=1
ans=0
while True:
    if light==2:
        break
    elif light==buttun[light]:
        ans=-1
        break
    else:
        light=buttun[light]
        ans+=1
    if light==1:
        ans=-1
        break
print(ans)

Fixed submission:
N=int(input())
buttun=[0]
for i in range(N):
    buttun.append(int(input()))

light=1
ans=0
while True:
    if light==2:
        break
    elif light==buttun[light]:
        ans=-1
        break
    else:
        tmp=light
        light=buttun[light]
        ans+=1
        buttun[tmp]=1
    if light==1:
        ans=-1
        break
print(ans)
Buggy python submission (verdict: Runtime Error):
import heapq

def add(i,pj,T):
    if T[i]['p']!=i:
        add(T[i]['p'],pj,T)
    T[i]['p']=pj
    
def parent(i,T):
    if T[i]['p']!=i:
        T[i]['p']= parent(T[i]['p'],T)
        return T[i]['p']
    else:
        return i

N=int(input())
town=[]
for i in range(N):
    x,y=list(map(int,input().split()))
    town.append([i,x,y])
townX=sorted(town,key=lambda x:x[1])
townY=sorted(town,key=lambda x:x[2])
road=[]
for i in range(1,N):
    heapq.heappush(road,[townX[i][1]-townX[i-1][1],townX[i-1][0],townX[i][0]])
for i in range(1,N):
    heapq.heappush(road,[townY[i][2]-townY[i-1][2],townY[i-1][0],townY[i][0]])

vi=0
ans=0
tree={i:{'p':i,'h':0} for i in range(N)}
while vi<N-1:
    d,u,v = heapq.heappop(road)
    pu=parent(u,tree)
    pv=parent(v,tree)
    if pu == pv:
        continue
    elif tree[pu]['h']<tree[pv]['h']:
        add(u,pv,tree)
    elif tree[pu]['h']==tree[pv]['h']:
        add(u,pv,tree)
        tree[pu]['h']+=1
    else:
        add(v,pu,tree)
    vi+=1
    ans+=d
print(ans)

Fixed submission:
import heapq
import sys

sys.setrecursionlimit(10**5)

def add(i,pj,T):
    if T[i]!=i:
        add(T[i],pj,T)
    T[i]=pj
    
def parent(i,T):
    if T[i]!=i:
        T[i]= parent(T[i],T)
        return T[i]
    else:
        return i

N=int(input())
town=[]
for i in range(N):
    x,y=list(map(int,input().split()))
    town.append([i,x,y])
townX=sorted(town,key=lambda x:x[1])
townY=sorted(town,key=lambda x:x[2])
road=[]
for i in range(1,N):
    heapq.heappush(road,[townX[i][1]-townX[i-1][1],townX[i-1][0],townX[i][0]])
for i in range(1,N):
    heapq.heappush(road,[townY[i][2]-townY[i-1][2],townY[i-1][0],townY[i][0]])

vi=0
ans=0
tree=[i for i in range(N)]
rank=[0 for i in range(N)]
while vi<N-1:
    d,u,v = heapq.heappop(road)
    pu=parent(u,tree)
    pv=parent(v,tree)
    ru=rank[pu]
    rv=rank[pv]
    if pu == pv:
        continue
    elif ru<rv:
        add(u,pv,tree)
    elif ru==rv:
        add(u,pv,tree)
        rank[pu]+=1
    else:
        add(v,pu,tree)
    vi+=1
    ans+=d
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N,Ma,Mb=list(map(int,input().split()))

mix=set([(0,0,0)])
rate=Ma/Mb
ans=10**9
for i in range(N):
    a,b,c=list(map(int,input().split()))
    tmp=mix.copy()
    for x in tmp:
        d=x[0]+a
        e=x[1]+b
        f=x[2]+c
        mix.add((d, e, f))
        if d/e==Ma/Mb and f<ans:
            ans=f
if ans==10**9:
    ans=-1
print(ans)

Fixed submission:
N,Ma,Mb=list(map(int,input().split()))

mix=set([(0,0)])
ans=10**9
cost={(0,0):0}
for i in range(N):
    a,b,c=list(map(int,input().split()))
    tmp=mix.copy()
    tmp2=cost.copy()
    for x in tmp:
        d=x[0]+a
        e=x[1]+b
        f=min(tmp2.get((d,e),10**9),tmp2[x]+c)
        cost[(d,e)]=f
        mix.add((d, e))
        if d*Mb==e*Ma and f<ans:
            ans=f
"""
print(cost)
print(mix)
"""
if ans==10**9:
    ans=-1
print(ans)