Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
N, Q = map(int,input().split())
S = input()
#'AC'が見つかったらCのほうに+1
count = [0]*(N+1)
for i in range(N):
    if S[i] == "A":
        if S[i+1] == "C":
            count[i+1] += count[i] + 1
        else:
            count[i+1] = count[i]
    else:
        count[i+1] = count[i]
    
#print(count)

for _ in range(Q):
    l, r = map(int,input().split())
    print(count[r-1]-count[l-1])

Fixed submission:
N, Q = map(int,input().split())
S = input()+"d"
#'AC'が見つかったらCのほうに+1
count = [0]*(N+1)
for i in range(N):
    if S[i] == "A":
        if S[i+1] == "C":
            count[i+1] += count[i] + 1
        else:
            count[i+1] = count[i]
    else:
        count[i+1] = count[i]
    
#print(count)

for _ in range(Q):
    l, r = map(int,input().split())
    print(count[r-1]-count[l-1])
Buggy python submission (verdict: Time Limit Exceeded):
S = list(input())

ans = 0
while True:
    tmp = 0
    for i in range(len(S)-1):
        if S[i] == "B" and S[i+1] == "W":
            S[i] = "W"
            S[i+1] = "B"
            tmp += 1
    if not tmp:
        break
    ans += tmp
        
print(ans)

Fixed submission:
S = input()

ans = 0
cnt = 0
for i in range(len(S)):
    if S[i] == "W":
        ans += i-cnt
        cnt += 1
        
print(ans)
Buggy python submission (verdict: Runtime Error):
S = input()
K = int(input())
last = "1"
for i,s in enumerate(S):
    if s != "1":
        last = s
        ind = i
        break
if K-1 < ind:
    print("1")
else:
    print(last)

Fixed submission:
S = input()
K = int(input())
last = "1"
ind = 100
for i,s in enumerate(S):
    if s != "1":
        last = s
        ind = i
        break
if K-1 < ind:
    print("1")
else:
    print(last)
Buggy python submission (verdict: Runtime Error):
N = int(input())

dp = [10**5]*(N+1)
dp[0] = 0
dp[1] = 1
s = 6
n = 9
for i in range(2,N+1):
    if 6*s <= i:
        s *= 6
    if 9*n <= i:
        n *= 9
    dp[i] = min(dp[i-1], dp[i-s], dp[i-n])+1
print(dp[N])

Fixed submission:
N = int(input())

dp = [10**5]*(N+1)
dp[0] = 0
dp[1] = 1
s = 6
n = 9
for i in range(2,N+1):
    if 6*s <= i:
        s *= 6
    if 9*n <= i:
        n *= 9
    if i < 6:
        dp[i] = dp[i-1]+1
    elif i < 9:
        dp[i] = min(dp[i-1], dp[i-s])+1
    else:
        dp[i] = min(dp[i-1], dp[i-s], dp[i-n])+1
print(dp[N])
Buggy python submission (verdict: Time Limit Exceeded):
import bisect
import sys

mod = 10**9+7

def modInverse(a,p):
    return pow(a,p-2,p)

def nCr(n,r,p=mod):
    num = 1
    den = 1
    for i in range(r):
        num = (num*(n-i)) % p
        den = (den*(i+1)) % p
    return num*modInverse(den,p)% p

def main(args):
    
    n = int(input())
    A = list(map(int,input().split()))
    
    A.sort()
    
    ncr = 0
    for a in A[::-1][:n-1]:
        check = a//2
        right = bisect.bisect_right(A, check)
        left = right - 1
        r1 = min(A[right],a-A[right])
        r2 = min(A[left],a-A[left])
        if r1 > r2:
            r = A[right]
        else:
            r = A[left]
        
        if nCr(a, r) > ncr:
            ncr = nCr(a, r)
            ans = [a, r]
            
    print(*ans)
    
if __name__ == '__main__':
    main(sys.argv[1:])

Fixed submission:
import bisect
import sys

def main(args):
    
    n = int(input())
    A = list(map(int,input().split()))
    
    A.sort()
    
    ans = [max(A), 0]
    ind = bisect.bisect_left(A ,ans[0]//2)
    if A[ind] == ans[0]//2:
        ans[1] = ans[0]//2
    else:
        r1 = min(A[ind],ans[0]-A[ind])
        r2 = min(A[ind-1],ans[0]-A[ind-1])
        if r1 > r2:
            ans[1] = A[ind]
        else:
            ans[1] = A[ind-1]
    
    print(*ans)
    
if __name__ == '__main__':
    main(sys.argv[1:])
Buggy python submission (verdict: Time Limit Exceeded):
H,W,D = map(int,input().split())

A = [[] for _ in range(H)]
Aset = [[] for _ in range(H)]
for i in range(H):
    A[i] = list(map(int,input().split()))
    Aset[i] = set(A[i])

#1,1+D,1+2D,...
#2,2+D,2+2D,...
#...
#D,2D,3D,...
cost = [0]*(H*W+1)
for i in range(1,D+1):
    num = i
    now = None#座標
    while True:
        if num > H*W:
            break
        for row in range(H):
            if num in Aset[row]:
                #print(row)
                ind = A[row].index(num)
                if now == None:
                    now = (ind, row)#x,y
                    cost[num] = 0
                else:
                    cost[num] = cost[num-D] + abs(ind-now[0]) + abs(row-now[1])
                    now = (ind, row)
        num += D

Q = int(input())
for _ in range(Q):
    l, r = map(int,input().split())
    print(cost[r]-cost[l])

Fixed submission:
H,W,D = map(int,input().split())

A = [[] for _ in range(H)]
Aset = [[] for _ in range(H)]
for i in range(H):
    A[i] = list(map(int,input().split()))
    Aset[i] = set(A[i])

#1,1+D,1+2D,...
#2,2+D,2+2D,...
#...
#D,2D,3D,...
cost = [0]*(H*W+1)
for i in range(1,D+1):
    num = i
    now = None#座標
    while True:
        if num > H*W:
            break
        for row in range(H):
            if num in Aset[row]:
                #print(row)
                ind = A[row].index(num)
                if now == None:
                    now = (ind, row)#x,y
                    cost[num] = 0
                else:
                    cost[num] = cost[num-D] + abs(ind-now[0]) + abs(row-now[1])
                    now = (ind, row)
        num += D

Q = int(input())
for _ in range(Q):
    l, r = map(int,input().split())
    print(cost[r]-cost[l])
Buggy python submission (verdict: Runtime Error):
a,b,c=map(int,input().split())
temp=a//c
if(temp>b):
    print(a-(b*c))
else:
    if(temp==b):
        if(a%c==0):
            print(0)
        else:
            print(a%c)
    else:
        if(a%c==0):
            b=b-temp;
            if(b%2==0):
                print(0)
            else:
                print(c)
        else:
            b=b-temp;
            if(b%2==0):
                print(a%c)
            else:
                t1=(a%c)-c
                t2=(a%c)+c
                if(abs(t1)>abs(t2)):
                    print(abs(t2))
                else:
                    print(abs(t1))
                
    


Fixed submission:
a,b,c=map(int,input().split())
a=abs(a)
temp=a//c
if(temp>b):
    temp=temp-b
    if(a%c==0):
        if(temp>b):
            print(a-(b*c))
        else:
            print(temp*c)
    else:
        print(a-(b*c))
else:
    if(temp==b):
        if(a%c==0):
            print(0)
        else:
            print(a%c)
    else:
        if(a%c==0):
            b=b-temp;
            if(b%2==0):
                print(0)
            else:
                print(c)
        else:
            b=b-temp;
            if(b%2==0):
                print(a%c)
            else:
                t1=(a%c)-c
                t2=(a%c)+c
                if(abs(t1)>abs(t2)):
                    print(abs(t2))
                else:
                    print(abs(t1))
Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
A=[1]*(n+1)
A[1]=1
for i in range(2,n+1):
    for j in range(i,n+1,i):
        A[j]+=1
A[1]=1
sum=0
for i in range(1,n+1):
    sum+=i*A[i]
print(sum)

Fixed submission:
n=int(input())
A=[1]*(n+1)
A[1]=1
for i in range(2,n+1):
    for j in range(i,n+1,i):
        A[j]+=1
A[1]=1
sum=0
for i in range(1,n+1):
    sum+=i*A[i]
print(sum)
Buggy python submission (verdict: Runtime Error):
alpha = 'abcdefghijklmnopqrstuvwxyz'
N= int(input())
len_alpha = 26
tmp = N
calc = tmp
cnt = 1

while tmp > 0:
    calc = tmp
    tmp -= (len_alpha **cnt)
    cnt += 1

res =''
for i in range(1, cnt - 1):
    tmp = int(calc/(len_alpha**(cnt - i - 1)))
    calc -= tmp * len_alpha**(cnt - i - 1)
    res += alpha[tmp]

res += alpha[calc-1]
print(res)

Fixed submission:
alpha = 'abcdefghijklmnopqrstuvwxyz'
N= int(input())
len_alpha = 26
tmp = N
calc = tmp
cnt = 1

while tmp > 0:
    calc = tmp
    tmp -= (len_alpha **cnt)
    cnt += 1

res =''
for i in range(1, cnt - 1):
    tmp = int((calc-1)/(len_alpha**(cnt - i - 1)))
    calc -= tmp * len_alpha**(cnt - i - 1)
    res += alpha[tmp]

res += alpha[calc-1]
print(res)
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

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

for i in range(Q):
    B, C= list(map(int, input().split()))
    A = A[np.where(A != B)]
    A = np.append(A, np.ones(N - A.shape[0])*C)

    print(int(np.sum(A)))

Fixed submission:
N= int(input())
A= list(map(int, input().split()))
Q= int(input())
tmpDic = {}
res = sum(A)

for i in range(N):
    if A[i] in tmpDic:
        tmpDic[A[i]] += 1
    else:
        tmpDic[A[i]] = 1

for i in range(Q):
    B, C= list(map(int, input().split()))
    if B in tmpDic and tmpDic[B] > 0:
        tmp = tmpDic[B]
        tmpDic[B] = 0
        res += (C - B)* tmp
        
        if C in tmpDic:
            tmpDic[C] += tmp
        else:
            tmpDic[C] = tmp
    print(res)
Buggy python submission (verdict: Runtime Error):
import math
from collections import Counter
A= int(input())
resArray = []
tmpA = A
i = 1
while i <= math.sqrt(A):
    #print (i)
    i += 1
    if tmpA == 1:
        break

    if i >2 and i %2 == 0:
        continue


    while tmpA % i == 0:
        tmpA = tmpA / i
        resArray.append(i)

res = 0
if tmpA == A:
    print(1)
    exit()
else tmpA > 1:
    res += 1


cArray = Counter(resArray)

for i in cArray.values():
    if i > 0:
        
        tmp = 1
        while tmp*(tmp+1)//2 <= i:
            tmp += 1
        res += tmp -1
print(res)

Fixed submission:
import math
from collections import Counter
A= int(input())
if A == 1:
    print(0)
    exit()
resArray = []
tmpA = A
i = 1
while i <= math.sqrt(A):
    #print (i)
    i += 1
    if tmpA == 1:
        break

    if i >2 and i %2 == 0:
        continue


    while tmpA % i == 0:
        tmpA = tmpA / i
        resArray.append(i)

res = 0
if tmpA == A:
    print(1)
    exit()
elif tmpA > 1:
    res += 1


cArray = Counter(resArray)

for i in cArray.values():
    if i > 0:
        
        tmp = 1
        while tmp*(tmp+1)//2 <= i:
            tmp += 1
        res += tmp -1
print(res)
Buggy python submission (verdict: Runtime Error):
a, b, c = list(map(int, input().split()))
print('Yes' if 2 * Decimal(math.sqrt(a*b)) < c - a - b else 'No')

Fixed submission:
from decimal import *
import math
a, b, c = list(map(int, input().split()))
print('Yes' if c - a - b> 0 and 4*a*b < (c - a - b)**2 else 'No')
Buggy python submission (verdict: Runtime Error):
import math

n,d=map(int,input().split())
x=[list(map(int,input().split())) for i in range(n)]
cnt=0
for i in range(n):
    for j in range(i+1,n):
        dis=0
        for k in range(d):
            dis+=(x[i][k]-x[j][k])**2
        if sqrt(dis)%1==0:
            cnt+=1
print(cnt)


Fixed submission:
import math

n,d=map(int,input().split())
x=[list(map(int,input().split())) for i in range(n)]
cnt=0
for i in range(n):
    for j in range(i+1,n):
        dis=0
        for k in range(d):
            dis+=(x[i][k]-x[j][k])**2
        if math.sqrt(dis)%1==0:
            cnt+=1
print(cnt)

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

n,d=map(int,input().split())
x=[list(map(int,input().split())) for i in range(n)]
cnt=0
for i in range(n):
    for j in range(i+1,n):
        dis=0
        for k in range(d):
            dis+=(x[i][k]-x[j][k])**2
        if is_integer(sqrt(dis)):
            cnt+=1
print(cnt)


Fixed submission:
import math

n,d=map(int,input().split())
x=[list(map(int,input().split())) for i in range(n)]
cnt=0
for i in range(n):
    for j in range(i+1,n):
        dis=0
        for k in range(d):
            dis+=(x[i][k]-x[j][k])**2
        if math.sqrt(dis)%1==0:
            cnt+=1
print(cnt)

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


#vector的なやつ
#a=[input() for in range(n)]

n,d=map(int,input().split())
x=[list(map(int,input().split())) for i in range(n)]
cnt=0
for i in range(n):
    for j in range(i+1,n):
        dis=0
        for k in range(d):
            dis+=(x[i][k]-x[j][k])**2
        if is_integer(sqrt(dis)):
            cnt+=1
print(cnt)


Fixed submission:
import math

n,d=map(int,input().split())
x=[list(map(int,input().split())) for i in range(n)]
cnt=0
for i in range(n):
    for j in range(i+1,n):
        dis=0
        for k in range(d):
            dis+=(x[i][k]-x[j][k])**2
        if math.sqrt(dis)%1==0:
            cnt+=1
print(cnt)

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


#vector的なやつ
#a=[input() for in range(n)]

n,d=map(int,input().split())
x=[list(map(int,input().split())) for i in range(n)]
cnt=0
for i in range(n):
    for j in range(i+1,n):
        dis=0
        for k in range(d):
            dis+=(x[i][k]-x[j][k])**2
        if(sqrt(dis)%1==0):
            cnt+=1
print(cnt)
    

Fixed submission:
import math

n,d=map(int,input().split())
x=[list(map(int,input().split())) for i in range(n)]
cnt=0
for i in range(n):
    for j in range(i+1,n):
        dis=0
        for k in range(d):
            dis+=(x[i][k]-x[j][k])**2
        if math.sqrt(dis)%1==0:
            cnt+=1
print(cnt)
Buggy python submission (verdict: Runtime Error):
import math

n=int(input())
f=list()
p=list()
ans=-10**10
for i in range(n):
    f.append(list(map(int,input().split())))
for i in range(n):
    p.append(list(map(int,input().split())))
for i in range(1,1<<10):
    shop=list()
    cnt=[0]*n
    tmp=0
    for j in range(10):
        if i&(1<<j):
            shop.append(j)
    for s in range(n):
        for p in shop:
            if(f[s][p]==1):
                cnt+=1
        tmp+=p[s][cnt[s]]
    ans=max(ans,tmp)
print(ans)

Fixed submission:
import math

n=int(input())
f=list()
p=list()
ans=-10**10
for i in range(n):
    f.append(list(map(int,input().split())))
for i in range(n):
    p.append(list(map(int,input().split())))
for i in range(1,1<<10):
    shop=list()
    cnt=[0]*n
    tmp=0
    for j in range(10):
        if i>>j&1:
            shop.append(j)
    for s in range(n):
        for k in shop:
            if(f[s][k]==1):
                cnt[s]+=1
        tmp+=p[s][cnt[s]]
    ans=max(ans,tmp)
print(ans)
Buggy python submission (verdict: Runtime Error):
n,p,m = list(map(int,input().split()))

seen = {}
ans = p%m
seen[p%m]=1
T = {}
T[1] = ans
last_p = p%m

r = 0

if n==1:
  print(p%m)
else:
  for i in range(2,m+1000):
    p = p ** 2
    p = p % m
    
    if p not in seen:
      seen[p]=i
    else:
      r = i-seen[p]
     # print(r, i, seen[p], p)
      last_p = p
      break
    
    ans += p
    T[i] = ans
    
    #last_p = p
    
    if i==n:
      print(ans)
      exit()
    
    
temp = T[seen[p]-1]
#print(temp)
ans -= temp
n -= seen[p]
#print(492443256176507)
#print(p, 'p')
    
k = n//r
ans *= k
ans += temp

n = n % r
if n==0:
  print(ans+last_p)
  exit()

for i in range(1,m+1000):
  p = p ** 2
  p = p % m
  ans += p
  if i==n:
    print(ans+last_p)
    exit()
      
      
    

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

seen = {}
ans = p%m
seen[p%m]=1
T = {}
T[1] = ans
T[0] = 0
last_p = p%m

r = 0

if n==1:
  print(p%m)
  exit()
else:
  for i in range(2,m+1000):
    p = p ** 2
    p = p % m
    
    if p not in seen:
      seen[p]=i
    else:
      r = i-seen[p]
      last_p = p
      break
    
    ans += p
    T[i] = ans
    
    if i==n:
      print(ans)
      exit()
    
    
temp = T[seen[p]-1]
ans -= temp
n -= seen[p]

k = n//r
ans *= k
ans += temp

n = n % r

if n==0:
  print(ans+last_p)
  exit()

for i in range(1,m+1000):
  p = p ** 2
  p = p % m
  ans += p
  if i==n:
    print(ans+last_p)
    exit()
Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stding.readline

t = int(input())
po = []

for _ in range(t):
  x,y = list(map(int,input().split()))
  po.append((x,y))
  
po.sort()

dp = [-(10**99) for _ in range(t)]

for i in range(t):
  value = min(x+y, x-y)
  dp[i] = min(dp[i-1], value)
  
best = -9999999999999999999999999999

for i in range(1,t):
  dist = po[i][0] + po[i][1] - dp[i-1]
  best = max(best, dist)
  
print(best)

Fixed submission:
import sys
input = sys.stdin.readline
 
t = int(input())
po = []
 
for _ in range(t):
  x,y = list(map(int,input().split()))
  po.append((x,y))
  
po.sort()
 
dp1 = [+(10**99) for _ in range(t)]
 
for i in range(t):
  x,y = po[i][0], po[i][1]
  value = x+y
  dp1[i] = min(dp1[i-1], value)
  
dp2 = [+(10**99) for _ in range(t)]
 
for i in range(t):
  x,y = po[i][0], po[i][1]
  value = x-y
  dp2[i] = min(dp2[i-1], value)
  
best = -9999999999999999999999999999
#print(dp)
 
for i in range(1,t):
  dist = po[i][0] + po[i][1] - dp1[i-1]
  best = max(best, dist)
  
  dist = po[i][0] - po[i][1] - dp2[i-1]
  best = max(best, dist)
  
print(best)
Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

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

if a%b==0:
  print((a//b) * c)
else:
  print((a//b) * (c+1))
  

Fixed submission:
import sys
input = sys.stdin.readline
 
a,b,c = list(map(int,input().split()))
 
if a%b==0:
  print((a//b) * c)
else:
  print(((a//b)+1) * c)
Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
 
#a,b,c = list(map(int,input().split()))
 
a = list(input())

ans = 0

for dig in a:
  ans += int(dig)
  
if ans % 9 ==0:
  print('Yes')
else:
  print('No')

Fixed submission:
import sys
input = sys.stdin.readline
 
#a,b,c = list(map(int,input().split()))
 
a = list(input().rstrip())

ans = 0

for dig in a:
  ans += int(dig)
  
#print(ans)
  
if ans % 9 ==0:
  print('Yes')
else:
  print('No')
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
 
h,w,m = list(map(int,input().split()))

R = [0]*h
C = [0]*w
G = {}

for _ in range(m):
  a,b = list(map(int,input().split()))
  a,b = a-1, b-1
  R[a] += 1
  C[b] += 1
  G[(a,b)] = 1
  
best = 0
for i in range(h):
  for j in range(w):
    cor = 0
    if (i,j) in G:
      cor = 1
    best = max(best,R[i]+C[j] - cor)
    
print(best)

Fixed submission:
import sys
input = sys.stdin.readline
 
h,w,m = list(map(int,input().split()))

R = [0]*h
C = [0]*w
G = {}

for _ in range(m):
  a,b = list(map(int,input().split()))
  a,b = a-1, b-1
  R[a] += 1
  C[b] += 1
  G[(a,b)] = 1
  
hhh = max(R)
ccc = max(C)
hei,wei = [],[]

for i in range(h):
  if R[i]==hhh:
    hei.append(i)
    
for i in range(w):
  if C[i]==ccc:
    wei.append(i)
  
  
best = 0
for i in hei:
  for j in wei:
    cor = 0
    if (i,j) in G:
      cor = 1
    best = max(best,R[i]+C[j] - cor)
    if cor==0:
      break
  if cor==0:
      break
      
print(best)

Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
 
h,w,m = list(map(int,input().split()))

R = [0]*h
C = [0]*w
G = [[0]*w for _ in range(h)]

for _ in range(m):
  a,b = list(map(int,input().split()))
  a,b = a-1, b-1
  R[a] += 1
  C[b] += 1
  G[a][b] = 1
  
best = 0
for i in range(h):
  for j in range(w):
    best = max(best,R[i]+C[j] - G[i][j])
    
print(best)

Fixed submission:
import sys
input = sys.stdin.readline
 
h,w,m = list(map(int,input().split()))

R = [0]*h
C = [0]*w
G = {}

for _ in range(m):
  a,b = list(map(int,input().split()))
  a,b = a-1, b-1
  R[a] += 1
  C[b] += 1
  G[(a,b)] = 1
  
hhh = max(R)
ccc = max(C)
hei,wei = [],[]

for i in range(h):
  if R[i]==hhh:
    hei.append(i)
    
for i in range(w):
  if C[i]==ccc:
    wei.append(i)
  
  
best = 0
for i in hei:
  for j in wei:
    cor = 0
    if (i,j) in G:
      cor = 1
    best = max(best,R[i]+C[j] - cor)
    if cor==0:
      break
  if cor==0:
      break
      
print(best)

Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
 
h,w,m = list(map(int,input().split()))

R = [0]*h
C = [0]*w

G = [[0]*w for _ in range(h)]


for _ in range(m):
  a,b = list(map(int,input().split()))
  a,b = a-1, b-1
  R[a] += 1
  C[b] += 1
  G[a][b] = 1
  
best = 0
for i in range(h):
  for j in range(w):
    best= max(best,R[i]+C[j] - G[i][j])
    
print(best)

Fixed submission:
import sys
input = sys.stdin.readline
 
h,w,m = list(map(int,input().split()))

R = [0]*h
C = [0]*w
G = {}

for _ in range(m):
  a,b = list(map(int,input().split()))
  a,b = a-1, b-1
  R[a] += 1
  C[b] += 1
  G[(a,b)] = 1
  
hhh = max(R)
ccc = max(C)
hei,wei = [],[]

for i in range(h):
  if R[i]==hhh:
    hei.append(i)
    
for i in range(w):
  if C[i]==ccc:
    wei.append(i)
  
  
best = 0
for i in hei:
  for j in wei:
    cor = 0
    if (i,j) in G:
      cor = 1
    best = max(best,R[i]+C[j] - cor)
    if cor==0:
      break
  if cor==0:
      break
      
print(best)
Buggy python submission (verdict: Time Limit Exceeded):
n, k = list(map(int,input().split()))
jump = list(map(int,input().split()))
arr = list(map(int,input().split()))
best = - (10**28)
VV = []
 
for i in range(n):
  if i not in VV:
    order = []
    prev_pos = i
    visited = []
    sub_best = 0
    order = []
    l = 0
 
    for jjj in range(n):
      order.append(arr[prev_pos])
      #sub_best = max(sub_best, sum(order))
      visited.append(prev_pos)
      VV.append(prev_pos)
      l += 1
 
      next_pos = jump[prev_pos] - 1
      prev_pos = next_pos
      if next_pos in visited:
        break
 
    if sum(order)<1:
      limit = min(k,l*2)
      ss = 0
    else:
      limit = (k % l) + l
      limit = min(k,limit)
      ss = sum(order) * ((k//l) - 1)
 
    sub_best += ss
    #print(limit)
    
    order = order + order + order
    
    for op in range(len(order)):
      for chuj in range(1,limit+1):
        kk = sum(order[op:op+chuj])
        best = max(best, sub_best + kk)
      
print(best)

Fixed submission:
n, k = list(map(int,input().split()))
jump = list(map(int,input().split()))
arr = list(map(int,input().split()))
best = - (10**28)
VV = []
 
for i in range(n):
  if i not in VV:
    order = []
    prev_pos = i
    visited = []
    sub_best = 0
    order = []
    l = 0
 
    for jjj in range(n):
      order.append(arr[prev_pos])
      #sub_best = max(sub_best, sum(order))
      visited.append(prev_pos)
      VV.append(prev_pos)
      l += 1
 
      next_pos = jump[prev_pos] - 1
      prev_pos = next_pos
      if next_pos in visited:
        break
 
    if sum(order)<1:
      limit = min(k,l*2)
      ss = 0
    else:
      limit = (k % l) + l
      limit = min(k,limit)
      ss = sum(order) * ((k//l) - 1)
      ss = max(ss,0) 
 
    sub_best = ss
   # print(limit, order, ss, sub_best)
    
    order = order + order + order
    
    for chuj in range(1,limit+1):
      kk = sum(order[0:0+chuj])
      best = max(best, sub_best + kk)
      for op in range(len(order)-chuj):
        kk -= order[op]
        kk += order[op+chuj]
        best = max(best, sub_best + kk)
      
print(best)