Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
n,q = map(int, input().split())
s = input()
t = [0]*n
for i in range(n):
    t[i] = len([0 for j in range(i) if s[j:j+2] == "AC"])
for i in range(q):
    r,l = map(int, input().split())
    print(t[l-1]-t[r-1])

Fixed submission:
N, Q = map(int, input().split())
S = input()
t = [0] * (N + 1)
for i in range(N):
	t[i + 1] = t[i] + (1 if S[i : i + 2] == 'AC' else 0)
for i in range(Q):
	l, r = map(int, input().split())
	print(t[r-1] - t[l-1])

Buggy python submission (verdict: Time Limit Exceeded):
n,q = map(int, input().split())
s = input()
t = [s[:i].count("AC") for i in range(1,n+1)]
for i in range(q):
    r,l = map(int, input().split())
    print(t[l-1]-t[r-1])

Fixed submission:
N, Q = map(int, input().split())
S = input()
t = [0] * (N + 1)
for i in range(N):
	t[i + 1] = t[i] + (1 if S[i : i + 2] == 'AC' else 0)
for i in range(Q):
	l, r = map(int, input().split())
	print(t[r-1] - t[l-1])

Buggy python submission (verdict: Time Limit Exceeded):
n,q = map(int, input().split())
s = input()
ss = s
t = []
for i in range(s.count("AC")):
  t.append(ss.index("AC")+1)
  u = ss.index("AC")+2
  ss = 'Q'*u+ss[u:]
for i in range(q):
  l,r = map(int, input().split())
  print(len(["" for i in t if l <= i and i+1 <= r]))

Fixed submission:
N, Q = map(int, input().split())
S = input()
t = [0] * (N + 1)
for i in range(N):
	t[i + 1] = t[i] + (1 if S[i : i + 2] == 'AC' else 0)
for i in range(Q):
	l, r = map(int, input().split())
	print(t[r-1] - t[l-1])

Buggy python submission (verdict: Runtime Error):
n,q = map(int, input().split())
s = input()
t = []
for i in range(len(s)):
  if s[i] == "A" and s[i+1] == "C":
    t.append(i+1)
for i in range(q):
  l,r = map(int, input().split())
  print(len([1 for i in t if l <= i and i+1 <= r]))

Fixed submission:
N, Q = map(int, input().split())
S = input()
t = [0] * (N + 1)
for i in range(N):
	t[i + 1] = t[i] + (1 if S[i : i + 2] == 'AC' else 0)
for i in range(Q):
	l, r = map(int, input().split())
	print(t[r-1] - t[l-1])

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())
  l -= 1
  print(s[l:r].count("AC"))

Fixed submission:
N, Q = map(int, input().split())
S = input()
t = [0] * (N + 1)
for i in range(N):
	t[i + 1] = t[i] + (1 if S[i : i + 2] == 'AC' else 0)
for i in range(Q):
	l, r = map(int, input().split())
	print(t[r-1] - t[l-1])
Buggy python submission (verdict: Runtime Error):
a,b,c = map(int, input().spit())
if b//a < c:
  print(b//a)
else:
  print(c)

Fixed submission:
a,b,c = map(int, input().split())
if b//a < c:
  print(b//a)
else:
  print(c)
Buggy python submission (verdict: Runtime Error):
n,m = map(int, input().split())
x = [int(i) for i in input().split()]
x.sort()
x2 = []
for i in range(len(x)-1):
  x2.append(x[i+1] - x[i])
x2.sort()
if n < m:
  print(sum(x2[:len(x2) - n + 1]))
else:
  print('0')

Fixed submission:
t,x = map(int ,input().split())
print(t/x)
Buggy python submission (verdict: Runtime Error):
n = int(input())
l = [int(i) for i in input().split()]
if max(l) < sum([i for i in l if i != max[l]]):
  print('Yes')
else:
  print('No')

Fixed submission:
n = int(input())
l = [int(i) for i in input().split()]
if max(l) < sum(l)/2:
  print('Yes')
else:
  print('No')
Buggy python submission (verdict: Runtime Error):
n,m = map(int, input().split())
x = [int(i) for i in input().split()]
x.sort()
x2 = []
for i in range(len(x)-1):
  x2.append(x[i+1] - x[i])
x2.sort()
if n < m:
  print(sum(x2[:len(x2) - n + 1]))
else:
  print('0')
n,m = map(int, input().split())
x = [int(i) for i in input().split()]
x.sort()
x2 = []
for i in range(len(x)-1):
  x2.append(x[i+1] - x[i])
x2.sort()
if n < m:
  print(sum(x2[:len(x2) - n + 1]))
else:
  print('0')

Fixed submission:
n,m = map(int, input().split())
x = [int(i) for i in input().split()]
x.sort()
x2 = []
for i in range(len(x)-1):
  x2.append(x[i+1] - x[i])
x2.sort()
if n < m:
  print(sum(x2[:len(x2) - n + 1]))
else:
  print('0')
Buggy python submission (verdict: Runtime Error):
n,a,b = map(int, input().slit())
if a > b:
  c = b
else:
  c = a
if 0 <= a + b - n:
  d = a + b -n
else:
  d = 0
print(c,d)

Fixed submission:
n,a,b = map(int, input().split())
if a > b:
  c = b
else:
  c = a
if 0 <= a + b - n:
  d = a + b -n
else:
  d = 0
print(c,d)
Buggy python submission (verdict: Time Limit Exceeded):
s = input()
c = 0
while 'BW' in s:
  c += 1
  s = s.replace("BW", "WB", 1)
print(c)

Fixed submission:
s = input()
t = list(s).count('W')
a = [i for i, x in enumerate(list(s)) if x == 'W']
b = int((t - 1) * t / 2)
print(sum(a)-b)
Buggy python submission (verdict: Runtime Error):
d = int(input())
if d = 25:
  print('Christmas')
elif d = 24:
  print('Christmas Eve')
elif d = 23:
  print('Christmas Eve Eve')
else:
  print('Christmas Eve Eve Eve')

Fixed submission:
d = int(input())
if d == 25:
  print('Christmas')
elif d == 24:
  print('Christmas Eve')
elif d == 23:
  print('Christmas Eve Eve')
else:
  print('Christmas Eve Eve Eve')

Buggy python submission (verdict: Runtime Error):
d = int(input())
if d = 25:
  print('Christmas')
elif d = 24:
  print('Christmas Eve')
elif d = 23:
  print('Christmas Eve Eve')
else d = 22:
  print('Christmas Eve Eve Eve')

Fixed submission:
d = int(input())
if d == 25:
  print('Christmas')
elif d == 24:
  print('Christmas Eve')
elif d == 23:
  print('Christmas Eve Eve')
else:
  print('Christmas Eve Eve Eve')

Buggy python submission (verdict: Runtime Error):
D = int(input())
if d = 25:
  print('Christmas')
elif d = 24:
  print('Christmas Eve')
elif d = 23:
  print('Christmas Eve Eve')
else d = 22:
  print('Christmas Eve Eve Eve')

Fixed submission:
d = int(input())
if d == 25:
  print('Christmas')
elif d == 24:
  print('Christmas Eve')
elif d == 23:
  print('Christmas Eve Eve')
else:
  print('Christmas Eve Eve Eve')
Buggy python submission (verdict: Runtime Error):
s = input()
g = float('inf')
for i in range(len(s)-2):
  if g > abs(int(s[i:i+3])-753):
  g = abs(int(s[i:i+3])-753)
print(g)

Fixed submission:
s = input()
g = float('inf')
for i in range(len(s)-2):
  if g > abs(int(s[i:i+3])-753):
  	g = abs(int(s[i:i+3])-753)
print(g)
Buggy python submission (verdict: Runtime Error):
x = input().split()
N,T = int(x[0]),int(x[1])
a,b,c,d = [],[],[],[]
for i in range(N):
    v = input().split()
    v = [int(s) for s in v]
    a.append(v)
for i in range(N):
    b.append(a[i][0])
for i in range(N):
    c.append(a[i][1])
for i in range(N):
    if b[i] < T:
        d.append(a[b.index(b[i])][0])
if len([i for i, x in enumerate(d) if x == min(d)]) == 1 and [i for i, x in enumerate(d) if x == min(d)] != [0]:
    print(min(d))
else:
    print('TLE')

Fixed submission:
a = int(input())
if a == 1:
    print('Hello World')
else:
    b = int(input())
    c = int(input())
    print(b + c)

Buggy python submission (verdict: Runtime Error):
x = input().split()
N,T = int(x[0]),int(x[1])
a = []
b = []
c = []
d = []
for i in range(N):
    v = input().split()
    v = [int(s) for s in v]
    a.append(v)
for i in range(N):
    b.append(a[i][0])
for i in range(N):
    c.append(a[i][1])
for i in range(N):
    if b[i] < T:
        d.append(a[b.index(b[i])][0])
if len([i for i, x in enumerate(d) if x == min(d)]) == 1 and [i for i, x in enumerate(d) if x == min(d)] != [0]:
    print(min(d))
else:
    print('TLE')

Fixed submission:
a = int(input())
if a == 1:
    print('Hello World')
else:
    b = int(input())
    c = int(input())
    print(b + c)
Buggy python submission (verdict: Runtime Error):
n,m=map(int,input())
a,b,c,d=[],[],[],[]
for i in range(n):
  o = list(map(int, input().split()))
  a.append(o[0])
  b.append(o[1])
for i in range(m):
  o = list(map(int, input().split()))
  c.append(o[0])
  d.append(o[1])
s = []
for i in range(n):
  g = float('inf')
  h = 0
  for j in range(m):
    if g > abs(a[i]-c[j])+abs(b[i]-d[j]):
      g = abs(a[i]-c[j])+abs(b[i]-d[j])
      h = j+1
  s.append(h)
for i in s:
  print(i)

Fixed submission:
n,m=map(int,input().split())
a,b,c,d=[],[],[],[]
for i in range(n):
    o = list(map(int, input().split()))
    a.append(o[0])
    b.append(o[1])
for i in range(m):
    o = list(map(int, input().split()))
    c.append(o[0])
    d.append(o[1])
s = []
for i in range(n):
    g = float('inf')
    h = 0
    for j in range(m):
        if g > abs(a[i]-c[j])+abs(b[i]-d[j]):
            g = abs(a[i]-c[j])+abs(b[i]-d[j])
            h = j+1
    s.append(h)
for i in s:
    print(i)
Buggy python submission (verdict: Runtime Error):
import math
n = int(input())
def f(n):
  g = float('inf')
  for i in [j for j in range(1,math.ceil(n**0.5))+1 if n%j == 0]:
    if g > len(str(max(i,n//i))):
      g = len(str(max(i,n//i)))
    if g == float('inf'):
        g = 1
  return g
print(f(n))

Fixed submission:
import math
n = int(input())
def f(n):
  g = float('inf')
  for i in [j for j in range(1,math.ceil(n**0.5)+1) if n%j == 0]:
    if g > len(str(max(i,n//i))):
      g = len(str(max(i,n//i)))
    if g == float('inf'):
        g = 1
  return g
print(f(n))
Buggy python submission (verdict: Time Limit Exceeded):
import itertools

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

C = list(itertools.combinations(A, 2))

ans = 0
for i, j in C:
    ans += i * j
    ans = ans % (1e9 + 7)
print(int(ans))


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

S = sum(A)
S2 = sum(map(lambda x: x * x, A))

print((S*S-S2)//2 % 1000000007)

Buggy python submission (verdict: Time Limit Exceeded):
import itertools

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

C = list(itertools.combinations(A, 2))

ans = 0
for i, j in C:
    ans += i * j
    ans = ans % (1e9 + 7)
print(int(ans))

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

S = sum(A)
S2 = sum(map(lambda x: x * x, A))

print((S*S-S2)//2 % 1000000007)
Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())

root = [-1]*n

def r(x):
    if root[x] < 0 :
        return x
    else:
        root[x] = r(root[x])
        return root[x]

def unite(x, y):
    x = r(x)
    y = r(y)
    if x == y:
        return
    root[x] += root[y]
    root[y] = x

def size(x):
    x = r(x)
    return - root[x]

for i in range(m):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    unite(x,y)

max_size = max(map(size,range(n)))

print(max_size)

Fixed submission:
import sys
sys.setrecursionlimit(10**9)

n, m = map(int, input().split())

root = [-1]*n

def r(x):
    if root[x] < 0 :
        return x
    else:
        root[x] = r(root[x])
        return root[x]

def unite(x, y):
    x = r(x)
    y = r(y)
    if x == y:
        return
    root[x] += root[y]
    root[y] = x

def size(x):
    x = r(x)
    return - root[x]

for i in range(m):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    unite(x,y)

max_size = max(map(size,range(n)))

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


H, W = map(int, input().split())
Ch, Cw = map(lambda x: int(x) - 1, input().split())
Dh, Dw = map(lambda x: int(x) - 1, input().split())
maze = [input() for _ in range(H)]

INF = 10 ** 12
path = [[INF] * W for _ in range(H)]  # path: 各マスへの移動コスト

# walk:移動A, warp:移動B
walk = [(0, 1), (0, -1), (-1, 0), (1, 0)]
warp = [(i, j) for i in range(-2, 3)
        for j in range(-2, 3) if (i, j) not in [(0, 0)] + walk]

q = deque()
path[Ch][Cw] = 0
q.append((Ch, Cw, 0))

while q:
    x, y, s = q.popleft()
    for dx, dy in walk:  # 移動A
        nx = x + dx
        ny = y + dy
        if 0 <= nx < H and 0 <= ny < W and maze[nx][ny] == '.' and path[nx][ny] > s:
            path[nx][ny] = s
            q.appendleft((nx, ny, s))  # 移動Aの場合はコスト0. キューの先頭に追加する.
    for dx, dy in warp:  # 移動B
        nx = x + dx
        ny = y + dy
        if 0 <= nx < H and 0 <= ny < W and maze[nx][ny] == '.' and path[nx][ny] > s + 1:
            path[nx][ny] = s + 1
            q.append((nx, ny, s + 1))  # 移動Bの場合はコスト1. キューの最後尾に追加する.

ans = path[Dh][Dw] if path[Dh][Dw] < INF else '-1'
print(ans)


Fixed submission:
from collections import deque


H, W = map(int, input().split())
Ch, Cw = map(lambda x: int(x) - 1, input().split())
Dh, Dw = map(lambda x: int(x) - 1, input().split())
maze = [input() for _ in range(H)]

INF = 10 ** 12
path = [[INF] * W for _ in range(H)]  # path: 各マスへの移動コスト

# walk:移動A, warp:移動B
walk = [(0, 1), (0, -1), (-1, 0), (1, 0)]
warp = [(i, j) for i in range(-2, 3)
        for j in range(-2, 3) if (i, j) not in [(0, 0)] + walk]

q = deque()
path[Ch][Cw] = 0
q.append((Ch, Cw, 0))

while q:
    x, y, s = q.popleft()
    for dx, dy in walk:  # 移動A
        nx = x + dx
        ny = y + dy
        if 0 <= nx < H and 0 <= ny < W and maze[nx][ny] == '.' and path[nx][ny] > s:
            path[nx][ny] = s
            q.appendleft((nx, ny, s))  # 移動Aの場合はコスト0. キューの先頭に追加する.
    for dx, dy in warp:  # 移動B
        nx = x + dx
        ny = y + dy
        if 0 <= nx < H and 0 <= ny < W and maze[nx][ny] == '.' and path[nx][ny] > s + 1:
            path[nx][ny] = s + 1
            q.append((nx, ny, s + 1))  # 移動Bの場合はコスト1. キューの最後尾に追加する.

ans = path[Dh][Dw] if path[Dh][Dw] < INF else '-1'
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int,input().split())
A = list(map(int,input().split()))
S = [1]

for i in range(0,N):
    tmp = S[-1] * A[i]
    S.append(tmp)

# print(S)

B = []
for i in range(K,N+1):
    B.append(S[i]//S[i-K])

for i in range(1,len(B)):
    if(B[i]>B[i-1]):
        print('Yes')
    else:
        print('No')


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

for i in range(K,N):
    if(A[i]>A[i-K]):
        print('Yes')
    else:
        print('No')
Buggy python submission (verdict: Runtime Error):
mai = n//1000
mai += 1
if mai*1000-n == 1000:
    print((mai-1)*1000-n)
else:
    print(mai*1000-n)

Fixed submission:
# coding: utf-8
# Your code here!

n = int(input())
mai = n//1000
mai += 1
if mai*1000-n == 1000:
    print((mai-1)*1000-n)
else:
    print(mai*1000-n)
Buggy python submission (verdict: Runtime Error):
X, N = map(int,input().split())


if N == 0:
    print(X)
else:
    P = list(map(int,input().split()))
    min_abs = 99999
    min_abs_list = []
    search_list = [i for i in range(1,101) if i not in P]
    
    
    
    for p in search_list:
        diff = abs(X-p)
        if diff < min_abs:
            min_abs = diff
            min_abs_list=[]
            min_abs_list.append(p)
        if diff == min_abs:
            min_abs_list.append(p)
        
    print(min(min_abs_list))

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


if N == 0:
    print(X)
else:
    P = list(map(int,input().split()))
    i = 0
    while(True):
        # print(X-i)
        if X-i not in P:
            print(X-i)
            break
        elif X+i not in P:
            print(X+i)
            break
        if i > 120:
            break
        i +=1
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())

num = list(input().split())
ans = 1
for i in num:
    ans *= int(i)
    n *= len(i)

if ans > 10**18:
    print('-1')
else: 
    print(ans)



Fixed submission:
n = int(input())

num = list(input().split())
ans = 1
if '0' in num :
    ans = 0
else:
    for i in num:
        ans *= int(i)
        if ans  > 10**18:
            break

if ans > 10**18:
    print('-1')
else: 
    print(ans)
Buggy python submission (verdict: Runtime Error):
N,K = map(int,input().split())
A = list(map(int,input().split()))

pos = 0
visited = [0]*N
move = []
loop = []

while visited[pos]!=2:
  if visited[pos] ==0:
    move.append(pos)
  else:
    loop.append(pos)
  visited[pos] += 1
  pos = A[pos]-1

if len(move)>k:
  print(move[k]+1)
else:
  print(roop[(K-(len(move)-len(loop)))%len(loop)]+1)

Fixed submission:
N,K = map(int,input().split())
A = list(map(int,input().split()))
pos = 0
visit = [0] * N
move = []
roop = []
while visit[pos]!=2:
    if visit[pos]==0:
        move.append(pos)
    else:
        roop.append(pos)
    visit[pos] += 1
    pos = A[pos] - 1
if len(move)>K:
    print(move[K]+1)
else:
    print(roop[(K-(len(move)-len(roop)))%len(roop)]+1)
Buggy python submission (verdict: Runtime Error):
a,b = input().split()
A = int(a+b)

flg = False
for i in range(1,101):
  if i*i = A:
    flg = True
    break

if flg:
  print("Yes")
else:
  print("No")

Fixed submission:
a,b = input().split()
A = int(a+b)

flg = False
for i in range(1,1000):
  if i*i == A:
    flg = True
    break

if flg:
  print("Yes")
else:
  print("No")
Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
A=list(map(int,input().split()))
ans=0
p= 10**9 +7
for i in range(N):
  ans += (A[i]*(sum(A[i+1:])))%p
print(ans%p)


Fixed submission:
N=int(input())
A=list(map(int,input().split()))
ans=0
S=sum(A)
p= 10**9 +7
for i in range(N):
  S-=A[i]
  ans += (A[i]*S)%p
print(ans%p)

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
N=int(input())
A=list(map(int,input().split()))
c=list(itertools.combinations(A,2))
p = 10**9 + 7
ans=0
for i in c:
  ans+= (i[0]*i[1])%p
print(ans%p)

Fixed submission:
N=int(input())
A=list(map(int,input().split()))
ans=0
S=sum(A)
p= 10**9 +7
for i in range(N):
  S-=A[i]
  ans += (A[i]*S)%p
print(ans%p)
Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int,input().split())
e=[[0]*N for i in range(N)]

for _ in range(M):
  a,b = map(int,input().split())
  a,b = a-1, b-1
  e[a][b]=1
  e[b][a]=1

end_q=[]
def conn(s):
  q=[s]
  end_q.append(s)
  an=0
  while len(q)>0:
    qq=q.pop(0)
    an+=1
    for i in range(N):
      if e[qq][i]==1 and (i not in end_q):
        q.append(i)
        end_q.append(i)
  return an
ans=0
for a in range(N):
  if a in end_q:
    continue
  else:
    ans=max(ans,conn(a))
print(ans)

Fixed submission:
#Union-Find木の実装
from collections import Counter
N,M=map(int,input().split())

#初期化
par=[i for i in range(N)]#親の要素(par[x]=xのときxは木の根)
rank=[0]*N#木の深さ

#木の根を求める
def find(x):
  #xが根だった場合xをそのまま返す
  if par[x] == x:
    return x
  else:
    #根が出るまで再帰する
    par[x] = find(par[x])
    return par[x]

#xとyの属する集合を併合
def unite(x,y):
  x=find(x)#xの根
  y=find(y)#yの根
  #根が同じなら何もしない
  if x == y:
    return
  #もし根の深さがyの方が深いなら
  if rank[x] < rank[y]:
    par[x] = y#yを根にする
  else:
    par[y] = x#xを根にする
    if rank[x] == rank[y]:
      rank[x]+=1

#xとyが同じ集合に属するか否か
#def same(x,y):
  #return find(x)==find(y)

for _ in range(M):
  a,b = map(int,input().split())
  a,b = a-1, b-1
  unite(a,b)

for i in range(N):
  find(i)
c=Counter(par)
c=c.most_common()[0][1]
print(c)

Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int,input().split())
e=[[0]*N for i in range(N)]

for _ in range(M):
  a,b = map(int,input().split())
  a,b = a-1, b-1
  e[a][b]=1
  e[b][a]=1

end_q=[]
def conn(s):
  q=[s]
  end_q.append(s)
  an=0
  while len(q)>0:
    qq=q.pop(0)
    an+=1
    for i in range(N):
      if e[qq][i]==1 and (i not in end_q):
        q.append(i)
        end_q.append(i)
  return an
ans=0
for a in range(N):
  if a in end_q:
    continue
  else:
    ans=max(ans,conn(a))
print(ans)

Fixed submission:
#Union-Find木の実装
from collections import Counter
N,M=map(int,input().split())

#初期化
par=[i for i in range(N)]#親の要素(par[x]=xのときxは木の根)
rank=[0]*N#木の深さ

#木の根を求める
def find(x):
  #xが根だった場合xをそのまま返す
  if par[x] == x:
    return x
  else:
    #根が出るまで再帰する
    par[x] = find(par[x])
    return par[x]

#xとyの属する集合を併合
def unite(x,y):
  x=find(x)#xの根
  y=find(y)#yの根
  #根が同じなら何もしない
  if x == y:
    return
  #もし根の深さがyの方が深いなら
  if rank[x] < rank[y]:
    par[x] = y#yを根にする
  else:
    par[y] = x#xを根にする
    if rank[x] == rank[y]:
      rank[x]+=1

#xとyが同じ集合に属するか否か
#def same(x,y):
  #return find(x)==find(y)

for _ in range(M):
  a,b = map(int,input().split())
  a,b = a-1, b-1
  unite(a,b)

for i in range(N):
  find(i)
c=Counter(par)
c=c.most_common()[0][1]
print(c)
Buggy python submission (verdict: Time Limit Exceeded):
n,k=map(int,input().split())
P=list(map(int,input().split()))
C=list(map(int,input().split()))
m=min(C)
score_max=m
for L in range(n):
  score=0
  d=[0]*n
  r=0
  d[L]=1
  for i in range(k):
    r+=1
    L=P[L]-1
    score+=C[L]
    score_max=max(score,score_max)
    if d[L]==0:
      d[L]=1
    elif score<0:
      break
    else:
      roop=k//r
      score = score*roop
      score_max=max(score,score_max)
      h=k%r
      for j in range(h):
        L=P[L]-1
        score+=C[L]
        score_max=max(score,score_max)
      break
print(score_max)


Fixed submission:
n,k=map(int,input().split())
P=list(map(int,input().split()))
C=list(map(int,input().split()))
score_max=max(C)
#スタート地点＝L
for L in range(n):
  score=0
  d=[0]*n
  r=0
  d[L]=1
  #k回移動する
  for _ in range(k):
    r+=1#移動回数をカウント
    L=P[L]-1#移動先の位置
    score+=C[L]
    score_max=max(score,score_max)
    #もしも移動した場所に未到達なら...
    if d[L]==0:
      d[L]=1
    #Cの最小値未満ならスコアは減り続けるのでbreak
    elif score<0:
      break
    else:
      roop=(k//r)-1
      #最後の1周と端数は一個ずつ見る
      h=(k%r)+r
      score*=roop
      score_max=max(score,score_max)

      #あまりの移動回数の処理
      for _ in range(h):
        L=P[L]-1
        score+=C[L]
        score_max=max(score,score_max)
      break
print(score_max)

Buggy python submission (verdict: Time Limit Exceeded):
n,k=map(int,input().split())
P=list(map(int,input().split()))
C=list(map(int,input().split()))
d=[0]*5001
score_max=min(C)
for L in range(n):
  score=0
  for i in range(k):
    score+=C[P[L]-1]
    score_max=max(score,score_max)
    L=P[L]-1
print(score_max)


Fixed submission:
n,k=map(int,input().split())
P=list(map(int,input().split()))
C=list(map(int,input().split()))
score_max=max(C)
#スタート地点＝L
for L in range(n):
  score=0
  d=[0]*n
  r=0
  d[L]=1
  #k回移動する
  for _ in range(k):
    r+=1#移動回数をカウント
    L=P[L]-1#移動先の位置
    score+=C[L]
    score_max=max(score,score_max)
    #もしも移動した場所に未到達なら...
    if d[L]==0:
      d[L]=1
    #Cの最小値未満ならスコアは減り続けるのでbreak
    elif score<0:
      break
    else:
      roop=(k//r)-1
      #最後の1周と端数は一個ずつ見る
      h=(k%r)+r
      score*=roop
      score_max=max(score,score_max)

      #あまりの移動回数の処理
      for _ in range(h):
        L=P[L]-1
        score+=C[L]
        score_max=max(score,score_max)
      break
print(score_max)
Buggy python submission (verdict: Time Limit Exceeded):
K=int(input())
num=7
i=1
d=[]
while 1:
  num=num%K
  if num==0:
    print(i)
    break
  if num in d:
    print(-1)
    break
  d.append(num)
  num = 10*num + 7
  i+=1


Fixed submission:
K=int(input())
num=7

#10と互いに素ではない数は必ずあまりが出る
if K%2==0 or K%5==0:
  print(-1)
  exit()

i=1
while 1:
  num=num%K
  if num==0:
    print(i)
    break
  num = 10*num + 7
  i+=1
Buggy python submission (verdict: Time Limit Exceeded):
n,m,k=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
a,b=[0],[0]
for i in range(n):
  a.append(a[i]+A[i])
for i in range(m):
  b.append(b[i]+B[i])
ans=0
book=0
for i in range(n+1):
  if a[i]>k:
    break
  for j in range(m+1):
    if a[i]+b[j]<=k:
      book=i+j
      if book>ans:
        ans = book
    else:
      break
print(ans)

Fixed submission:
n,m,k=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
a,b=[0],[0]
for i in range(n):
  a.append(a[i]+A[i])
for i in range(m):
  b.append(b[i]+B[i])
ans=0
for i in range(n+1):
  if a[i]>k:
    break
  for j in range(m,-1,-1):
    if a[i]+b[j]<=k:
      ans=max(ans,i+j)
      m=j
      break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m,k=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
a,b=[0],[0]
for i in range(n):
  a.append(a[i]+A[i])
for i in range(m):
  b.append(b[i]+B[i])
ans=0
book=0
for i in range(n+1):
  for j in range(m+1):
    if a[i]+b[j]<=k:
      book=i+j
      if book>ans:
        ans = book
    else:
      break
print(ans)

Fixed submission:
n,m,k=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
a,b=[0],[0]
for i in range(n):
  a.append(a[i]+A[i])
for i in range(m):
  b.append(b[i]+B[i])
ans=0
for i in range(n+1):
  if a[i]>k:
    break
  for j in range(m,-1,-1):
    if a[i]+b[j]<=k:
      ans=max(ans,i+j)
      m=j
      break
print(ans)