Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
n,m = map(int,input().split())
ls = []
switch = []
for i in range(n):
    switch.append(i+1)
for i in range(m):
    ks = list(map(int,input().split()))
    ls.append(ks)
p = list(map(int,input().split()))


pattern = []
for i in range(2**n):
    pattern.append(list(bin(i)[2:].zfill(n)))

ans = 0

for i in pattern:
    turn = []
    count = 0
    on = 0
    for j in range(n):
        if i[j] == '1':
            turn.append(switch[j])
    for k in range(m):
        for l in range(1,ls[k][0]+1):
            if ls[k][l] in turn:
                count += 1
        if count % 2 == p[k]:
            on += 1
    if on == m:
        ans += 1
            
print(ans)

Fixed submission:
n = int(input())
ls = []
for i in range(n):
    s,t = input().split()
    ls.append([s,-int(t),i+1])
    
ls = sorted(ls)
for i in ls:
  print(i[-1])
Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
key = []
for i in range(1,n+1):
    key.append(i)
ans = []
for i in range(m):
    ls = []
    L,R = map(int,input().split())
    for j in range(L,R+1):
        ls.append(j)
    key = (set(ls)&set(key))
    
print(len(key))
    


Fixed submission:
n,m = map(int,input().split())
key = []
for i in range(1,n+1):
    key.append(i)
    
ls = [0,100000]
for i in range(m):
    L,R = map(int,input().split())
    if L > ls[0]:
        ls[0] = L
    if R < ls[1]:
        ls[1] = R
    
ans = []    
for i in range(ls[0],ls[1]+1):
    ans.append(i)
    
print(len(set(ans)&set(key)))
Buggy python submission (verdict: Runtime Error):
from fractions import gcd
import functools

n = int(input())
a = input().split()

i = 0
list = []
while i < n:
    list.append(int(a[i]))
    i += 1
    
gc = functools.reduce(gcd, list)
print(gc)

Fixed submission:
import math

abt = input().split()

count = math.floor(int(abt[2]) / int(abt[0])) * int(abt[1])
print(count)

Buggy python submission (verdict: Runtime Error):
import gcd
import functools

n = int(input())
a = input().split()

i = 0
list = []
while i < n:
    list.append(int(a[i]))
    i += 1
    
gc = functools.reduce(gcd, list)
print(gc)

Fixed submission:
import math

abt = input().split()

count = math.floor(int(abt[2]) / int(abt[0])) * int(abt[1])
print(count)
Buggy python submission (verdict: Runtime Error):
import math
a = 0
b = 0
c = 0
d = 0
e = 0
ls = [a,b,c,d,e]
for i in range(len(ls)):
    ls[i] = int(input())
for i in ls:
    if i > 9:
        i = i/10
ans = 0
for i in ls:
    ans += math.ceil(i)

one = 0
lsls = []
for i in range(len(ls)):
    ls_one = str(ls[i])
    one += int(ls_one[-1])
    if int(ls_one[-1]) > 0:
    	lsls.append(10-int(ls_one[-1]))
    
lsls.remove(max(lsls))
print(ans+sum(lsls))

Fixed submission:
import math
a = 0
b = 0
c = 0
d = 0
e = 0
ls = [a,b,c,d,e]
for i in range(len(ls)):
    ls[i] = int(input())
for i in ls:
    if i > 9:
        i = i//10
ans = 0
for i in ls:
    ans += math.ceil(i)

lsls = [0]
for i in range(len(ls)):
    ls_one = str(ls[i])
    if int(ls_one[-1]) > 0:
    	lsls.append(10-int(ls_one[-1]))
    
lsls.remove(max(lsls))
print(ans+sum(lsls))

Buggy python submission (verdict: Runtime Error):
import math
a = 0
b = 0
c = 0
d = 0
e = 0
ls = [a,b,c,d,e]
for i in range(len(ls)):
    ls[i] = int(input())
for i in ls:
    if i > 9:
        i = i//10
ans = 0
for i in ls:
    ans += math.ceil(i)

one = 0
lsls = []
for i in range(len(ls)):
    ls_one = str(ls[i])
    one += int(ls_one[-1])
    if int(ls_one[-1]) > 0:
    	lsls.append(10-int(ls_one[-1]))
    
lsls.remove(max(lsls))
print(ans+sum(lsls))

Fixed submission:
import math
a = 0
b = 0
c = 0
d = 0
e = 0
ls = [a,b,c,d,e]
for i in range(len(ls)):
    ls[i] = int(input())
for i in ls:
    if i > 9:
        i = i//10
ans = 0
for i in ls:
    ans += math.ceil(i)

lsls = [0]
for i in range(len(ls)):
    ls_one = str(ls[i])
    if int(ls_one[-1]) > 0:
    	lsls.append(10-int(ls_one[-1]))
    
lsls.remove(max(lsls))
print(ans+sum(lsls))
Buggy python submission (verdict: Time Limit Exceeded):
n,q = map(int,input().split())
s = input()
for i in range(q):
    l,r = map(int,input().split())
    ans = 0
    for j in range(l-1,r-1):
        if s[j]=="A" and s[j+1]=="C":
            ans += 1
    print(ans)
            

Fixed submission:
n,q = map(int,input().split())
s = input()
    
ans = 0
ls = [0]
for i in range(len(s)-1):
    if s[i] == "A" and s[i+1] == "C":
        ans += 1
        ls.append(1+ls[i-1])
    else:
        ls.append(ls[i])
        
for i in range(q):
    l,r = map(int,input().split())
    print(ls[r-1]-ls[l-1])
Buggy python submission (verdict: Runtime Error):
n = int(input())
h = list(map(int,input().split()))
ans = 0
index = 0
if n == 2:
    print(abs(h[index]-h[index+1]))
else:
    for i in range(n):
        if abs(h[index]-h[index+1]) < abs(h[index]-h[index+2]):
            ans += abs(h[index]-h[index+1])
            index += 1
        elif abs(h[index]-h[index+1]) > abs(h[index]-h[index+2]):
            ans += abs(h[index]-h[index+2])
            index += 2
        else:
            ans += abs(h[index]-h[index+2])
            index += 2
        if index == n-1:
            print(ans)
            break

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

dp = [0,abs(h[1]-h[0])]

for i in range(2,n):
    dp.append(min(dp[i-1] + abs(h[i]-h[i-1]),dp[i-2] + abs(h[i]-h[i-2])))
    
print(dp[-1])
Buggy python submission (verdict: Runtime Error):
s = list(input())
for i in range(len(s)):
  if ls[i] == "9":
    ls[i] = "1"
  elif ls[i] == "1":
    ls[i] = "9":
for i in s:
  print(i,end="")

Fixed submission:
s = list(input())
for i in range(len(s)):
    if s[i] == "9":
        s[i] = "1"
    elif s[i] == "1":
        s[i] = "9"
for i in s:
    print(i,end="")

Buggy python submission (verdict: Runtime Error):
s = list(input())
for i in s:
  if i == 9:
    i = 1
  elif i == 1:
    i = 9:
for i in s:
  print(i,end="")

Fixed submission:
s = list(input())
for i in range(len(s)):
    if s[i] == "9":
        s[i] = "1"
    elif s[i] == "1":
        s[i] = "9"
for i in s:
    print(i,end="")
Buggy python submission (verdict: Runtime Error):
n = list(int(input()))
for i in range(3):
  print(max(n))

Fixed submission:
n = list(input())
ls = []
for i in range(3):
  ls.append(int(n[i]))
  
if ls[0] >= ls[1] and ls[0] >= ls[2]:
  for i in range(3):
    print(ls[0],end="")
else:
  for i in range(3):
    print(ls[0]+1,end="")
Buggy python submission (verdict: Runtime Error):
n,m,x,y = map(int,input().split())
X = map(int,input().split())
Y = map(int,input().split())
if max(X)+1 <= min(Y) and max(X) > x and min(Y) <= y:
    print("No War")
else:
    print("War")

Fixed submission:
n,m,x,y = map(int,input().split())
X = list(map(int,input().split()))
Y = list(map(int,input().split()))
if max(X)+1 <= min(Y) and max(X) > x and min(Y) <= y:
    print("No War")
else:
    print("War")
Buggy python submission (verdict: Runtime Error):
a,b,c = map(int,input().split())
k = int(input())
ans = max(a,b,c)
for i in range(k):
  ans = ans + ans
print(ans+min(a,b,c)+a+b+c-max(a,b,c)-min(a,b,c)

Fixed submission:
a,b,c = map(int,input().split())
k = int(input())
ans = max(a,b,c)
for i in range(k):
  ans = ans + ans
print(ans+min(a,b,c)+a+b+c-max(a,b,c)-min(a,b,c))

Buggy python submission (verdict: Runtime Error):
a,b,c = map(int,input().split())
k = int(input())
ans = max(a,b,c)
for i in range(k):
  ans = ans × 2
print(ans)


Fixed submission:
a,b,c = map(int,input().split())
k = int(input())
ans = max(a,b,c)
for i in range(k):
  ans = ans + ans
print(ans+min(a,b,c)+a+b+c-max(a,b,c)-min(a,b,c))

Buggy python submission (verdict: Runtime Error):
a,b,c = map(int,input().split())
k = int(input())
ans = max(a,b,c)
for i in range(k):
  ans = ans×2
print(ans)


Fixed submission:
a,b,c = map(int,input().split())
k = int(input())
ans = max(a,b,c)
for i in range(k):
  ans = ans + ans
print(ans+min(a,b,c)+a+b+c-max(a,b,c)-min(a,b,c))

Buggy python submission (verdict: Runtime Error):
a,b,c = map(int,input().split())
k = int(input())
ans = max(a,b,c)
for i in range(k):
  ans ×= 2
print(ans)


Fixed submission:
a,b,c = map(int,input().split())
k = int(input())
ans = max(a,b,c)
for i in range(k):
  ans = ans + ans
print(ans+min(a,b,c)+a+b+c-max(a,b,c)-min(a,b,c))
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int,input().split()))
ls = [a[0]]
for i in range(1,n):
    ls.append(a[i]+ls[i-1])
    
ans = 0
k = 1
for i in range(n):
    if ls[i] == 0:
      ans += 1
    for j in range(k,n):
        if ls[i] == ls[j]:
            ans += 1
    k += 1

print(ans)

Fixed submission:
import collections
n = int(input())
a = list(map(int,input().split()))
ls = [a[0]]
for i in range(1,n):
    ls.append(a[i]+ls[i-1])
    
ans = 0
ans += ls.count(0)
c = collections.Counter(ls)
c = (c.most_common())
i = 0
while True:
  if i == len(c):
    break
  else:
    ans += c[i][1]*(c[i][1]-1)/2
  i += 1
    
print(round(ans))

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int,input().split()))
ls = [a[0]]
for i in range(1,n):
    ls.append(a[i]+ls[i-1])
    
ans = 0
k = 1
for i in range(n):
    if ls[i] == 0:
      ans += 1
    for j in range(k,n):
        if ls[i] == ls[j]:
            ans += 1
    k += 1

print(ans)

Fixed submission:
import collections
n = int(input())
a = list(map(int,input().split()))
ls = [a[0]]
for i in range(1,n):
    ls.append(a[i]+ls[i-1])
    
ans = 0
ans += ls.count(0)
c = collections.Counter(ls)
c = (c.most_common())
i = 0
while True:
  if i == len(c):
    break
  else:
    ans += c[i][1]*(c[i][1]-1)/2
  i += 1
    
print(round(ans))
Buggy python submission (verdict: Runtime Error):
a,b,c = map(int,input().split())
if a+b <= c:
  print("Yes)
else:
  print("No")

Fixed submission:
a,b,c = map(int,input().split())
if a+b >= c:
	print("Yes")
else:
	print("No")

Buggy python submission (verdict: Runtime Error):
a,b,c = map(int,input().split())
if a+b <= c:
	print("Yes)
else:
	print("No")

Fixed submission:
a,b,c = map(int,input().split())
if a+b >= c:
	print("Yes")
else:
	print("No")
Buggy python submission (verdict: Runtime Error):
n = int(input())
a = list(map(int,input().split()))
alice = 0
bob = 0
for i in range(1,n+1):
  if i%2==1:
    alice += max(a)
    del max(a)
  else:
    bob += max(a)
    del　max(a)

Fixed submission:
n = int(input())
a = list(map(int,input().split()))
alice = 0
bob = 0
for i in range(1,n+1):
  if i%2==1:
    alice += max(a)
    a.remove(max(a))
  else:
    bob += max(a)
    a.remove(max(a))
    
print(alice-bob)
Buggy python submission (verdict: Time Limit Exceeded):
n,y = map(int,input().split())
p = 0
for i in range(n+1):
    for j in range(n+1):
        for k in range(n+1):
            if 1000*i+5000*j+10000*k == y:
                if i+j+k == n:
                    print(k,j,i)
                    p += 1
                    break
    if p == 1:
        break
            
    
if p == 0:
    print("-1 -1 -1")
    

Fixed submission:
n,y = map(int,input().split())
p = 0
for i in range(n+1):
    for j in range(n+1):
        k = (n-i-j)
        if k < 0:
            k = 0
        if 1000*i+5000*j+10000*k == y:
            if i+j+k == n:
                print(k,j,i)
                p += 1
                break       
    if p == 1:
        break
            
    
if p == 0:
    print("-1 -1 -1")
Buggy python submission (verdict: Runtime Error):
a,b = map(int,input().split())
s = input()
if s[a] == "-":
    s.remove(s[a])
    if "-" in s:
        print("No")
    else:
        print("Yes")

Fixed submission:
a,b = map(int,input().split())
s = list(input())
if s[a] == "-":
    s.remove(s[a])

    ls = "0123456789"
    c = 0
    for i in s:
        if i in ls:
            c += 1

    if c == a+b:
        print("Yes")
    else:
        print("No")
else:
    print("No")
Buggy python submission (verdict: Runtime Error):
x,y = map(str,input().split())
dic = {10:"A",11:"B",12:"C",13:"D",14:"E",15:"F"}
if x == y:
  print("=")
elif dic[x] > dic[y]:
  print(">")
else:
  print("<")


Fixed submission:
x,y = map(str,input().split())
dic = {"A":10,"B":11,"C":12,"D":13,"E":14,"F":15}
if x == y:
  print("=")
elif dic[x] > dic[y]:
  print(">")
else:
  print("<")

Buggy python submission (verdict: Runtime Error):
x,y = map(str,input().split())
dic = {10:"A",11:"B",12:"C",13:"D",14:"E",15:"F"}
if x == y:
  print("=")
elif dic{x} > dic{y}:
  print(">")
else:
  print("<")


Fixed submission:
x,y = map(str,input().split())
dic = {"A":10,"B":11,"C":12,"D":13,"E":14,"F":15}
if x == y:
  print("=")
elif dic[x] > dic[y]:
  print(">")
else:
  print("<")
Buggy python submission (verdict: Runtime Error):
h,w = map(int,input().split())
for i in range(w):
    print("#",end="")
print("")
for i in range(h):
    a = input()
    print("#" + a + "#")

for i in range(w):    
    print("#",end=")

Fixed submission:
h,w = map(int,input().split())
for i in range(w+2):
    print("#",end="")
print("")
for i in range(h):
    a = input()
    print("#" + a + "#")

for i in range(w+2):    
    print("#",end="")
Buggy python submission (verdict: Runtime Error):
a,b = int(input())
b = int(input())
if a == b:
    print("EQUAL")
elif a > b:
    print("GREATER")
else:
    print("LESS")

Fixed submission:
a = int(input())
b = int(input())
if a == b:
    print("EQUAL")
elif a > b:
    print("GREATER")
else:
    print("LESS")
Buggy python submission (verdict: Runtime Error):
a,b,c = map(int,input().split())
if b−a == c−b:
  print("YES")
else:
  print("NO")

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

if (b-a) == (c-b):
  print("YES")
else:
  print("NO")
Buggy python submission (verdict: Runtime Error):
a,b,c,d = map(int,input().split())
print(max(a*b,c*d)

Fixed submission:
a,b,c,d = map(int,input().split())
print(max(a*b,c*d))
Buggy python submission (verdict: Runtime Error):
9 20
.....***....***.....
....*...*..*...*....
...*.....**.....*...
...*.....*......*...
....*.....*....*....
.....**..*...**.....
.......*..*.*.......
........**.*........
.........**.........


Fixed submission:
h,w = map(int,input().split())
for i in range(h):
    c = input()
    print(c)
    print(c)
Buggy python submission (verdict: Runtime Error):
a,b,c = map(int,input().split())
if a+b = c:
  print("Yes")
elif b+c = a:
  print("Yes")
elif c+a=b:
  print("Yes")
else:
  print("No")

Fixed submission:
a,b,c = map(int,input().split())
if a+b == c:
  print("Yes")
elif b+c == a:
  print("Yes")
elif c+a== b:
  print("Yes")
else:
  print("No")
Buggy python submission (verdict: Runtime Error):
n = int(input())
ans = 0
for i in range(a,n):
  ans += i
  
print(ans)

Fixed submission:
n = int(input())
ans = 0
for i in range(1,n+1):
  ans += i
  
print(ans)
Buggy python submission (verdict: Runtime Error):
a,b,c = map(int,input().split())
if (a > 0 and b > 0) or (a > b and a > 0) or (b > a and b > 0):
    print("Positive")
elif (a-b) == 0 or (b-a) == 0:
    print("Zero")
elif (a < 0 and b < 0) or (a > b and a < 0) or (b > a and b < 0):
    print("Negative")
print()

Fixed submission:
import sys
a,b = map(int,input().split())
c = 0
if (a+b) == 0:
    print("Zero")
    sys.exit()
elif a < 0 and b < 0:
    if  (a-b) % 2 == 1:
        print("Positive")
        sys.exit()
elif a < 0 and b >= 0:
    if a%2 == 0:
        print("Positive")
        sys.exit()
elif b < 0 and a >= 0:
    if b % 2 == 0:
        print("Positive")
        sys.exit()
if (a+b) > 0:
    print("Positive")
else:
    print("Negative")

Buggy python submission (verdict: Runtime Error):
a,b,c = map(int,input().split())
if (a > 0 and b > 0) or (a > b and a > 0) or (b > a and b > 0):
    print("Positive")
elif (a-b) == 0 or (b-a) == 0:
    print("Zero")
elif (a < 0 and b < 0) or (a > b and a < 0) or (b > a and b < 0):
    print("Negative")

Fixed submission:
import sys
a,b = map(int,input().split())
c = 0
if (a+b) == 0:
    print("Zero")
    sys.exit()
elif a < 0 and b < 0:
    if  (a-b) % 2 == 1:
        print("Positive")
        sys.exit()
elif a < 0 and b >= 0:
    if a%2 == 0:
        print("Positive")
        sys.exit()
elif b < 0 and a >= 0:
    if b % 2 == 0:
        print("Positive")
        sys.exit()
if (a+b) > 0:
    print("Positive")
else:
    print("Negative")
Buggy python submission (verdict: Runtime Error):
a=int(input())
b=float(input())
a*=int(b*100)
print(int(a/100))

Fixed submission:
a, b = input().split()
print(int(a)*int(b[0]+b[2]+b[3])//100)

Buggy python submission (verdict: Runtime Error):
a=int(input())
b=float(input())
a*=int(b*100)
print(int(a/100))

Fixed submission:
a, b = input().split()
print(int(a)*int(b[0]+b[2]+b[3])//100)
Buggy python submission (verdict: Runtime Error):
n,k=int(input().split());print(0--~-n//~-k)

Fixed submission:
n,k,*_=map(int,open(0).read().split())
print(0--~-n//~-k)

Buggy python submission (verdict: Runtime Error):
n,k=map(int,open(0).split())
print(0--~-n//~-k)

Fixed submission:
n,k,*_=map(int,open(0).read().split())
print(0--~-n//~-k)

Buggy python submission (verdict: Runtime Error):
a,b=int(input())
for x in input().split():
  print(x)

Fixed submission:
n,k,*_=map(int,open(0).read().split())
print(0--~-n//~-k)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
A = [int(input()) for _ in range(n)]
B = list(set(A))
ans = 0

for i in B:
    if A.count(i) % 2 == 1:
        ans += 1

print(ans)


Fixed submission:
n=int(input())
d=dict()
for _ in range(n):
    hoge=int(input())
    if d.get(hoge,0)==0:
        d[hoge]=1
    else:
        d[hoge]+=1
ans=0
for i in d.values():
    if i%2==1:
        ans+=1
print(ans)
Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

if a >= 0 and c >= 0:
    print(b * d)
    sys.exit(0)

if b <= 0 and d <= 0:
    print(a * c)
    sys.exit(0)

if a <= 0 <= b and c <= 0 <= d:
    print(max(a * c, b * d))
    sys.exit(0)

if b <= 0 and c >= 0:
    pritn(b * c)
    sys.exit(0)

if d <= 0 and a >= 0:
    print(a * d)
    sys.exit(0)

if a <= 0 <= b and d <= 0:
    print(0)
    sys.exit(0)

if c <= 0 <= d and b <= 0:
    print(0)
    sys.exit(0)


Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

ans = max(a * c, a * d, b * c, b * d)

if ans < 0 and (a <= 0 <= b or c <= 0 <= d):
    print(0)
else:
    print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

n = int(input())

total = 0
while n:
    total += (n % 10)
    n //= 10

if total % 9 == 0:
    print('Yes')
else:
    print('No')


Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

n = int(input())

if n % 9 == 0:
    print('Yes')
else:
    print('No')

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

n = int(input())

total = 0
while n:
    total += (n % 10)
    n //= 10

if total % 9 == 0:
    print('Yes')
else:
    print('No')


Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

n = int(input())

if n % 9 == 0:
    print('Yes')
else:
    print('No')
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

n, k = map(int, input().split())
p = [0] + list(map(int, input().split()))
c = [0] + list(map(int, input().split()))

dp = {}
for i in range(1, n + 1):
    dp[(i, 0)] = 0
    dp[(i, 1)] = c[p[i]]

def score(i, k, route):
    if (i, k) in dp:
        return dp[(i, k)]
    else:
        if i in route and k >= len(route):
            tmp_ans = 0
            for r in route:
                tmp_ans += dp[(r, 1)]
            return (tmp_ans * (k // len(route))) + score(p[i], k % len(route), route)
        else:
            route = route.copy()
            route.add(i)
            return c[p[i]] + score(p[i], k - 1, route)

ans = None
for i in range(1, n + 1):
    for j in range(1, k + 1):
        tmp_ans = score(i, j, set())
        if ans == None:
            ans = tmp_ans
        else:
            ans = max(ans, tmp_ans)
print(ans)


Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

n, k = map(int, input().split())
p = [0] + list(map(int, input().split()))
c = [0] + list(map(int, input().split()))

from collections import deque

def bfs(queue, cycle_index, cycle_num):
    cycle = []
    score = 0
    while queue:
        current = queue.popleft()
        cycle_index[current] = cycle_num
        cycle.append(current)
        score += c[current]

        if p[current] not in cycle_index:
            queue.append(p[current])
    return cycle, score

cycle_index = {}
cycle_num = 0
cycles = []
for i in range(1, n + 1):
    if i in cycle_index:
        continue
    queue = deque([i])
    cycle, score = bfs(queue, cycle_index, cycle_num)
    cycles.append((cycle, score))
    cycle_num += 1

ans = None
for i in range(1, n + 1):
    cycle, score = cycles[cycle_index[i]]
    tmp_ans = 0

    cum = [0]
    current = i
    for j in range(1, len(cycle)):
        current = p[current]
        cum.append(cum[-1] + c[current])

    if score > 0:
        q = (k // len(cycle))
        r = k % len(cycle)
        # 一周しない方がいいケースがある
        # FYI: https://atcoder.jp/contests/abc175/submissions/15968040
        if r == 0:
            tmp_ans = max((score * q), (score * (q - 1)) + max(cum[1:]))
        else:
            tmp_ans = (score * q) + max(cum[1:r+1])
    else:
        r = min(k, len(cycle))
        tmp_ans = max(cum[1:r+1])

    if ans is None:
        ans = tmp_ans
    else:
        ans = max(ans, tmp_ans)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

n, k = map(int, input().split())
a = [0] + list(map(int, input().split()))

route = [1]
index = k
for i in range(n+1):
    _next = a[route[-1]]
    if _next in route:
        route.append(_next)
        index -= (i+1)
        break
    route.append(_next)

if index <= 0:
    print(route[k])
else:
    route = route[route.index(route[-1]):-1]
    print(route[index % len(route)])


Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

n, k = map(int, input().split())
a = [0] + list(map(int, input().split()))

route = [1]
index = k
hit = set([1])
for i in range(n+1):
    _next = a[route[-1]]
    if _next in hit:
        route.append(_next)
        index -= (i+1)
        break
    route.append(_next)
    hit.add(_next)

if index <= 0:
    print(route[k])
else:
    route = route[route.index(route[-1]):-1]
    print(route[index % len(route)])
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

import collections

s = collections.deque(input().strip())
num = int(input())
query = [0] * num
for i in range(num):
    query[i] = input().strip()

for q in query:
    if q[0] == '1':
        s.reverse()
    else:
        _, f, c = q.split()
        if f == '1':
            s.appendleft(c)
        else:
            s.append(c)
print(''.join(list(s)))


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

import collections

s = collections.deque(input().strip())
num = int(input())
query = [0] * num
for i in range(num):
    query[i] = input().strip()

reverse_flag = False
for q in query:
    if q[0] == '1':
        reverse_flag = not reverse_flag
    else:
        _, f, c = q.split()
        if (f == '1' and not reverse_flag) or (f == '2' and reverse_flag):
            s.appendleft(c)
        if (f == '2' and not reverse_flag) or (f == '1' and reverse_flag):
            s.append(c)
if reverse_flag:
    s.reverse()
print(''.join(list(s)))