Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
from sys import stdin
input = stdin.readline

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

    d = dict()

    # dictに格納 O(1)
    for a in A:
        try:
            d[a] += 1
        except KeyError:
            d[a] = 1
    
    ans = 0

    # 一旦ans出す
    for dat in d.items():
        ans += dat[0]*dat[1]

    # B,Cの操作
    for bc in BC:
        try:
            d[bc[1]] += d[bc[0]]
        except KeyError:
            try:
                d[bc[1]] = d[bc[0]]
            except KeyError:
                d[bc[1]] = 0
        finally:
        # 増減分だけ変更
            ans += bc[1]*d[bc[0]] - bc[0]*d[bc[0]]
            d[bc[0]] = 0
        print(ans)

main()

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

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

    d = dict()

    # dictに格納 O(1)
    for a in A:
        try:
            d[a] += 1
        except KeyError:
            d[a] = 1
    
    ans = 100000000000000000000000000
    ans = 0

    # 一旦ans出す
    for dat in d.items():
        ans += dat[0]*dat[1]

    # B,Cの操作
    # for bc in BC:
    #     try:
    #         d[bc[1]] += d[bc[0]]
    #     except KeyError:
    #         try:
    #             d[bc[1]] = d[bc[0]]
    #         except KeyError:
    #             d[bc[0]] = 0
    #             d[bc[1]] = 0
    #     finally:
    #     # 増減分だけ変更
    #         ans += bc[1]*d[bc[0]] - bc[0]*d[bc[0]]
    #         d[bc[0]] = 0
    #     print(ans)

    # B,Cの操作
    for bc in BC:
        if bc[0] in d:
            if bc[1] in d:
                d[bc[1]] += d[bc[0]]
                ans += bc[1]*d[bc[0]] - bc[0]*d[bc[0]]
                d[bc[0]] = 0
            else:
                d[bc[1]] = d[bc[0]]
                ans += bc[1]*d[bc[0]] - bc[0]*d[bc[0]]
                d[bc[0]] = 0
        else:
            pass
        print(ans)



if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
import math
import numpy as np

N = int(input())
A = list(map(int,input().split()))
ans = 1
A_log = list(map(math.log10,A))

if sum(A_log) >= 18:
    print(-1)
else:
    for i in A:
        ans *= i
    print(ans)

Fixed submission:
import math
import numpy as np

N = int(input())
A = list(map(int,input().split()))
ans = 1
A_log = A
try:
    A_log = list(map(math.log10,A))
except ValueError:
    print(0)
else:
    if sum(A_log) > 18:
        print(-1)
    else:
        for i in A:
            ans *= i
        if ans <= 1e18:
            print(ans)
        else:
            print(-1)
Buggy python submission (verdict: Runtime Error):
def main():
    H1, M1, H2, M2, K = map(int, input().split())
    print( K * ( abs((sleep - 1)-start) // K) )


if __name__ == "__main__":
    main()


Fixed submission:
import datetime

def main():
    H1, M1, H2, M2, K = map(int, input().split())

    start = H1*60 + M1
    sleep = H2*60 + M2

    print(abs(sleep - start - K))


if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
import math

def solve():
    N, K = map(int,input().split())
    A = list(map(int, input().split()))

    state = 1
    chklist = [0] * (N + 1)
    chklist_flag = [False] * (N + 1)
    chkmod = 0

    chklist_flag[0] = True

    for i in range(1, K + 1):
        state = A[state - 1]
        if chklist_flag[state - 1]:
            chkmod = (K - chklist[state - 1]) % (i - chklist[state - 1]) 
            chkrem = chklist[state - 1]
            break
        chklist[state - 1] = i 
        chklist_flag[state - 1] = True
    
    state = 1

    for i in range(chkrem + chkmod):
        state = A[state - 1]
    
    print(state)

if __name__ == "__main__":
    solve()

Fixed submission:
import math

def solve():
    N, K = map(int,input().split())
    A = list(map(int, input().split()))

    state = 1
    chklist = [0] * (N + 1)
    chklist_flag = [False] * (N + 1)
 
    chkmod = 0
    chkrem = K

    chklist_flag[0] = True

    for i in range(1, K + 1):
        state = A[state - 1]
        if chklist_flag[state - 1]:
            chkmod = (K - chklist[state - 1]) % (i - chklist[state - 1]) 
            chkrem = chklist[state - 1]
            break
        chklist[state - 1] = i 
        chklist_flag[state - 1] = True
    
    state = 1

    for i in range(chkrem + chkmod):
        state = A[state - 1]
    
    print(state)

if __name__ == "__main__":
    solve()

Buggy python submission (verdict: Runtime Error):
import math

def solve():
    N, K = map(int,input().split())
    A = list(map(int, input().split()))

    state = 0
    chklist = [0] * N
    chklist_flag = [False] * N
    chkmod = 0

    chklist_flag[0] = True

    for i in range(1, K + 1):
        if chklist_flag[state - 1]:
            chkmod = (K - chklist[state - 1]) % (i - chklist[state - 1]) 
            break
        chklist[state - 1] = i 
        chklist_flag[state - 1] = True
        state = A[state]
        print(chklist_flag)

    for i in range(chklist[state - 1] + chkmod):
        chklist[state - 1] = i 
        state = A[state- 1]

    print(state)

if __name__ == "__main__":
    solve()

Fixed submission:
import math

def solve():
    N, K = map(int,input().split())
    A = list(map(int, input().split()))

    state = 1
    chklist = [0] * (N + 1)
    chklist_flag = [False] * (N + 1)
 
    chkmod = 0
    chkrem = K

    chklist_flag[0] = True

    for i in range(1, K + 1):
        state = A[state - 1]
        if chklist_flag[state - 1]:
            chkmod = (K - chklist[state - 1]) % (i - chklist[state - 1]) 
            chkrem = chklist[state - 1]
            break
        chklist[state - 1] = i 
        chklist_flag[state - 1] = True
    
    state = 1

    for i in range(chkrem + chkmod):
        state = A[state - 1]
    
    print(state)

if __name__ == "__main__":
    solve()
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N, K, Q = map(int, input().split())
    A_list = [int(input()) for _ in range(Q)]
    player_score = [K for _ in range(N)]

    for player_answer in A_list:
        # 正解者スコア+1して, その後全体-1
        player_score[player_answer - 1] = player_score[player_answer - 1] + 1
        player_score = list(map(lambda i: i - 1, player_score))
    
    for score in player_score:
        print("Yes" if score > 0 else "No")
    
if __name__ == "__main__":
    main()

Fixed submission:
def main():
    N, K, Q = map(int, input().split())
    A_list = [int(input()) for _ in range(Q)]
    player_score = [K for _ in range(N)]

    for player_answer in A_list:
        # 正解者スコア+1して, その後全体-n
        player_score[player_answer - 1] = player_score[player_answer - 1] + 1

    player_score = list(map(lambda i: i - len(A_list), player_score))
    
    for score in player_score:
        print("Yes" if score > 0 else "No")
    
if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
N = str(input())

count = 0
yes = False

for i in range(N):
  a , b = map(int, input().split())
  
  if a==b:
    count +=1
    if count >= 3:
      print("Yes")
      yes = True
      break
  else:
    count = 0

if not(yes):
  print("No")

Fixed submission:
N = int(input())

count = 0
yes = False

for i in range(N):
  a , b = map(int, input().split())
  
  if a==b:
    count +=1
    if count >= 3:
      print("Yes")
      yes = True
      break
  else:
    count = 0

if not(yes):
  print("No")

Buggy python submission (verdict: Runtime Error):
N = str(input())

count = 0
yes = False

for i in range(N):
  a , b = map(int, input().split())
  
  if a==b:
    count +=1
    if count >= 3:
      print("Yes")
      yes = True
      break
  else:
    count = 0

if !yes:
  print("No")

Fixed submission:
N = int(input())

count = 0
yes = False

for i in range(N):
  a , b = map(int, input().split())
  
  if a==b:
    count +=1
    if count >= 3:
      print("Yes")
      yes = True
      break
  else:
    count = 0

if not(yes):
  print("No")
Buggy python submission (verdict: Runtime Error):
a,b,c,d = map(int, input().split()

print(max( [a*c,a*d,b*c,b*d]  ))

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

print( max( [a*c,a*d,b*c,b*d] ))
Buggy python submission (verdict: Runtime Error):
S = input()
T = input()

min = length(T)

for index in range( length(S)-length(T)+1 ):
  temp = 0
  for index2 in range(T):
    if T[index2] == S[index+index2]:
      temp += 1
  if temp < min:
    min = temp

print(min)

Fixed submission:
S = input()
T = input()

min = len(T)

for index in range( len(S)-len(T)+1 ):
  temp = 0
  for index2 in range(len(T)):
    if T[index2] != S[index+index2]:
      temp += 1
  if temp < min:
    min = temp

print(min)
Buggy python submission (verdict: Runtime Error):
a,b = map(int, input().split())
 
p = [1]*a
 
while True:
  try:
    input()
  except EOFError:
    break
  c = input().split()
  for d in c:
    p[int(d)]=0
 
print(sum(p))

Fixed submission:
a,b = map(int, input().split())
 
p = [1]*a
 
while True:
  try:
    input()
  except EOFError:
    break
  c = input().split()
  for d in c:
    p[int(d)-1]=0
 
print(sum(p))

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

p = [1]*a

while True:
  if !input():
    break
  c = input().split()
  for d in c:
    p[d]=0

print(sum(p))

Fixed submission:
a,b = map(int, input().split())
 
p = [1]*a
 
while True:
  try:
    input()
  except EOFError:
    break
  c = input().split()
  for d in c:
    p[int(d)-1]=0
 
print(sum(p))
Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
"""Untitled3.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1VVfPJl7_dXodPfxyID5RNKhqW1ZRqVux
"""

N= int(input())
count=0
for i in range(N):
  p,q=map(int,input().split(' '))
  if count<3:
    if p==q:
      count+=1
    else:
      count=0
  else:
    pass

if count>=3:
  print('Yes')
else:
  print('No')

Fixed submission:
S= input()
if S[-1]=='s':
	S+='es'
else:
	S+='s'
print(S)

Buggy python submission (verdict: Runtime Error):
N= int(input())
count=0
for i in range(N):
	p,q=map(int,input().split(' '))
	if p==q:
		count+=1
	else:
		count=0

if count>=3:
	print('Yes')
else:
  	print('No')

Fixed submission:
S= input()
if S[-1]=='s':
	S+='es'
else:
	S+='s'
print(S)
Buggy python submission (verdict: Runtime Error):
N = int(input())
count=0
for i in range(N):
  p,q=map(int,input().split(' '))
  if count<3:
	if p==q:
      count+=1
    else:
      count=0
  else:
    pass
if count>=3:
  print('Yes')
else:
  print('No')

Fixed submission:
N= int(input())
count=0
for i in range(N):
  p,q=map(int,input().split(' '))
  if count<3:
    if p==q:
      count+=1
    else:
      count=0
  else:
    pass

if count>=3:
  print('Yes')
else:
  print('No')

Buggy python submission (verdict: Runtime Error):
N= int(input())
count=0
for i in range(N):
  p,q=map(int,input().split(' '))
  if p==q:
    count+=1
	else:
    count=0

if count>=3:
  print('Yes')
else:
  print('No')
		


Fixed submission:
N= int(input())
count=0
for i in range(N):
  p,q=map(int,input().split(' '))
  if count<3:
    if p==q:
      count+=1
    else:
      count=0
  else:
    pass

if count>=3:
  print('Yes')
else:
  print('No')

Buggy python submission (verdict: Runtime Error):
N= int(input())
count=0
for i in range(N):
	p,q=list(map(int,input().split(' ')))
    if p==q:
		count+=1
	else:
      	count=0

if count>=3:
  print('Yes')
else:
  print('No')
		


Fixed submission:
N= int(input())
count=0
for i in range(N):
  p,q=map(int,input().split(' '))
  if count<3:
    if p==q:
      count+=1
    else:
      count=0
  else:
    pass

if count>=3:
  print('Yes')
else:
  print('No')

Buggy python submission (verdict: Runtime Error):
N= int(input())
count=0
for i in range(N):
	p,q=map(int,input().split(' '))
    if p==q:
		count+=1
	else:
      	count=0

if count>=3:
  print('Yes')
else:
  print('No')
		

Fixed submission:
N= int(input())
count=0
for i in range(N):
  p,q=map(int,input().split(' '))
  if count<3:
    if p==q:
      count+=1
    else:
      count=0
  else:
    pass

if count>=3:
  print('Yes')
else:
  print('No')
Buggy python submission (verdict: Runtime Error):
from collections import deque
N,M=map(int,input().strip().split())
l=list()
dp=[list() for _ in range(N)]
id=[-1 for _ in range(N)] #友人グループid 友人のいない者に-1をつける
for _ in range(M):
    a,b=map(int,input().strip().split())
    l.append((a,b))
    dp[a-1].append(b)
    dp[b-1].append(a)
    id[a-1]=0
    id[b-1]=0

i=1
c=[]
for n in range(1,N+1):
    cnt=0
    if id[n-1]==0:
        q=deque([n])
        cnt+=1
        while q:
            tmp=q.popleft()
            id[tmp-1]=i
            for e in dp[tmp-1]:
                if id[e-1]==0:
                    id[e-1]=i
                    q.append(e)
                    cnt+=1
                else:
                    continue
        i+=1
        c.append(cnt)
    else:
        continue
print(max(c))

Fixed submission:
from collections import deque
N,M=map(int,input().strip().split())
l=list()
dp=[list() for _ in range(N)]
id=[-1 for _ in range(N)] #友人グループid 友人のいない者に-1をつける
for _ in range(M):
    a,b=map(int,input().strip().split())
    l.append((a,b))
    dp[a-1].append(b)
    dp[b-1].append(a)
    id[a-1]=0
    id[b-1]=0

i=1
c=[]
for n in range(1,N+1):
    cnt=0
    if id[n-1]==0:
        q=deque([n])
        while q:
            tmp=q.popleft()
            cnt+=1
            id[tmp-1]=i
            for e in dp[tmp-1]:
                if id[e-1]==0:
                    id[e-1]=i
                    q.append(e)
                else:
                    continue
        i+=1
        c.append(cnt)
    else:
        continue
if c:
    print(max(c))
else:
    print(1)
Buggy python submission (verdict: Time Limit Exceeded):
H,W,M=map(int,input().strip().split())
h=[0 for _ in range(H)]
w=[0 for _ in range(W)]

dp=[[0 for _ in range(W)] for _ in range(H)]
for m in range(M):
    i,j=map(int,input().strip().split())
    dp[i-1][j-1]=1
    h[i-1]+=1
    w[j-1]+=1

h_max=0
hmax_l=[]
for i in range(H):
    if h_max==h[i]:
        hmax_l.append(i+1)
    elif h_max<h[i]:
        h_max=h[i]
        hmax_l=[]
        hmax_l.append(i+1)
w_max=0
wmax_l=[]
for i in range(W):
    if w_max==w[i]:
        wmax_l.append(i+1)
    elif w_max<w[i]:
        w_max=w[i]
        wmax_l=[]
        wmax_l.append(i+1)

d=True
for i in range(len(hmax_l)):
    for j in range(len(wmax_l)):
        if dp[hmax_l[i]-1][wmax_l[j]-1]==0:
            d=False
            break

ans=max(h)+max(w) if d==False else max(h)+max(w)-1
print(ans)

Fixed submission:
H,W,M=map(int,input().strip().split())
h=[0 for _ in range(H)]
w=[0 for _ in range(W)]

dp=[]
for m in range(M):
    i,j=map(int,input().strip().split())
    dp.append((i,j))
    h[i-1]+=1
    w[j-1]+=1

h_max=0
hmax_l=[]
for i in range(H):
    if h_max==h[i]:
        hmax_l.append(i+1)
    elif h_max<h[i]:
        h_max=h[i]
        hmax_l=[]
        hmax_l.append(i+1)
w_max=0
wmax_l=[]
for i in range(W):
    if w_max==w[i]:
        wmax_l.append(i+1)
    elif w_max<w[i]:
        w_max=w[i]
        wmax_l=[]
        wmax_l.append(i+1)

cnt=0
for m in range(M):
    if h[dp[m][0]-1]+w[dp[m][1]-1]==h_max+w_max:
        cnt+=1

ans=h_max+w_max if cnt<len(hmax_l)*len(wmax_l) else h_max+w_max-1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N,K=map(int,input().strip().split())
A=list(map(int,input().strip().split()))
p=1
for k in range(K):
    p*=A[k]
for k in range(K,N):
    p_new=p*A[k]//A[k-K]
    if p_new>p:
        print("Yes")
    else:
        print("No")
    p=p_new

Fixed submission:
N,K=map(int,input().strip().split())
A=list(map(int,input().strip().split()))
for k in range(K,N):
    if A[k]/A[k-K]>1:
        print("Yes")
    else:
        print("No")
Buggy python submission (verdict: Time Limit Exceeded):
N,M,K=map(int,input().strip().split())
A=list(map(int,input().strip().split()))
B=list(map(int,input().strip().split()))

a=[sum(A[:n]) for n in range(N+1)]
b=[sum(B[:m]) for m in range(M+1)]
num=0
bmax=M+1
for n in range(N+1):
    if a[n]>K:
        break
    for m in reversed(range(bmax)):
        if b[m]<=K-a[n]:
            num=max(num,n+m)
            bmax=m+1
            break
print(num)


Fixed submission:
N,M,K=map(int,input().strip().split())
A=list(map(int,input().strip().split()))
B=list(map(int,input().strip().split()))

a=[0]
b=[0]
for n in range(N):
    a.append(a[n]+A[n])
for m in range(M):
    b.append(b[m]+B[m])

num=0
bmax=M+1
for n in range(N+1):
    if a[n]>K:
        break
    for m in reversed(range(bmax)):
        if b[m]<=K-a[n]:
            num=max(num,n+m)
            bmax=m+1
            break
print(num)
Buggy python submission (verdict: Runtime Error):
def main():
    #input
    N=int(input())
    l=list("zabcdefghijklmnopqrstuvwxy")

    name=[]
    n=N
    while n>=26:
        mod=n%26
        if mod!=0:
            n//=26
        else:
            n//=26
            n-=1
        name.append(l[mod])
    if mod!=0:
       name.append(l[n])
    name=[name[n] for n in reversed(range(len(name)))]
    print("".join(name))

if __name__=="__main__":
    main()

Fixed submission:
def main():
    #input
    N=int(input())
    l=list("zabcdefghijklmnopqrstuvwxy")
    if N<26:
        print(l[N])
    else:
        name=[]
        n=N
        while n>=26:
            mod=n%26
            if mod!=0:
                n//=26
            else:
                n//=26
                n-=1
            name.append(l[mod])
        if n!=0:
            name.append(l[n])
        name=[name[n] for n in reversed(range(len(name)))]
        print("".join(name))

if __name__=="__main__":
    main()

Buggy python submission (verdict: Runtime Error):
def main():
    #input
    N=int(input())
    l=list("zabcdefghijklmnopqrstuvwxy")

    name=[]
    n=N
    while n>26:
        mod=n%26
        n//=26
        name.append(l[mod])
    name.append(l[n])
    name=[name[n] for n in reversed(range(len(name)))]
    print("".join(name))

if __name__=="__main__":
    main()

Fixed submission:
def main():
    #input
    N=int(input())
    l=list("zabcdefghijklmnopqrstuvwxy")
    if N<26:
        print(l[N])
    else:
        name=[]
        n=N
        while n>=26:
            mod=n%26
            if mod!=0:
                n//=26
            else:
                n//=26
                n-=1
            name.append(l[mod])
        if n!=0:
            name.append(l[n])
        name=[name[n] for n in reversed(range(len(name)))]
        print("".join(name))

if __name__=="__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
A=list(map(int,input().strip().split()))
Q=int(input())

dp=[0 for n in range(100000)]
for n in range(len(A)):
    dp[A[n]-1]+=1

for n in range(Q):
    B,C=map(int,input().strip().split())
    dp[C-1]+=dp[B-1]
    dp[B-1]=0

    ans=0
    for n in range(1,len(dp)):
        ans+=n*dp[n-1]
    print(ans)

Fixed submission:
N=int(input())
A=list(map(int,input().strip().split()))
Q=int(input())

dp=[0 for n in range(100000+1)]
for n in range(len(A)):
    dp[A[n]-1]+=1

init=0
for n in range(1,len(dp)):
    init+=n*dp[n-1]

ans=init
for n in range(Q):
    B,C=map(int,input().strip().split())
    ans=ans-dp[B-1]*B+dp[B-1]*C
    dp[C-1]+=dp[B-1]
    dp[B-1]=0
    print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
A=list(map(int,input().strip().split()))

INF=1000000000000000000

ans=1
for n in range(N):
    ans*=A[n]

if ans>INF:
    print(-1)
else:
    print(ans)


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

INF=1000000000000000000
if 0 in A:
    print(0)
else:
    ans=1
    for n in range(N):
        ans*=A[n]
        if ans>INF:
            break

    if ans>INF:
        print(-1)
    else:
        print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

def bfs(AB,N,M):
    #initialize route guide
    g=[0 for n in range(N)]
    g[0]=1
    
    q=deque([1])
    while q:
        x=q.popleft()
        for m in range(M):
            if AB[m][0]==x and g[AB[m][1]-1]==0:
                q.append(AB[m][1])
                g[AB[m][1]-1]=x
            elif AB[m][1]==x and g[AB[m][0]-1]==0:
                q.append(AB[m][0])
                g[AB[m][0]-1]=x
    ok=True if 0 not in g else False
    return ok,g
    
def main():
    #normal input
    N,M=map(int,input().strip().split())
    AB=[list(map(int,input().strip().split())) for _ in range(M)]
    
    ok,g=bfs(AB,N,M)
    
    print("Yes" if ok else "No")
    if ok:
        for n in range(1,N):
            print(g[n])
    
    
    return

if __name__=="__main__":
    main()

Fixed submission:
from collections import deque

def bfs2(g,n,m):
    pre=[0 for _ in range(n+1)]
    pre[1]=1
    q=deque([1])
    while q:
        x=q.popleft()
        for i in range(len(g[x])):
            if pre[g[x][i]]==0:
                q.append(g[x][i])
                pre[g[x][i]]=x
    return pre

def main2():
    #normal input
    n,m=map(int,input().strip().split())
    g=[[] for _ in range(n+1)]
    for _ in range(m):
        a,b=map(int,input().strip().split())
        g[a].append(b)
        g[b].append(a)
        
    pre=bfs2(g,n,m)
    
    print("Yes" if 0 not in pre[1:] else "No")
    for i in range(2,len(pre)):
        print(pre[i])

if __name__=="__main__":
    main2()
Buggy python submission (verdict: Runtime Error):
N,K=map(int,input().strip().split())
A=list(map(int,input().strip().split()))

l=[1]
x=1
for n in range(N):
    y=A[x-1]
    l.append(y)
    x=y

cnt=[0 for n in range(N)]
for n in range(N):
    cnt[l[n]-1]+=1
    if cnt[l[n]-1]==2:
        temp=n
        break
for m in range(len(l)):
    if l[m]==temp:
        bias=m
        mul=temp-bias+1
        break

if bias>K:
    print(l[K])
else:
    print(l[bias+(K-bias)%mul])

Fixed submission:
N,K=map(int,input().strip().split())
A=list(map(int,input().strip().split()))

l=[1]
x=1
cnt=[0 for n in range(N)]
cnt[0]=1
for n in range(N):
    y=A[x-1]
    l.append(y)
    cnt[y-1]+=1
    x=y
    if cnt[y-1]==2:
        value=y
        temp=n
        break

for m in range(len(l)):
    if l[m]==value:
        bias=m
        mul=temp-bias+1
        break
    
if bias>K:
    print(l[K])
else:
    print(l[bias+(K-bias)%mul])

Buggy python submission (verdict: Runtime Error):
N,K=map(int,input().strip().split())
A=list(map(int,input().strip().split()))

l=[1]
x=1
for n in range(N):
    y=A[x-1]
    l.append(y)
    x=y

x=[0 for n in range(N)]
for n in range(N):
    x[l[n]-1]+=1
    if x[l[n]-1]==2:
        temp=n
        break
for m in range(len(l)):
    if l[m]==temp:
        bias=m
        mul=temp-bias+1
        break

if bias>K:
    print(l[K])
else:
    print(l[bias+(K-bias)%mul])

Fixed submission:
N,K=map(int,input().strip().split())
A=list(map(int,input().strip().split()))

l=[1]
x=1
cnt=[0 for n in range(N)]
cnt[0]=1
for n in range(N):
    y=A[x-1]
    l.append(y)
    cnt[y-1]+=1
    x=y
    if cnt[y-1]==2:
        value=y
        temp=n
        break

for m in range(len(l)):
    if l[m]==value:
        bias=m
        mul=temp-bias+1
        break
    
if bias>K:
    print(l[K])
else:
    print(l[bias+(K-bias)%mul])

Buggy python submission (verdict: Runtime Error):
N,K=map(int,input().strip().split())
A=list(map(int,input().strip().split()))

l=[1]
x=1
for n in range(N):
    y=A[x-1]
    l.append(y)
    x=y

x=[0 for n in range(N)]
for n in range(N):
    x[l[n]-1]+=1
    if x[l[n]-1]==2:
        temp=n
        break
for m in range(len(l)):
    if l[m]==temp:
        bias=m
        mul=temp-bias+1
        break

if bias>K:
    print(l[K])
else:
    print(l[bias+(K-bias)%mul])

Fixed submission:
N,K=map(int,input().strip().split())
A=list(map(int,input().strip().split()))

l=[1]
x=1
cnt=[0 for n in range(N)]
cnt[0]=1
for n in range(N):
    y=A[x-1]
    l.append(y)
    cnt[y-1]+=1
    x=y
    if cnt[y-1]==2:
        value=y
        temp=n
        break

for m in range(len(l)):
    if l[m]==value:
        bias=m
        mul=temp-bias+1
        break
    
if bias>K:
    print(l[K])
else:
    print(l[bias+(K-bias)%mul])