Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
n,w=map(int,input().split())
wv=[list(map(int,input().split())) for _ in range(n)]
value=[[0 for _ in range(w+1)]for _ in range(n+1)]
for i in range(n):
    for j in range(w+1):
        lastw=j-wv[i][0]
        if lastw>=0:
            value[i+1][j]=max(value[i][j],value[i][lastw]+wv[i][1])
        else:
            value[i+1][j]=value[i][j]

print(value[n][w])


Fixed submission:

import numpy as np
n,w=map(int,input().split())
wv=[list(map(int,input().split())) for _ in range(n)]
value=np.zeros((n+1,w+1),dtype=int)
for i in range(n):
    wei=wv[i][0]
    value[i+1][:wei]=value[i][:wei]
    # np.maximum([1,-3,2],0) == [1,0,2]
    value[i+1][wei:]=np.maximum(value[i][wei:],value[i][:-wei]+wv[i][1])
# print(value)
print(value[n][w])

Buggy python submission (verdict: Time Limit Exceeded):
n,w=map(int,input().split())
wv=[list(map(int,input().split())) for _ in range(n)]
# wv.sort(key=lambda x:x[0])
# wv.sort(key=lambda x:-x[1])
# value[i][j] i番目までで重さがj以下となるように選んだときの価値の最大値
value=[[0 for _ in range(w+1)]for _ in range(n+1)]
for i in range(n):
    for j in range(w+1):
        if j-wv[i][0]>=0:
            value[i+1][j]=max(value[i][j],value[i][j-wv[i][0]]+wv[i][1])
        value[i+1][j]=max(value[i+1][j],value[i][j])

print(value[n][w])


Fixed submission:

import numpy as np
n,w=map(int,input().split())
wv=[list(map(int,input().split())) for _ in range(n)]
value=np.zeros((n+1,w+1),dtype=int)
for i in range(n):
    wei=wv[i][0]
    value[i+1][:wei]=value[i][:wei]
    # np.maximum([1,-3,2],0) == [1,0,2]
    value[i+1][wei:]=np.maximum(value[i][wei:],value[i][:-wei]+wv[i][1])
# print(value)
print(value[n][w])
Buggy python submission (verdict: Runtime Error):
n,x=map(int,input().split())

def makeBurger(n):
    if n==1:
        return [1,3,1]
    else:
        burger=makeBurger(n-1)
        newBurger=[*burger,1,*burger]
        newBurger[0]+=1
        newBurger[-1]+=1
        # print(newBurger)
        return newBurger

burger=makeBurger(n)
size=0
index=0
cnt=0
while size<=x:
    layerSize=burger[index]
    if index%2:
        if size+layerSize>x:
            cnt+=x-size
            break
        else:
            cnt+=layerSize
    size+=layerSize
    index+=1
print(cnt)


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

thickness=[1]
for i in range(1,n):
    thickness.append(2*thickness[i-1]+3)
patty=[1]
for i in range(1,n):
    patty.append(2*patty[i-1]+1)


def pattyCnt(n,x):
    if n==0:
        return 0 if x<=0 else 1
    elif x<=1+thickness[n-1]:
        return pattyCnt(n-1,x-1)
    else:
        return patty[n-1]+1+pattyCnt(n-1,x-2-thickness[n-1])

print(pattyCnt(n,x))

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

def makeBurger(n):
    if n==1:
        return [1,3,1]
    else:
        burger=makeBurger(n-1)
        newBurger=[*burger,1,*burger]
        newBurger[0]+=1
        newBurger[-1]+=1
        print(newBurger)
        return newBurger

burger=makeBurger(n)
size=0
index=0
cnt=0
while size<=x:
    layerSize=burger[index]
    if index%2:
        if size+layerSize>x:
            cnt+=x-size
            break
        else:
            cnt+=layerSize
    size+=layerSize
    index+=1
print(cnt)

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

thickness=[1]
for i in range(1,n):
    thickness.append(2*thickness[i-1]+3)
patty=[1]
for i in range(1,n):
    patty.append(2*patty[i-1]+1)


def pattyCnt(n,x):
    if n==0:
        return 0 if x<=0 else 1
    elif x<=1+thickness[n-1]:
        return pattyCnt(n-1,x-1)
    else:
        return patty[n-1]+1+pattyCnt(n-1,x-2-thickness[n-1])

print(pattyCnt(n,x))

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

def makeBurger(n):
    if n==1:
        return 'bpppb'
    else:
        burger=makeBurger(n-1)
        print(n)
        return 'b'+burger+'p'+burger+'b'

burger=makeBurger(n)
cnt=0
for i in range(x):
    if burger[i]=='p':
        cnt+=1
print(cnt)

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

thickness=[1]
for i in range(1,n):
    thickness.append(2*thickness[i-1]+3)
patty=[1]
for i in range(1,n):
    patty.append(2*patty[i-1]+1)


def pattyCnt(n,x):
    if n==0:
        return 0 if x<=0 else 1
    elif x<=1+thickness[n-1]:
        return pattyCnt(n-1,x-1)
    else:
        return patty[n-1]+1+pattyCnt(n-1,x-2-thickness[n-1])

print(pattyCnt(n,x))
Buggy python submission (verdict: Runtime Error):
x=input().split()

print(x[0]+x[1]/2)

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

print(int(x+y/2))
Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int, input().split())
prefecture=[[] for j in range(n)]
p=[]
y=[]
for i in range(m):
    a,b=map(int, input().split())
    p.append(a)
    y.append(b)
    prefecture[p[i]-1].append(y[i])

for j in range(n):
    prefecture[j].sort()

for i in range(m):
    print('{0:06d}{1:06d}'.format(p[i],prefecture[p[i]-1].index(y[i])+1))

Fixed submission:
n,m=map(int, input().split())
prefecture=[]
for i in range(m):
    pk,yk=map(int, input().split())
    prefecture.append([pk,yk,i])

prefecture.sort(key=lambda x:(x[0],x[1])) # x[0]:pで整理したうえでx[1]:yで整理
p_num=prefecture[0][0]
city_num=1
for i in range(m):
    if(prefecture[i][0]!=p_num):
        p_num=prefecture[i][0]
        city_num=1

    prefecture[i].append(city_num)
    city_num+=1

prefecture.sort(key=lambda x:x[2])

for i in range(m):
    print('{0:06d}{1:06d}'.format(prefecture[i][0], prefecture[i][3]))

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int, input().split())
p=[]
y=[]
for i in range(m):
    a,b=map(int, input().split())
    p.append(a)
    y.append(b)

prefecture=[[] for j in range(n)]
for i in range(m):
    prefecture[p[i]-1].append(y[i])

for j in range(n):
    prefecture[j].sort()

# for j in range(n):
#     city=[] # number
#     for i in range(m):
#         if(p[i]==j+1):
#             city.append(y[i])
#     city.sort()
#     prefecture.append(city)

for i in range(m):
    print('{0:06d}{1:06d}'.format(p[i],prefecture[p[i]-1].index(y[i])+1))

Fixed submission:
n,m=map(int, input().split())
prefecture=[]
for i in range(m):
    pk,yk=map(int, input().split())
    prefecture.append([pk,yk,i])

prefecture.sort(key=lambda x:(x[0],x[1])) # x[0]:pで整理したうえでx[1]:yで整理
p_num=prefecture[0][0]
city_num=1
for i in range(m):
    if(prefecture[i][0]!=p_num):
        p_num=prefecture[i][0]
        city_num=1

    prefecture[i].append(city_num)
    city_num+=1

prefecture.sort(key=lambda x:x[2])

for i in range(m):
    print('{0:06d}{1:06d}'.format(prefecture[i][0], prefecture[i][3]))

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int, input().split())
p=[]
y=[]
for i in range(m):
    a,b=map(int, input().split())
    p.append(a)
    y.append(b)

prefecture=[]
for j in range(n):
    city=[] # number
    for i in range(m):
        if(p[i]==j+1):
            city.append(y[i])
    city.sort()
    prefecture.append(city)

for i in range(m):
    print('{0:06d}{1:06d}'.format(p[i],prefecture[p[i]-1].index(y[i])+1))

Fixed submission:
n,m=map(int, input().split())
prefecture=[]
for i in range(m):
    pk,yk=map(int, input().split())
    prefecture.append([pk,yk,i])

prefecture.sort(key=lambda x:(x[0],x[1])) # x[0]:pで整理したうえでx[1]:yで整理
p_num=prefecture[0][0]
city_num=1
for i in range(m):
    if(prefecture[i][0]!=p_num):
        p_num=prefecture[i][0]
        city_num=1

    prefecture[i].append(city_num)
    city_num+=1

prefecture.sort(key=lambda x:x[2])

for i in range(m):
    print('{0:06d}{1:06d}'.format(prefecture[i][0], prefecture[i][3]))
Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
def gcd(a,b):
    while b:
        a,b=b,a%b
    return a

# a1,...,anがdivisorで割り切れる、すなわちdivisorはMの約数である
divisor=m//n
while m%divisor!=0:
    divisor-=1
print(divisor)


Fixed submission:
# できなかった
# a1,...,anがdivisorで割り切れる、すなわちdivisorはMの約数である
n,m=map(int,input().split())

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

divisors.sort(key=lambda x:-x)
# print(divisors)
for d in divisors:
    if d<=m//n:
        print(d)
        break

Buggy python submission (verdict: Runtime Error):
import fraction
# import math
n,m=map(int,input().split())
# 1*mのうちnコを予め除いた残りで組み合わせの数を求められる
# (m-n+n-1)!/((m-n)!(n-1)!)
if m%n==0:
    print(m//n)
else:
    print(fraction.gcd(m//n,m%n))
    # print(math.gcd(m//n,m%n))


Fixed submission:
# できなかった
# a1,...,anがdivisorで割り切れる、すなわちdivisorはMの約数である
n,m=map(int,input().split())

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

divisors.sort(key=lambda x:-x)
# print(divisors)
for d in divisors:
    if d<=m//n:
        print(d)
        break

Buggy python submission (verdict: Runtime Error):
import math
n,m=map(int,input().split())
# 1*mのうちnコを予め除いた残りで組み合わせの数を求められる
# (m-n+n-1)!/((m-n)!(n-1)!)
if m%n==0:
    print(m//n)
else:
    print(math.gcd(m//n,m%n))


Fixed submission:
# できなかった
# a1,...,anがdivisorで割り切れる、すなわちdivisorはMの約数である
n,m=map(int,input().split())

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

divisors.sort(key=lambda x:-x)
# print(divisors)
for d in divisors:
    if d<=m//n:
        print(d)
        break

Buggy python submission (verdict: Runtime Error):
import math
n,m=map(int,input().split())
# 1*mのうちnコを予め除いた残りで組み合わせの数を求められる
# (m-n+n-1)!/((m-n)!(n-1)!)
if n%m==0:
    print(m//n)
else:
    print(math.gcd(m//n,m%n))


Fixed submission:
# できなかった
# a1,...,anがdivisorで割り切れる、すなわちdivisorはMの約数である
n,m=map(int,input().split())

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

divisors.sort(key=lambda x:-x)
# print(divisors)
for d in divisors:
    if d<=m//n:
        print(d)
        break
Buggy python submission (verdict: Time Limit Exceeded):
N,X=map(int,input().split())
x=list(map(int,input().split()))

diffx=[abs(X-x[i]) for i in range(N)]
minDiff=min(diffx)
isFinished=0
for i in range(minDiff,0,-1):
    if minDiff%i==0:
        canDivide=1
        for j in range(N):
            if diffx[j]%i:
                canDivide=0
                break
        if canDivide:
            print(i)
            isFinished=1
            break
    if isFinished:
        break


Fixed submission:
N,X=map(int,input().split())
x=list(map(int,input().split()))
diffx=[abs(X-x[i]) for i in range(N)]

def euclid(a,b):
    if b==0:
        return a
    else:
        return euclid(b,a%b)

gcd=diffx[0]
for i in range(1,N):
    gcd=euclid(diffx[i],gcd)
print(gcd)

Buggy python submission (verdict: Time Limit Exceeded):
N,X=map(int,input().split())
x=list(map(int,input().split()))

tmp=abs(X-min(x))
isFinished=0
for i in range(tmp,0,-1):
    if tmp%i==0:
        canDivide=1
        for j in range(N):
            tmp=abs(X-x[j])
            if tmp%i:
                canDivide=0
                break
        if canDivide:
            print(i)
            isFinished=1
            break
    if isFinished:
        break


Fixed submission:
N,X=map(int,input().split())
x=list(map(int,input().split()))
diffx=[abs(X-x[i]) for i in range(N)]

def euclid(a,b):
    if b==0:
        return a
    else:
        return euclid(b,a%b)

gcd=diffx[0]
for i in range(1,N):
    gcd=euclid(diffx[i],gcd)
print(gcd)

Buggy python submission (verdict: Time Limit Exceeded):
N,X=map(int,input().split())
x=list(map(int,input().split()))

tmp=abs(X-x[0])
flag=0
for i in range(tmp,0,-1):
    if tmp%i==0:
        canDivide=1
        for j in range(N):
            tmp=abs(X-x[j])
            if tmp%i:
                canDivide=0
                break
        if canDivide:
            print(i)
            flag=1
            break
    if flag:
        break


Fixed submission:
N,X=map(int,input().split())
x=list(map(int,input().split()))
diffx=[abs(X-x[i]) for i in range(N)]

def euclid(a,b):
    if b==0:
        return a
    else:
        return euclid(b,a%b)

gcd=diffx[0]
for i in range(1,N):
    gcd=euclid(diffx[i],gcd)
print(gcd)
Buggy python submission (verdict: Time Limit Exceeded):
S = input()
 
cnt = 0
S = S[::-1]
for i in range(0, len(S)-4):
    found_step = 0
    for j in range(i+5, len(S)+1):
        s = int(S[i+found_step:j][::-1])
        if s < 12114:
            continue
        elif s % 2019 == 0:
            found_step = j - i
            cnt += 1
print(cnt)


Fixed submission:
S = input()
 
mods = [0 for _ in range(2019)]
mods[0] = 1
t = 0
mod10 = 1
for s in reversed(S):
    t += int(s) * mod10
    t = t % 2019
    mod10 *= 10
    mod10 %= 2019
    mods[t] += 1

cnt = sum([m*(m-1)/2 for m in mods])
print(int(cnt))

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

cnt = 0
for i in range(len(S)-4):
    for j in range(i+5, len(S)+1):
        s = int(S[i:j])
        if s % 2019 == 0:
            cnt += 1
print(cnt)


Fixed submission:
S = input()
 
mods = [0 for _ in range(2019)]
mods[0] = 1
t = 0
mod10 = 1
for s in reversed(S):
    t += int(s) * mod10
    t = t % 2019
    mod10 *= 10
    mod10 %= 2019
    mods[t] += 1

cnt = sum([m*(m-1)/2 for m in mods])
print(int(cnt))

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

cnt = 0
for i in range(len(S)-4):
    for j in range(i+5, len(S)+1):
        s = int(S[i:j])
        if s % 2019 == 0:
            cnt += 1
print(cnt)


Fixed submission:
S = input()
 
mods = [0 for _ in range(2019)]
mods[0] = 1
t = 0
mod10 = 1
for s in reversed(S):
    t += int(s) * mod10
    t = t % 2019
    mod10 *= 10
    mod10 %= 2019
    mods[t] += 1

cnt = sum([m*(m-1)/2 for m in mods])
print(int(cnt))
Buggy python submission (verdict: Time Limit Exceeded):
import math
import itertools
from functools import reduce


def gcd(*numbers):
    return reduce(math.gcd, numbers)


K = int(input())
GCD = 0
range_list = list(range(1, K+1))
iterator = itertools.product(range_list, range_list, range_list)
for (a, b, c) in iterator:
    GCD += gcd(a, b, c)

print(GCD)


Fixed submission:
import math
import itertools
from functools import reduce


def gcd(*numbers):
    return reduce(math.gcd, numbers)


K = int(input())
GCD = 0
range_list = list(range(1, K+1))
iterator = itertools.product(range_list, range_list, range_list)
for a in range(1, K+1):
    for b in range(a, K+1):
        for c in range(b, K+1):
            val= gcd(a, b, c)
            if a == b and b == c:
                GCD += val
            elif a == b or b == c:
                GCD += 3 * val
            else:
                GCD += 6 * val

print(GCD)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

h, w = map(int, input().split())
ch, cw = map(int, input().split())
ch -= 1
cw -= 1
Dh, Dw = map(int, input().split())
Dh -= 1
Dw -= 1
s = []
for _ in range(h):
    s.append(list(input()))

def is_valid_point(ph, pw):
    if 0 <= ph < h and 0 <= pw < w and s[ph][pw] == '.':
        return True
    else:
        return False

dh = (1, 0, -1, 0)
dw = (0, 1, 0, -1)

inf = 10 ** 18
dist = [[inf] * w for _ in range(h)]
dist[ch][cw] = 0

q = deque([(ch, cw)])
while q:
    ph, pw = q.popleft()
    for k in range(4):
        nh, nw = ph + dh[k], pw + dw[k]
        if is_valid_point(nh, nw):
            if dist[ph][pw] < dist[nh][nw]:
                dist[nh][nw] = dist[ph][pw]
                q.appendleft((nh, nw))
    for nh in range(ph - 2, ph + 3):
        for nw in range(pw - 2, pw + 3):
            if is_valid_point(nh, nw):
                if dist[ph][pw] + 1 < dist[nh][nw]:
                    dist[nh][nw] = dist[ph][pw] + 1
                    q.append((nh, nw))

if dist[Dh][Dw] == inf:
    print(-1)
else:
    print(dist[Dh][Dw])


Fixed submission:
from collections import deque

h, w = map(int, input().split())
ch, cw = map(int, input().split())
ch -= 1
cw -= 1
Dh, Dw = map(int, input().split())
Dh -= 1
Dw -= 1
s = []
for _ in range(h):
    s.append(list(input()))

def is_valid_point(ph, pw):
    if 0 <= ph < h and 0 <= pw < w and s[ph][pw] == '.':
        return True
    else:
        return False

dh = (1, 0, -1, 0)
dw = (0, 1, 0, -1)

inf = 10 ** 18
dist = [[inf] * w for _ in range(h)]
dist[ch][cw] = 0

q = deque([(ch, cw)])
while q:
    ph, pw = q.popleft()
    for k in range(4):
        nh, nw = ph + dh[k], pw + dw[k]
        if is_valid_point(nh, nw):
            if dist[ph][pw] < dist[nh][nw]:
                dist[nh][nw] = dist[ph][pw]
                q.appendleft((nh, nw))
    for nh in range(ph - 2, ph + 3):
        for nw in range(pw - 2, pw + 3):
            if is_valid_point(nh, nw):
                if dist[ph][pw] + 1 < dist[nh][nw]:
                    dist[nh][nw] = dist[ph][pw] + 1
                    q.append((nh, nw))

if dist[Dh][Dw] == inf:
    print(-1)
else:
    print(dist[Dh][Dw])
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))

a.sort()
a.reverse()

ans = 0
seq = [a[0]]

seq.append(a[1])
ans += a[0]

l = 2
for i in range(2, n):
    pos = 0
    m_ans, M_ans = 0, 0
    for p in range(l):
        np = p + 1
        if np == l:
            np = 0
        m, M = min(seq[p], seq[np]), max(seq[p], seq[np])
        if m >= m_ans and M >= m_ans:
            pos = np
            m_ans = m
            M_ans = M
    ans += m_ans
    seq.insert(pos, a[i])
    l += 1
print(ans)


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

a.sort()
a.reverse()

#ans = 0
#seq = [a[0]]
#
#seq.append(a[1])
#ans += a[0]
#
#l = 2
#for i in range(2, n):
#    pos = 0
#    m_ans, M_ans = 0, 0
#    for p in range(l):
#        np = p + 1
#        if np == l:
#            np = 0
#        m, M = min(seq[p], seq[np]), max(seq[p], seq[np])
#        if m >= m_ans and M >= m_ans:
#            pos = np
#            m_ans = m
#            M_ans = M
#    ans += m_ans
#    seq.insert(pos, a[i])
#    l += 1
#print(ans)

ans = 0
cur = 0
cnt = 1
for i in range(n - 1):
    if cnt == 2:
        cur += 1
        cnt = 0
    ans += a[cur]
    cnt += 1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
def input(): return sys.stdin.readline().strip()

n = int(input())
As = list(map(int, input().split()))
table = [0] * (10 ** 6 + 1)
ans = n
for a in As:
    tmp = a
    table[tmp] += 1
    for _ in range((10 ** 6) // a - 1):
        tmp += a
        table[tmp] += 1
for a in As:
    ans -= (table[a] > 1)
print(ans)


Fixed submission:
import numpy as np
import sys
def input(): return sys.stdin.readline().strip()

n = int(input())
As = list(map(int, input().split()))
ans = n
val = 10 ** 6
table = np.array([0] * (val + 1))
for a in As:
    if table[a] > 1:
        continue
    table[::a] += 1
for a in As:
    ans -= (table[a] > 1)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
As = list(map(int, input().split()))
As.sort()
table = [0] * (10 ** 6 + 1)
ans = n
for a in As:
    tmp = a
    table[tmp] += 1
    for _ in range((10 ** 6) // a - 1):
        tmp += a
        table[tmp] += 1
for a in As:
    ans -= (table[a] > 1)
print(ans)


Fixed submission:
import numpy as np
import sys
def input(): return sys.stdin.readline().strip()

n = int(input())
As = list(map(int, input().split()))
ans = n
val = 10 ** 6
table = np.array([0] * (val + 1))
for a in As:
    if table[a] > 1:
        continue
    table[::a] += 1
for a in As:
    ans -= (table[a] > 1)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
from copy import deepcopy

n, k = map(int, input().split())
As = list(map(int, input().split()))

for _ in range(k):
    if sum(As) == n * n:
        break
    As_next = [0] * n
    for i in range(n):
        for j in range(max(0, i - As[i]), min(n, i + As[i] + 1)):
            As_next[j] += 1
    As = deepcopy(As_next)

print(" ".join(map(str, As)))


Fixed submission:
from copy import deepcopy

n, k = map(int, input().split())
As = list(map(int, input().split()))

for _ in range(k):
    if sum(As) == n * n:
        break
    Bs = [0] * n
    for i in range(n):
        l = max(0, i - As[i])
        r = min(n - 1, i + As[i])
        Bs[l] += 1
        if r + 1 < n:
            Bs[r + 1] -= 1
    for i in range(1, n):
        Bs[i] += Bs[i - 1]
    As = Bs

print(" ".join(map(str, As)))
Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
a = list(map(int, input().split()))

if k <= n:
    takahashi = 0
    for _ in range(k):
        takahashi = a[takahashi] - 1
    print(takahashi + 1)
else:
    start = 0
    takahashi = start
    trajectory = [takahashi]
    loop_start = -1
    loop_end = -1
    for i in range(n):
        takahashi = a[takahashi] - 1
        if takahashi in trajectory:
            loop_start = trajectory.index(takahashi)
            break
        trajectory.append(takahashi)
    print(trajectory[loop_start:][(k - loop_start) % (i + 1 - loop_start)] + 1)


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

if k <= n:
    takahashi = 0
    for _ in range(k):
        takahashi = a[takahashi] - 1
    print(takahashi + 1)
else:
    start = 0
    takahashi = start
    trajectory = [takahashi]
    loop_start = -1
    loop_end = -1
    visited = [False] * n
    visited[0] = True
    for i in range(n):
        takahashi = a[takahashi] - 1
        if visited[takahashi]:
            loop_start = trajectory.index(takahashi)
            break
        visited[takahashi] = True
        trajectory.append(takahashi)
    print(trajectory[loop_start:][(k - loop_start) % (i + 1 - loop_start)] + 1)
Buggy python submission (verdict: Time Limit Exceeded):
def encode(s):
    val = 0
    min_val = 0
    for c in s:
        if c == '(':
            val += 1
        else:
            val -= 1
            min_val = min(min_val, val)
    return [min_val, val]

def check(s):
    h = 0
    for p in s:
        b = h + p[0]
        if b < 0:
            return False
        h += p[1]
    return True


n = int(input())
ls = []
rs = []
l_total = 0
r_total = 0
for _ in range(n):
    si = encode(input())
    if si[1] > 0:
        ls.append(si)
        l_total += si[1]
    else:
        si[0] -= si[1]
        si[1] *= -1
        rs.append(si)
        r_total += si[1]
list.sort(ls, reverse=True)
list.sort(rs, reverse=True)
if check(ls) and check(rs) and l_total == r_total:
    print("Yes")
else:
    print("No")


Fixed submission:
def encode(s):
    val = 0
    min_val = 0
    for c in s:
        if c == '(':
            val += 1
        else:
            val -= 1
            min_val = min(min_val, val)
    return [min_val, val]

def check(s):
    h = 0
    for p in s:
        b = h + p[0]
        if b < 0:
            return False
        h += p[1]
    return True


n = int(input())
ls = []
rs = []
l_total = 0
r_total = 0
for _ in range(n):
    si = encode(input())
    if si[1] > 0:
        ls.append(si)
        l_total += si[1]
    else:
        si[0] -= si[1]
        si[1] *= -1
        rs.append(si)
        r_total += si[1]
list.sort(ls, reverse=True)
list.sort(rs, reverse=True)
if check(ls) and check(rs) and l_total == r_total:
    print("Yes")
else:
    print("No")