Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=[]
for i in range(n):
  a.append(int(input())-1)
i=0

out=0

while i!=1:
  out+=1
  i=a[i]
  if i>n:
    out=-1
    break

print(out)


Fixed submission:
n=int(input())
a=[]
for i in range(n):
  a.append(int(input())-1)
i=0

out=0
kai=0
while i!=1:
  kai+=1
  out+=1
  i=a[i]
  if kai>n:
    out=-1
    break

print(out)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=[]
for i in range(n):
  a.append(int(input())-1)
i=0
seen=[]
out=0
if 1 not in a:
  out=-1
  i=1
while i!=1:
  out+=1
  i=a[i]
  if i in seen:
    out=-1
    break
  seen.append(i)
print(out)


Fixed submission:
n=int(input())
a=[]
for i in range(n):
  a.append(int(input())-1)
i=0

out=0
kai=0
while i!=1:
  kai+=1
  out+=1
  i=a[i]
  if kai>n:
    out=-1
    break

print(out)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=[]
for i in range(n):
  a.append(int(input())-1)
i=0
seen=[]
out=0
if 1 not in a:
  out=-1
  i=1
while i!=1:
  out+=1
  i=a[i]
  if i in seen:
    out=-1
    break
  seen.append(i)
print(out)


Fixed submission:
n=int(input())
a=[]
for i in range(n):
  a.append(int(input())-1)
i=0

out=0
kai=0
while i!=1:
  kai+=1
  out+=1
  i=a[i]
  if kai>n:
    out=-1
    break

print(out)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=[]
for i in range(n):
  a.append(int(input())-1)
i=0
seen=[]
out=0
while i!=1:
  out+=1
  i=a[i]
  if i in seen:
    out=-1
    break
  seen.append(i)
print(out)


Fixed submission:
n=int(input())
a=[]
for i in range(n):
  a.append(int(input())-1)
i=0

out=0
kai=0
while i!=1:
  kai+=1
  out+=1
  i=a[i]
  if kai>n:
    out=-1
    break

print(out)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=[]
for i in range(n):
  a.append(int(input())-1)
i=0
seen=[]
out=0
while i!=1:
  out+=1
  i=a[i]
  if i in seen:
    out=-1
    break
  seen.append(i)
print(out)

Fixed submission:
n=int(input())
a=[]
for i in range(n):
  a.append(int(input())-1)
i=0

out=0
kai=0
while i!=1:
  kai+=1
  out+=1
  i=a[i]
  if kai>n:
    out=-1
    break

print(out)
Buggy python submission (verdict: Runtime Error):
n=int(input())
a=list(map(int,input().split()))
c=[0]*9

for r in a:
  x=r//400
  if x>8:
    x==8
  c[x]+=1
ans=0
for i in range(9):
  if i==8:
    if ans==0:
      mn=1
      mx=n
    else:
      mn=ans
      mx=ans+c[i]
  if c[i]!=0:
    ans+=1

print("{} {}".format(mn,mx))

Fixed submission:
n=int(input())
a=list(map(int,input().split()))
c=[0]*9

for r in a:
  x=r//400
  if x>8:
    x=8
  c[x]+=1
ans=0
for i in range(9):
  if i==8:
    if ans==0:
      mn=1
      mx=n
    else:
      mn=ans
      mx=ans+c[i]
  if c[i]!=0:
    ans+=1

print("{} {}".format(mn,mx))
Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
li=[]
for i in range(n):
  li.append(int(input()))
pl=[0]
for i in range(2**n):
  point=0
  for j in range(n):
    if ((i>>j)&1):
      point+=li[j]
  if point%10!=0:
    pl.append(point)
pl.sort(reverse=True)
for i in pl:
  if i%10!=0:
    print(i)
    break
else:
  print(0)
    

Fixed submission:
n=int(input())
li=[]
for i in range(n):
  li.append(int(input()))
out=sum(li)
if out%10!=0:
  print(out)
else:
  li.sort()
  for i in li:
    if i%10!=0:
      print(out-i)
      break
  else:
    print(0)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
li=[]
for i in range(n):
  li.append(int(input()))
pl=[]
for i in range(2**n):
  point=0
  for j in range(n):
    if ((i>>j)&1):
      point+=li[j]
  pl.append(point)
pl.sort(reverse=True)
for i in pl:
  if i%10!=0:
    print(i)
    break
else:
  print(0)

Fixed submission:
n=int(input())
li=[]
for i in range(n):
  li.append(int(input()))
out=sum(li)
if out%10!=0:
  print(out)
else:
  li.sort()
  for i in li:
    if i%10!=0:
      print(out-i)
      break
  else:
    print(0)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
li=[]
for i in range(n):
  li.append(int(input()))
pl=[]
for i in range(2**n):
  point=0
  for j in range(n):
    if ((i>>j)&1):
      point+=li[j]
  pl.append(point)
pl.sort(reverse=True)
for i in pl:
  if i%10!=0:
    print(i)
    break
else:
  print(0)

Fixed submission:
n=int(input())
li=[]
for i in range(n):
  li.append(int(input()))
out=sum(li)
if out%10!=0:
  print(out)
else:
  li.sort()
  for i in li:
    if i%10!=0:
      print(out-i)
      break
  else:
    print(0)
Buggy python submission (verdict: Runtime Error):
x=sum(list(map(int,input.split())))
if x>=24:
  x-=24
print(x)

Fixed submission:
x=sum(list(map(int,input().split())))
if x>=24:
  x-=24
print(x)
Buggy python submission (verdict: Time Limit Exceeded):
k,s=map(int,input().split())
ans=0
for x in range(k+1):
  for y in range(x,k+1):
    for z in range(y,k+1):
      if x+y+z==s:
        if x==y and y==z:
          ans+=1
        elif x==y or y==z or x==z:
          ans+=3
        else:
          ans+=6
print(ans)


Fixed submission:
k,s=map(int,input().split())
ans=0
for x in range(k+1):
  for y in range(x,k+1):
    z=s-(x+y)
    if z>k or z<y:
      continue
    if x==y and y==z:
      ans+=1
    elif x==y or y==z or x==z:
      ans+=3
    else:
      ans+=6
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
k,s=map(int,input().split())
ans=0
for x in range(k+1):
  for y in range(k+1):
    for z in range(k+1):
      if x+y+z==s:
        ans+=1
print(ans)


Fixed submission:
k,s=map(int,input().split())
ans=0
for x in range(k+1):
  for y in range(x,k+1):
    z=s-(x+y)
    if z>k or z<y:
      continue
    if x==y and y==z:
      ans+=1
    elif x==y or y==z or x==z:
      ans+=3
    else:
      ans+=6
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
k,s=map(int,input().split())
ans=0
for x in range(k+1):
  for y in range(k+1):
    for z in range(k+1):
      if x+y+z==s:
        ans+=1
print(ans)

Fixed submission:
k,s=map(int,input().split())
ans=0
for x in range(k+1):
  for y in range(x,k+1):
    z=s-(x+y)
    if z>k or z<y:
      continue
    if x==y and y==z:
      ans+=1
    elif x==y or y==z or x==z:
      ans+=3
    else:
      ans+=6
print(ans)
Buggy python submission (verdict: Runtime Error):
a,op,b=imput().split()
a=int(a)
b=int(b)
if op=="+":
  print(a+b)
else:
  print(a-b)

Fixed submission:
a,op,b=input().split()
a=int(a)
b=int(b)
if op=="+":
  print(a+b)
else:
  print(a-b)
Buggy python submission (verdict: Runtime Error):
lis=[]
ll=["a","b","c"]
for i in range(3):
  lis.append(list(input()))
turn=0
while 1:
  x=ll.index(lis[turn].pop())
  if lis[turn]=[]:
    print(ll[turn].upper())
    break
  turn=x

Fixed submission:
lis=[]
ll=["a","b","c"]
for i in range(3):
  lis.append(list(input()))
turn=0
while 1:
  if lis[turn]==[]:
    print(ll[turn].upper())
    break
  turn=ll.index(lis[turn].pop(0))
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
N=int(input())
A=list(map(int,input().split()))
# cnt=[0]*(10**6+1)
cnt=np.zeros(10**6+1, dtype=np.int32)
for a in A:cnt[a]+=1
 
if np.gcd.reduce(A)!=1:
  print("not coprime")
elif np.all([sum(cnt[i::i])<=1 for i in range(2,10**6+1)]):
  print("pairwise coprime")
else:
  print("setwise coprime")

Fixed submission:
from math import gcd
def prime_table(n):
    is_prime = [True] * (n + 1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5) + 1):
        if not is_prime[i]:
            continue
        for j in range(i * 2, n + 1, i):
            is_prime[j] = False
    return [i for i in range(n + 1) if is_prime[i]]

N = int(input())
A = list(map(int,input().split()))
g = A[0]
for i in range(1,N):
    g = gcd(g,A[i])
    if g == 1:
        break
if g != 1:
    print('not coprime')
else:
    PT = prime_table(10**6+1)
    l = len(PT)
    flag = set()
    for j in range(N):
        n = A[j]
        for i in range(l):
            p = PT[i]
            if n % p == 0:
                while n % p == 0:
                    n //= p
                if p in flag:
                    print('setwise coprime')
                    exit()
                else:
                    flag.add(p)
            if p*p > n:
                if n in flag and n != 1:
                    print('setwise coprime')
                    exit()
                else:
                    if n != 1:
                        flag.add(n)
                break
    print('pairwise coprime')
Buggy python submission (verdict: Runtime Error):
from collections import defaultdict
H,W,M = map(int,input().split())
bomb = [[0]*(W+1) for _ in range(H+1)]
hdic = defaultdict(int)
wdic = defaultdict(int)
for i in range(M):
    hi,wi = map(int,input().split())
    hdic[hi] += 1
    wdic[wi] += 1
    bomb[hi][wi] = 1
hmax = 0
hlist = []
wmax = 0
wlist = []
for i in hdic.keys():
    if hdic[i] > hmax:
        hlist = [i]
        hmax = hdic[i]
    elif hdic[i] == hmax:
        hlist.append(i)
for i in wdic.keys():
    if wdic[i] > wmax:
        wlist = [i]
        wmax = wdic[i]
    elif wdic[i] == wmax:
        wlist.append(i)
for i in hlist:
    for j in wlist:
        if bomb[i][j] == 0:
            print(hmax+wmax)
            exit()
print(hmax+wmax-1)

Fixed submission:
from collections import defaultdict
H,W,M = map(int,input().split())
bomb = [set() for _ in range(H+1)]
hdic = [0]*(H+1)
wdic = [0]*(W+1)
for i in range(M):
    hi,wi = map(int,input().split())
    hdic[hi] += 1
    wdic[wi] += 1
    bomb[hi].add(wi)
hmax = 0
hlist = []
wmax = 0
wlist = []
for i in range(1,H+1):
    if hdic[i] > hmax:
        hlist = [i]
        hmax = hdic[i]
    elif hdic[i] == hmax:
        hlist.append(i)
for i in range(1,W+1):
    if wdic[i] > wmax:
        wlist = [i]
        wmax = wdic[i]
    elif wdic[i] == wmax:
        wlist.append(i)
for i in hlist:
    for j in wlist:
        if j not in bomb[i]:
            print(hmax+wmax)
            exit()
print(hmax+wmax-1)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
N = int(input())
Slist = []
for i in range(N):
    S = input()
    Slist.append(S)
Slist.sort(key=len)
dic = defaultdict(lambda:defaultdict(int))
ans = 0
for S in Slist:
    alp = set()
    letter = S
    for i in range(len(S)):
        alp.add(letter[0])
        letter = letter[1:]
        alpdic = dic[letter]
        for X in alpdic.keys():
            if X in alp:
                ans += alpdic[X]
    dic[S[1:]][S[0]] += 1
print(ans)

Fixed submission:
from collections import defaultdict
class RollingHash():
    def __init__(self, s, base, mod):
        self.mod = mod
        self.pw = pw = [1]*(len(s)+1)

        l = len(s)
        self.h = h = [0]*(l+1)

        v = 0
        for i in range(l):
            h[i+1] = v = (v * base + ord(s[i])) % mod
        v = 1
        for i in range(l):
            pw[i+1] = v = v * base % mod
    def get(self, l, r):
        return (self.h[r] - self.h[l] * self.pw[r-l]) % self.mod

N = int(input())
Slist = []
for i in range(N):
    S = input()
    Slist.append(S)
Slist.sort(key=len)
dic = defaultdict(lambda:defaultdict(int))
ans = 0
for S in Slist:
    alp = set()
    l = len(S)
    RH = RollingHash(S,37,10**18+9)
    for i in range(len(S)):
        alp.add(S[i])
        alpdic = dic[RH.get(i+1,l)]
        for X in alpdic.keys():
            if X in alp:
                ans += alpdic[X]
    dic[RH.get(1,l)][S[0]] += 1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
K = int(input())
cur = 7
count = 1
digit = 10
if K == 2 or K == 5 or K == 10:
    print("-1")
else:
    while cur % K != 0:
        count += 1
        cur += 7*digit
        digit = (digit*10)%K
    print(count)

Fixed submission:
K = int(input())
cur = 7
count = 1
digit = 10
X = K
if X % 2 == 0 or X % 5 == 0 :
    print(-1)
else:
    while cur % K != 0:
        count += 1
        cur += 7*digit
        digit = (digit*10)%K
    print(count)
Buggy python submission (verdict: Runtime Error):
N = int(input())
X = list(input())
P = X.count("1")
P1 = 0
P2 = 0
for i in range(N):
    if X[i] == "1":
        P1 += pow(2,N-i-1,P-1)
        P1 %= P-1
        P2 += pow(2,N-i-1,P+1)
        P2 %= P+1
for i in range(N):
    count = 1
    if X[i] == "0":
        cur = P2+pow(2,N-i-1,P+1)
        cur %= P+1
    else:
        cur = P1-pow(2,N-1-i,P-1)
        cur %= P-1
    while cur > 0:
        mod = bin(cur).count("1")
        cur %= mod
        count += 1
    print(count)

Fixed submission:
N = int(input())
X = list(input())
P = X.count("1")
if P != 1:
    P1 = 0
    P2 = 0
    for i in range(N):
        if X[i] == "1":
            P1 += pow(2,N-i-1,P-1)
            P1 %= P-1
            P2 += pow(2,N-i-1,P+1)
            P2 %= P+1
    for i in range(N):
        count = 1
        if X[i] == "0":
            cur = P2+pow(2,N-i-1,P+1)
            cur %= P+1
        else:
            cur = P1-pow(2,N-1-i,P-1)
            cur %= P-1
        while cur > 0:
            mod = bin(cur).count("1")
            cur %= mod
            count += 1
        print(count)
else:
    P2 = 0
    for i in range(N):
        if X[i] == "1":
            P2 += pow(2,N-i-1,P+1)
    for i in range(N):
        count = 1
        if X[i] =="1":
            print(0)
        else:
            cur = P2+pow(2,N-i-1,P+1)
            cur %= P+1
            while cur > 0:
                mod = bin(cur).count("1")
                cur %= mod
                count += 1
            print(count)
Buggy python submission (verdict: Runtime Error):
mod = 10**9+7
N,K = map(int,input().split())
A = list(map(int,input().split()))
canPositive = 0
Positive = 0
ans = 1
def inv(arg):
    return pow(arg,mod-2,mod)
for i in range(N):
    if A[i] >= 0:
        Positive += 1
if N - K >= 1 and Positive >= 1:
    canPositive = 1
if Positive == 0 and K % 2 == 0:
    canPositive = 1
anslist = []
if canPositive:
    A.sort(key=lambda x:abs(x),reverse=True)
    n = 0
    for a in A[:K]:
        anslist.append(a)
        if a < 0:
           n += 1
    if n % 2 == 1:
        minpos = 0
        minneg = 0
        maxpos = 0
        minneg = 0
        for i in anslist[::-1]:
            if i > 0:
                minpos = i
                break
        for i in anslist[::-1]:
            if i < 0:
                minneg = i
                break
        for i in A[K:]:
            if i < 0:
                maxneg = i
                break
        for i in A[K:]:
            if i > 0:
                maxpos = i
                break
        if abs(minpos*maxpos) > abs(maxneg*minneg):
            ans *= maxpos*inv(minneg)
        else:
            ans *= maxneg*inv(minpos)
else:
    A.sort(key=lambda x:abs(x))
    for a in A[:K]:
        anslist.append(a)
for i in anslist:
    ans *= i
    ans %= mod
print(ans)

Fixed submission:
mod = 10**9+7
N,K = map(int,input().split())
A = list(map(int,input().split()))
canPositive = 0
Positive = 0
ans = 1
def inv(arg):
    return pow(arg,mod-2,mod)
for i in range(N):
    if A[i] >= 0:
        Positive += 1
if N - K >= 1 and Positive >= 1:
    canPositive = 1
if Positive == 0 and K % 2 == 0:
    canPositive = 1
anslist = []
if canPositive:
    A.sort(key=lambda x:abs(x),reverse=True)
    n = 0
    for a in A[:K]:
        anslist.append(a)
        if a < 0:
           n += 1
    if n % 2 == 1:
        minpos = 0
        minneg = 0
        maxpos = 0
        maxneg = 0
        for i in anslist[::-1]:
            if i > 0:
                minpos = i
                break
        for i in anslist[::-1]:
            if i < 0:
                minneg = i
                break
        for i in A[K:]:
            if i < 0:
                maxneg = i
                break
        for i in A[K:]:
            if i > 0:
                maxpos = i
                break
        if maxneg == 0 or minpos == 0:
            ans *= maxpos*inv(minneg) 
        elif maxpos == 0 or minneg == 0:
            ans *= maxneg*inv(minpos) 
        else:
            if abs(minpos*maxpos) > abs(maxneg*minneg):
                ans *= maxpos*inv(minneg) 
            else:
                ans *= maxneg*inv(minpos) 
else:
    A.sort(key=lambda x:abs(x))
    for a in A[:K]:
        anslist.append(a)
for i in anslist:
    ans *= i
    ans %= mod
print(ans)
Buggy python submission (verdict: Runtime Error):
from collections import defaultdict
import heapq as hq
d = int(input())
c = list(map(int,input().split()))
s = [0]
for i in range(d):
    s.append([0]+list(map(int,input().split())))
dic = defaultdict(int)
contest = [[] for _ in range(27)]
contests = [-1]*(d+1)
for i in range(1,d+1):
    maxscore = 0
    maxcontest = -1
    for k in range(1,27):
        score = 0
        score += s[i][k]
        score += c[k]*(i-dic[k])
        if score > maxscore:
            maxscore = score
            maxcontest = k
    print(maxcontest)
    dic[maxcontest] = i


Fixed submission:
from collections import defaultdict
d = int(input())
c = list(map(int,input().split()))
s = []
for i in range(d):
    s.append(list(map(int,input().split())))
dic = defaultdict(int)
for i in range(1,d+1):
    maxscore = -10000000
    maxcontest = -1
    for k in range(26):
        score = 0
        score += s[i-1][k-1]
        for j in range(26):
            if j != k:
                score -= c[j]*(i-dic[j])
        if score > maxscore:
            maxscore = score
            maxcontest = k
    print(maxcontest+1)
    dic[maxcontest] = i
Buggy python submission (verdict: Runtime Error):
n = int(input())
a = list(map(int,input().split()))
x = 0
for i in range(2,n):
    x ^= a[i]
s = a[0]+a[1]
if s < x or (s-x) % 2 == 1:
    print(-1)
    exit()
d = (s-x)//2
if d & x != 0 or d > a[0]:
    print(-1)
    exit()
i = 50
ans = d
while i >= 0:
    if x >> i & 1:
        if 1 << i + ans <= a:
            ans += a << i
    i -= 1
print(a[0]-ans)

Fixed submission:
n = int(input())
a = list(map(int,input().split()))
x = 0
for i in range(2,n):
    x ^= a[i]
s = a[0]+a[1]
if s < x or (s-x) % 2 == 1:
    print(-1)
    exit()
d = (s-x)//2
if d & x != 0 or d > a[0]:
    print(-1)
    exit()
i = 50
ans = d
while i >= 0:
    if x >> i & 1:
        if (1 << i) + ans <= a[0]:
            ans += 1 << i
    i -= 1
if ans == 0:
    print(-1)
    exit()
print(a[0]-ans)
Buggy python submission (verdict: Time Limit Exceeded):
a,b,c,d = map(int,input().split())
mod = 998244353
dp = [[0]*(d+1) for _ in range(c+1)]
dp[a][b] = 1
for i in range(a+1,c+1):
    dp[i][b] = dp[i-1][b]*b
    dp[i][b] %= mod
for i in range(a,c+1):
    for j in range(b+1,d+1):
        dp[i][j] += dp[i][j-1]*i+dp[i-1][j]*j-dp[i-1][j-1]*(i-1)*(j-1)
        dp[i][j] %= mod
print(dp[c][d])

Fixed submission:
a,b,c,d = map(int,input().split())
mod = 998244353
dp = [[0]*(d+1) for _ in range(c+1)]
dp[a][b] = 1
for i in range(a+1,c+1):
    dp[i][b] = dp[i-1][b]*b
    dp[i][b] %= mod
for i in range(a,c+1):
    for j in range(b+1,d+1):
        dp[i][j] += dp[i][j-1]*i+dp[i-1][j]*j-dp[i-1][j-1]*(i-1)*(j-1)
        dp[i][j] %= mod
print(dp[c][d])
Buggy python submission (verdict: Runtime Error):
import numpy as np
import sys
from numba import njit
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


def make_divisors(n):
    divisors = np.zeros(0,np.int32)
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors = np.append(divisors,i)
            if i != n // i:
                divisors = np.append(divisors,n//i)
    return divisors

@njit
def ok(i,a):
  def make_divisors(n):
    divisors = np.zeros(0,np.int32)
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors = np.append(divisors,i)
            if i != n // i:
                divisors = np.append(divisors,n//i)
    return divisors
    x = a[i]
    for j in make_divisors(x):
        for k in range(n):
            if i != k and j == a[k]:
                break
        else:
            continue
        break
    else:
        return 1
    return 0

n = int(readline())
a = list(map(int,input().split()))
a = np.array(a,np.int32)
ans = 0
for i in range(n):
    ans += ok(i,a)
print(ans)

Fixed submission:
n = int(input())
a = list(map(int,input().split()))
a.sort()
ok = [1]*(10**6+1)
ans = 0
for i in range(n):
    if ok[a[i]]:
        if i < n-1:
            if a[i+1] != a[i]:
                ans += 1
        else:
            ans += 1
        for j in range(1,10**6//a[i]+1):
            ok[a[i]*j] = 0
print(ans)
Buggy python submission (verdict: Runtime Error):
t = input()
t = t.reprace("?","D")
print(t)

Fixed submission:
t = input()
t = t.replace("?","D")
print(t)
Buggy python submission (verdict: Time Limit Exceeded):
n,k = map(int,input().split())
a = [0]+list(map(int,input().split()))
memory = [1]
cur = 1
count = 0
while True:
    if count == k:
        print(cur)
        exit()
    if a[cur] in memory:
        for i in range(len(memory)):
            if memory[i] == a[cur]:
                loop_begin = i
                break
        loop_end = count
        break
    else:
        memory.append(a[cur])
        cur = a[cur]
        count += 1
loop = memory[loop_begin:]
loop_length = loop_end-loop_begin+1
k -= loop_begin
print(loop[k%loop_length])

Fixed submission:
n,k = map(int,input().split())
a = [0]+list(map(int,input().split()))
memory = [1]
dic = {}
cur = 1
count = 0
while True:
    if count == k:
        print(cur)
        exit()
    if a[cur] in dic.keys():
        loop_begin = dic[a[cur]]
        loop_end = count
        break
    else:
        count += 1
        memory.append(a[cur])
        dic[a[cur]] = count
        cur = a[cur]
loop = memory[loop_begin:]
loop_length = loop_end-loop_begin+1
k -= loop_begin
print(loop[k%loop_length])
Buggy python submission (verdict: Runtime Error):
n = int(input())
brackets_plus = []
brackets_minus = []
right = 0
left = 0
for i in range(n):
    s = input()
    cur = 0
    m = 0
    for j in range(len(s)):
        if s[j] == "(":
            left += 1
            cur += 1
        else:
            right += 1
            cur -= 1
            m = min(m,cur)
    if cur >= 0:
        brackets_plus.append((m,cur))
    else:
        brackets_minus.append((m,cur,m-cur))
if right != left:
    print("No")
    exit()
cur = 0
brackets_plus.sort(reverse = True)
for i in brackets_plus:
    if i[0] + cur < 0:
        print("No")
        exit()
    cur += i[1]
brackets_minus.sort(key == lambda x:x[2])
for i in brackets_minus:
    if i[0] + cur < 0:
        print("No")
        exit()
    cur += i[1]
print("Yes")

Fixed submission:
n = int(input())
brackets_plus = []
brackets_minus = []
right = 0
left = 0
for i in range(n):
    s = input()
    cur = 0
    m = 0
    for j in range(len(s)):
        if s[j] == "(":
            left += 1
            cur += 1
        else:
            right += 1
            cur -= 1
            m = min(m,cur)
    if cur >= 0:
        brackets_plus.append((m,cur))
    else:
        brackets_minus.append((m,cur,m-cur))
if right != left:
    print("No")
    exit()
cur = 0
brackets_plus.sort(reverse = True)
for i in brackets_plus:
    if i[0] + cur < 0:
        print("No")
        exit()
    cur += i[1]
brackets_minus.sort(key = lambda x:x[2])
for i in brackets_minus:
    if i[0] + cur < 0:
        print("No")
        exit()
    cur += i[1]
print("Yes")
Buggy python submission (verdict: Runtime Error):
from collections import deque
de = deque()
n,m,q = map(int,input().split())
queries = []
for i in range(q):
    a,b,c,d = map(int,input().split())
    queries.append((a,b,c,d))
de.append([1])
maxscore = 0
while de: 
    li = de.popleft()
    if len(li) < m:
        for i in range(li[-1],11):
            new_li = li + [i]
            de.append(new_li)
    else:
        score = 0
        for i in range(q):
            a,b,c,d = queries[i]
            if li[b-1]-li[a-1] == c:
                score += d
        maxscore = max(maxscore,score)
print(maxscore)


Fixed submission:
from collections import deque
de = deque()
n,m,q = map(int,input().split())
queries = []
for i in range(q):
    a,b,c,d = map(int,input().split())
    queries.append((a,b,c,d))
de.append([1])
maxscore = 0
while de: 
    li = de.popleft()
    if len(li) < n:
        for i in range(li[-1],m+1):
            new_li = li + [i]
            de.append(new_li)
    else:
        score = 0
        for i in range(q):
            a,b,c,d = queries[i]
            if li[b-1]-li[a-1] == c:
                score += d
        maxscore = max(maxscore,score)
print(maxscore)