Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
import sys
x,n = map(int,input().split())
if n == 0:
    print(x)
    sys.exit(0)
p = list(map(int,input().split()))
p.sort()
a = []
j = 0
for i in range(n):
    while (j < p[i]):
        a.append(j)
        j += 1 
    j += 1
for i in range(j,101):
    a.append(j)
    j += 1 
    
#print(a)
def solve(a,x):
    if x < a[0]:
        print(a[0])
        return
    if x > a[-1]:
        if x - a[-1] <= 101 - x:
            print(a[-1])
        else:
            print(101)
        return
    if (binary_search(a,x)):
        mid = binary_search(a,x) - 2
        minimum = abs(a[mid] - x)
        index = mid
        for i in range(1,5):
            #print(a[mid + i])
            if abs(a[mid + i] - x) < minimum:
                index = mid + i 
                minimum = abs(a[mid + i] - x)
        print(a[index])
        return
    

def binary_search(a,x):
    left = 0
    right = len(a)
    while (left + 1 < right):
        mid = (left + right)//2
        if x == a[mid]:
            print(a[mid])
            return 
        elif x < a[mid]:
            right = mid 
        else:
            left = mid + 1
    return mid 

solve(a,x)


Fixed submission:
X, N = map(int, input().split())
p = list(map(int, input().split()))
for d in range(X + 1):
    for s in [-1, +1]:
        a = X + s * d
        if p.count(a) == 0:
            print(a)
            exit(0)
Buggy python submission (verdict: Runtime Error):
import math
n = int(input())
a = list(map(int,input().split()))

a.sort()
#print(a)
flag = 0
if a[0] == a[1]:
    ans = 0
else:
    ans = 1
if a[0] == 1:
    print(ans)
    exit(0)

if 2 in a:
    flag = 1
    
dict = {a[0]:1}

for i in range(1,n):
    #print(str(i) + ":")
    for j in range(1,round(math.sqrt(a[i]))+1):
        if flag == 1 and a[i] % 2 == 0:
            break 
        if a[i] % j == 0:
            #print(j)
            if j in dict.keys():
                break
            if a[i] // j in dict.keys():
                break
        if j == round(math.sqrt(a[i])):
            ans += 1
            #print("ans:" + str(ans))
    dict[a[i]] = 1
    
print(ans)


Fixed submission:
n = int(input())
a = list(map(int,input().split()))
largest = max(a)
b = [True] * (largest+1)
dict = {}
ans = 0
for num in a:
    if num in dict.keys():
        b[num] = False
    else:
        dict[num] = 1
        y = num * 2
        while(y <= largest):
            b[y] = False
            y += num
for num in a:
    if b[num] == True:
        ans += 1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int,input().split()))
number = a[0]
for i in range(1,n):
    number = number * a[i]
    
if number > 10**18:
    print(-1)
else:
    print(number)
    

Fixed submission:
n = int(input())
a = list(map(int,input().split()))
a=sorted(a,reverse = True)
if a[-1]==0:
    print(0)
    exit()
else:
    
    number = a[0]
    for i in range(1,n):
        number = number * a[i]
        if number > 10**18:
            print(-1)
            exit()

print(number)
Buggy python submission (verdict: Runtime Error):
import math
N,M,X = map(int,input().split())
#a[i][0]:price of the i-th book
a = [[] for i in range(N)]
for i in range(N):
    a[i] = list(map(int,input().split()))
a.sort()
#dp[j][k]:lowest amount of money needed to make j-th algorithm level k
dp = [[math.inf for k in range(X+1)] for j in range(M)]

for j in range(M):
    dp[j][0] = 0

for i in range(N):
    for j in range(M):
        for k in range(X,a[i][j+1],-1):
            dp[j][k] = min(dp[j][k],dp[j][k-a[i][j+1]] + a[i][0])
        for k in range(1,a[i][j+1]+1):
            dp[j][k] = min(dp[j][k],a[i][0])

#print(dp)
maximum = 0
for j in range(M):
    maximum = max(maximum,dp[j][X])
if maximum == math.inf:
    print(-1)
else:
    print(maximum)
            


Fixed submission:
import math
N,M,X = map(int,input().split())
#a[i][0]:price of the i-th book
a = [[] for i in range(N)]
for i in range(N):
    a[i] = list(map(int,input().split()))

def is_greater(b):
    for i in b:
        if i < X:
            return False
    return True 
    
cost = math.inf
for i in range(2**N):
    b = [0]*(M+1)
    for j in range(N):
        if ((i >> j)&1):
            for k in range(M+1):
                b[k] += a[j][k]
    if b[0] < cost and is_greater(b[1:]):
        cost = b[0]

if cost == math.inf:
    print(-1)
else:
    print(cost)

Buggy python submission (verdict: Runtime Error):
import math
N,M,X = map(int,input().split())
#a[i][0]:price of the i-th book
a = [[] for i in range(N)]
for i in range(N):
    a[i] = list(map(int,input().split()))
a.sort()
#dp[j][k]:lowest amount of money needed to make j-th algorithm level k
dp = [[math.inf for k in range(X+1)] for j in range(M)]

for j in range(M):
    dp[j][0] = 0

for i in range(N):
    for j in range(M):
        for k in range(X,a[i][j+1],-1):
            dp[j][k] = min(dp[j][k],dp[j][k-a[i][j+1]] + a[i][0])
        for k in range(1,a[i][j+1]+1):
            dp[j][k] = min(dp[j][k],a[i][0])

#print(dp)
maximum = 0
for j in range(M):
    maximum = max(maximum,dp[j][X])
if maximum == math.inf:
    print(-1)
else:
    print(maximum)
            


Fixed submission:
import math
N,M,X = map(int,input().split())
#a[i][0]:price of the i-th book
a = [[] for i in range(N)]
for i in range(N):
    a[i] = list(map(int,input().split()))

def is_greater(b):
    for i in b:
        if i < X:
            return False
    return True 
    
cost = math.inf
for i in range(2**N):
    b = [0]*(M+1)
    for j in range(N):
        if ((i >> j)&1):
            for k in range(M+1):
                b[k] += a[j][k]
    if b[0] < cost and is_greater(b[1:]):
        cost = b[0]

if cost == math.inf:
    print(-1)
else:
    print(cost)
Buggy python submission (verdict: Time Limit Exceeded):
s = input()
t = 0
number = 0
m = [0 for i in range(2019)]
m[0] = 1
for i in range(len(s)):
    t = t  +  int(s[len(s) - 1 - i])* (10**i)
    #print(t)
    m[t % 2019] += 1
    #print(t%2019)

for j in range(2019):
    if m[j]>= 2:
        number += (m[j]*(m[j] - 1))//2

print(number)

Fixed submission:
s = input()
t = 0
number = 0
#m = [0 for i in range(2019)]
m = [0]*2019
m[0] = 1
a = 1
for i in range(len(s)):
    t = t  +  int(s[len(s) - 1 - i])* a 
    #print(t)
    m[t % 2019] += 1
    a = a * 10
    a = a % 2019
    #print(t%2019)

for j in range(2019):
    #if m[j]>= 2:
    number += (m[j]*(m[j] - 1))//2

print(number)

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
number = 0
for i in range(0,len(s)- 3):
    sum = 100 * int(s[i]) + 10 * int(s[i+1]) + int(s[i+2])
    for j in range(i+3,len(s)):
        sum = sum*10  + int(s[j])
        #print(sum)
        if sum % 2019 == 0:
            
            number = number + 1
    
print(number)

Fixed submission:
s = input()
t = 0
number = 0
#m = [0 for i in range(2019)]
m = [0]*2019
m[0] = 1
a = 1
for i in range(len(s)):
    t = t  +  int(s[len(s) - 1 - i])* a 
    #print(t)
    m[t % 2019] += 1
    a = a * 10
    a = a % 2019
    #print(t%2019)

for j in range(2019):
    #if m[j]>= 2:
    number += (m[j]*(m[j] - 1))//2

print(number)

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
number = 0
for i in range(0,len(s)- 3):
    for j in range(i+4,len(s)):
        sum = 0
        for k in range(j-i+1):
            sum = sum + int(s[j- k]) * (10 ** (k))
        if sum % 2019 == 0:
            number = number + 1
print(number)
         

Fixed submission:
s = input()
t = 0
number = 0
#m = [0 for i in range(2019)]
m = [0]*2019
m[0] = 1
a = 1
for i in range(len(s)):
    t = t  +  int(s[len(s) - 1 - i])* a 
    #print(t)
    m[t % 2019] += 1
    a = a * 10
    a = a % 2019
    #print(t%2019)

for j in range(2019):
    #if m[j]>= 2:
    number += (m[j]*(m[j] - 1))//2

print(number)

Buggy python submission (verdict: Time Limit Exceeded):

s = input()
number = 0
for i in range(0,len(s)- 3):
    for j in range(i+3,len(s)):
        sum = 0
        for k in range(j-i+1):
            sum = sum + int(s[j- k]) * (10 ** (k))
            #print(sum)
        if sum % 2019 == 0:
            number = number + 1
print(number)

Fixed submission:
s = input()
t = 0
number = 0
#m = [0 for i in range(2019)]
m = [0]*2019
m[0] = 1
a = 1
for i in range(len(s)):
    t = t  +  int(s[len(s) - 1 - i])* a 
    #print(t)
    m[t % 2019] += 1
    a = a * 10
    a = a % 2019
    #print(t%2019)

for j in range(2019):
    #if m[j]>= 2:
    number += (m[j]*(m[j] - 1))//2

print(number)
Buggy python submission (verdict: Time Limit Exceeded):
import math
from functools import reduce

k = int(input())

def gcd2(*numbers):
    return reduce(math.gcd,numbers)
sum = 0
for a in range(1,k+1):
    for b in range(1,k+1):
        for c in range(1,k+1):
            #print(gcd2(a,b,c))
            sum = sum + gcd2(a,b,c)

print(sum)
        

Fixed submission:
import math
from functools import reduce

k = int(input())

def gcd2(*numbers):
    return reduce(math.gcd,numbers)
sum = 0
for a in range(1,k+1):
    for b in range(a,k+1):
        for c in range(b,k+1):
            if len(set([a,b,c]))==2:
                sum = sum + gcd2(a,b,c)*3
            elif len(set([a,b,c]))==1:
                sum = sum + gcd2(a,b,c)
            else:
                sum = sum + gcd2(a,b,c)*6
            #print(gcd2(a,b,c))


print(sum)
Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
a = [int(input()) for i in range(m)]
dp = [0]*(n+1)
dp[0] = 1
if 1 not in a:
    dp[1] = 1
for i in range(2,n+1):
    if i not in a:
        dp[i] = (dp[i-1] + dp[i-2]) % 1000000007
#print(dp)
print(dp[n])

Fixed submission:
n,m = map(int,input().split())
a = [int(input()) for i in range(m)]
a.append(1000000)
dp = [0]*(n+1)
dp[0] = 1
j = 0
if a[j] == 1:
    j += 1
else:
    dp[1] = 1

for i in range(2,n+1):
    if i == a[j]:
        j += 1
    else:
        dp[i] = (dp[i-1] + dp[i-2]) % 1000000007
#print(dp)
print(dp[n])
Buggy python submission (verdict: Runtime Error):
H,W = map(int,input().split())
h,w = = map(int,input().split())
print((H-h)*(W-w))

Fixed submission:
H,W = map(int,input().split())
h,w = map(int,input().split())
print((H-h)*(W-w))
Buggy python submission (verdict: Runtime Error):
N = int(input())
S = input()
ans = 0
a = [0] * 26
for i in range(N):
    a[ord(S[i])-ord('a')] += 1

b = [0] * 26
for i in range(26):
    a[ord(S[i])-ord('a')] -= 1
    b[ord(S[i])-ord('a')] += 1
    cnt = 0
    for j in range(26):
        if a[j]*b[j]>0:
            cnt += 1
    ans = max(ans,cnt)
print(ans)


Fixed submission:
N = int(input())
S = input()
ans = 0
a = [0] * 26
for i in range(N):
    a[ord(S[i])-ord('a')] += 1

b = [0] * 26
for i in range(N):
    a[ord(S[i])-ord('a')] -= 1
    b[ord(S[i])-ord('a')] += 1
    cnt = 0
    for j in range(26):
        if a[j]*b[j]>0:
            cnt += 1
    ans = max(ans,cnt)
print(ans)
Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int,input().split()))

cnt = 0
for i in range(N):
    if A[i] == i+1:
        A[i],A[i+1] = A[i+1],A[i]
        cnt += 1
        i += 1
print(cnt)

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

cnt = 0
for i in range(N-1):
    if A[i] == i+1:
        A[i],A[i+1] = A[i+1],A[i]
        cnt += 1
        i += 1
if A[N-1]==N:
    cnt += 1
print(cnt)
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n = int(input())
    a = list(map(int, input().split()))
    ret = 0
    sum_list = [None] * n + [0]
    for i in range(n):
        sum_list[i] = sum(a[i:])
    for i in range(n):
        ret += a[i] * sum_list[i+1]
        ret %= 1000000007
    print(ret)

if __name__ == "__main__":
    main()


Fixed submission:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    ret = 0
    sum_list = [None] * n + [0]
    for i in range(n-1, -1, -1):
        sum_list[i] = sum_list[i+1] + a[i]
    for i in range(n):
        ret += a[i] * sum_list[i+1]
        ret %= 1000000007
    print(ret)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n = int(input())
    a = list(map(int, input().split()))
    ret = 0
    for i in range(n):
        ret += a[i] * sum(a[i + 1:])
        ret %= 1000000007
    print(ret)

if __name__ == "__main__":
    main()


Fixed submission:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    ret = 0
    sum_list = [None] * n + [0]
    for i in range(n-1, -1, -1):
        sum_list[i] = sum_list[i+1] + a[i]
    for i in range(n):
        ret += a[i] * sum_list[i+1]
        ret %= 1000000007
    print(ret)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n = int(input())
    a = list(map(int, input().split()))
    ret = 0
    for i in range(n):
        for j in range(i):
            ret += a[i] * a[j]
            ret %= 10 ** 9 + 7
    print(ret)

if __name__ == "__main__":
    main()


Fixed submission:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    ret = 0
    sum_list = [None] * n + [0]
    for i in range(n-1, -1, -1):
        sum_list[i] = sum_list[i+1] + a[i]
    for i in range(n):
        ret += a[i] * sum_list[i+1]
        ret %= 1000000007
    print(ret)

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n, k = list(map(int, input().split()))
    a = list(map(int, input().split()))

    score = 1
    for _a in a[:k]:
        score *= _a

    for i, _a in enumerate(a[k:]):
        if score < (score * _a / a[i]):
            print("Yes")
        else:
            print("No")
        score = score * _a / a[i]
    
    return


if __name__ == "__main__":
    main()



Fixed submission:
def main():
    n, k = list(map(int, input().split()))
    a = list(map(int, input().split()))

    for i, _a in enumerate(a[k:]):
        if a[i] < _a:
            print("Yes")
        else:
            print("No")
    
    return


if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
a=int(input())
if a%2==0:
  print(0.5)
 else:
  print((a-1)/2/a)

Fixed submission:
a=int(input())
if a%2==0:
    print(0.5)
else:
    print(1-(a-1)/2/a)
Buggy python submission (verdict: Time Limit Exceeded):
A, B = map(int,input().split())
import fractions
_gcd = fractions.gcd(A, B)

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

l = make_divisors(_gcd)
if len(l)==1:
    print(1)
    quit()
l.sort()

l2 = l[:]
for i,x in enumerate(l):
    for y in l[i+1:]:
        if fractions.gcd(x, y) != 1 and y in l2:
            l2.remove(y)
print(len(l2))

Fixed submission:
A, B = map(int,input().split())
import fractions
_gcd = fractions.gcd(A, B)

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

l = make_divisors(_gcd)
if len(l)==1:
    print(1)
    quit()
l.sort()
aa=False
for x in l:
    if x%2==0:
        aa=x
        break
if aa:
    l2 = [x for x in l if x%2]
    l2.append(aa)
else:
    l2 = l[:]
for i,x in enumerate(l2):
    for y in l2[i+1:]:
        if fractions.gcd(x, y) != 1 and y in l2:
            l2.remove(y)
print(len(l2))

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

import math
gcd = math.gcd(A, B)

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

l = make_divisors(gcd)
if len(l)==1:
    print(1)
    quit()
l.sort()

l2 = l[:]
for i,x in enumerate(l):
    for y in l[i+1:]:
        if math.gcd(x, y) != 1 and y in l2:
            l2.remove(y)
print(len(l2))


Fixed submission:
A, B = map(int,input().split())
import fractions
_gcd = fractions.gcd(A, B)

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

l = make_divisors(_gcd)
if len(l)==1:
    print(1)
    quit()
l.sort()
aa=False
for x in l:
    if x%2==0:
        aa=x
        break
if aa:
    l2 = [x for x in l if x%2]
    l2.append(aa)
else:
    l2 = l[:]
for i,x in enumerate(l2):
    for y in l2[i+1:]:
        if fractions.gcd(x, y) != 1 and y in l2:
            l2.remove(y)
print(len(l2))
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
n, m = map(int, input().split())

matrix = np.array([[0 for i in range(n)] for i in range(n)])
for i in range(m):
    a, b = map(int, input().split())
    matrix[a-1][b-1] = 1
    matrix[b-1][a-1] = 1

print(matrix.sum(axis=1).max()+1)

Fixed submission:
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

      
from collections import Counter
n, m = map(int, input().split())

uf = UnionFind(n)
for i in range(m):
    a, b = map(int, input().split())
    uf.union(a-1,b-1)

print(abs(min((uf.parents))))
Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
score = list(map(int, input().split()))

mv_scores = []
for i in range(k, n+1):
    tmp_score = 1
    for s in score[i-k:i]:
        tmp_score *= s
    mv_scores.append(tmp_score)

for j in range(1, len(mv_scores)):
    if mv_scores[j] - mv_scores[j-1] > 0:
        print("Yes")
    else:
        print("No")

Fixed submission:
n, k = map(int, input().split())
score = list(map(int, input().split()))

for i in range(k, n):
    if score[i-k] < score[i]:
        print("Yes")
    else:
        print("No")
Buggy python submission (verdict: Runtime Error):
n = int(input())
values = np.array(list(map(int, input().split())))

kabu = 0
money = 1000
for i in range(n-1):
    if values[i] < values[i+1]:
        kabu = money // values[i] 
        money += (values[i+1] - values[i]) * kabu
        
print(money)

Fixed submission:
n = int(input())
values = list(map(int, input().split()))

kabu = 0
money = 1000
for i in range(n-1):
    if values[i] < values[i+1]:
        kabu = money // values[i] 
        money += (values[i+1] - values[i]) * kabu
        
print(money)

Buggy python submission (verdict: Runtime Error):
n = int(input())
values = np.array(list(map(int, input().split())))

kabu = 0
money = 1000
for i in range(n-1):
    if values[i] < values[i+1]:
        kabu = money // values[i] 
        money += (values[i+1] - values[i]) * kabu
        
    else:
        continue
        
print(money)

Fixed submission:
n = int(input())
values = list(map(int, input().split()))

kabu = 0
money = 1000
for i in range(n-1):
    if values[i] < values[i+1]:
        kabu = money // values[i] 
        money += (values[i+1] - values[i]) * kabu
        
print(money)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
ans = 1
for i in a:
    ans *= i
if ans > 10**18:
    print(-1)
else:
    print(ans)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
a = sorted(a, reverse=True)
ans = 1
flag = 0
if a[-1] == 0:
    print(0)
else:
    for i in a:
        ans *= i
        if ans > 10**18:
            flag +=1
            break
    if flag>0:
        print(-1)
    else:
        print(ans)
Buggy python submission (verdict: Runtime Error):
a, b, c, k = map(int, input().split())

ones = [1] * a
zeros = [0] * b
minus_ones = [-1] * c

ans = sorted(ones + zeros + minus_ones)[-k:]
print(sum(ans))

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

if a+b >= k:
    print(min(a, k))
else:
    print(a-k+(a+b))
Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
a = list(map(int, input().split()))

idx = a[0]
hist = [1]
flag = 0
for i, _ in enumerate(range(k), start=1):
    if idx in hist:
        hist.append(idx)
        flag = 1
        break
    hist.append(idx)
    idx = a[idx-1]
    
if flag:
    l_start2, l_end2 = hist.index(idx), i
    surplus = (k-l_start) % (l_end - l_start)
    print(hist[l_start+surplus])
else:
    print(idx)

Fixed submission:
n, k = map(int, input().split())
a = list(map(int, input().split()))

idx = a[0]
hist = [1]
passed = [0] * n
flag = 0
for i, _ in enumerate(range(k), start=1):
    if passed[idx-1] == 1:
        hist.append(idx)
        passed[idx-1] = 1
        flag = 1
        break
    hist.append(idx)
    passed[idx-1] = 1
    idx = a[idx-1]
    
    
if flag:
    l_start, l_end = hist.index(idx), i
    surplus = (k - l_start) % (l_end - l_start)
    print(hist[l_start + surplus])
else:
    print(hist[-1])

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

idx = a[0]
hist = [1]
for _ in range(k):
    if idx in hist:
        hist.append(idx)
        break
    hist.append(idx)
    idx = a[idx-1]
    
l_start, l_end = [i for i, v in enumerate(hist) if hist[-1]==v]

surplus = (k-l_start) % (l_end - l_start)
print(hist[l_start+surplus])

Fixed submission:
n, k = map(int, input().split())
a = list(map(int, input().split()))

idx = a[0]
hist = [1]
passed = [0] * n
flag = 0
for i, _ in enumerate(range(k), start=1):
    if passed[idx-1] == 1:
        hist.append(idx)
        passed[idx-1] = 1
        flag = 1
        break
    hist.append(idx)
    passed[idx-1] = 1
    idx = a[idx-1]
    
    
if flag:
    l_start, l_end = hist.index(idx), i
    surplus = (k - l_start) % (l_end - l_start)
    print(hist[l_start + surplus])
else:
    print(hist[-1])
Buggy python submission (verdict: Runtime Error):
import itertools
x = int(input())
digits = len(str(x))
dic = {}
keta = []
for i in range(10000):
    num = i ** 5    
    if (num > (10**9)) or (num > 10**(digits+1)):
        break
    if num > 10**(digits-2):
        dic[i] = num
    elif num < 10**(digits+1):
        dic[i] = num
        
for num1,num2 in itertools.combinations(dic.keys(), 2):
    a, b = dic[num1], dic[num2]
    if x == (a+b):
        ans = (num1,num2)
        break
    if x == (a-b):
        ans = (num1,-num2)
        break
    if x == (-a+b):
        ans = (-num1,num2)
        break
    if x == (-a-b):
        ans = (-num1,-num2)
        break
print(f"{ans[0]} {ans[1]}")

Fixed submission:
import itertools
x = int(input())
digits = len(str(x))
dic = {}
keta = []
for i in range(121):
    num = i ** 5    
    if (num > 10**(digits+2)):
        break
    if num > 10**(digits-2):
        dic[i] = num
        dic[-i] = -num
    elif num < 10**(digits+1):
        dic[i] = num
        dic[-i] = -num
        
for num1,num2 in itertools.combinations(dic.keys(), 2):
    a, b = dic[num1], dic[num2]
    if x == (a-b):
        ans = (num1,num2)
        break
    if x == (-a+b):
        ans = (num2,num1)
        break
print(f"{ans[0]} {ans[1]}")