Buggy python submission (verdict: Time Limit Exceeded):
n,k = map(int, input().split())
p = list(int(x)-1 for x in input().split())
c = list(map(int, input().split()))
score = -10**9
loop = [[] for _ in range(n+1)]
group = [0]*n
while 0 in group:
  i = group.index(0)
  here = i
  while group[i]==0:
    here = p[here]
    loop[i+1].append(here+1)
    group[here] = i+1
while [] in loop:
  loop.remove([])
for i in loop:
  s = [0]
  cc = []
  for j in i:
    s.append(s[-1]+c[j-1])
    cc.append(c[j-1])
  s.remove(0)
  l = k%len(s)
  resd = -10**9-1
  cum = -10**9-1
  cc = cc + cc[:-1]
  for j in range(len(s)):
    resd = max(sum(cc[j:j+l]),resd)
  for j in range(1,min(k,len(s))+1):
    for m in range(len(s)):
      cum = max(sum(cc[m:m+j]),cum)
  if s[-1]>0 and len(s)<k:
    sc = s[-1]*(k//len(s))+resd
  else:
    sc = cum
  score = max(sc,score)
print(score)

Fixed submission:
n,k=map(int,input().split())
p=list(map(int,input().split()))
c=list(map(int,input().split()))
ans=-10**18
flag=dict()
for i in range(n):
    if i in flag:
        continue
    cur=i
    accum=[]
    flag[cur]=0
    s=0
    while flag[cur]<2:
        cur=p[cur]-1
        s+=c[cur]
        accum.append(s)
        if cur not in flag:
            flag[cur]=0
        else:
            flag[cur]+=1
    l=len(accum)//2
    loop = accum[l-1]
    accum=[0]+accum
    for x in range(1,l+1):
        if loop<=0 or k//l<1:
            ans=max(ans,max(accum[x:x+min(k,l)])-accum[x-1])
        else:
            a=max(accum[x:x+l-1])-accum[x-1]
            d=accum[x+l-1]-accum[x-1]
            if k%l!=0:
                b=max(accum[x:x+k%l])-accum[x-1]
                ans=max(ans,max(k//l*d,k//l*d+b,(k//l-1)*d+a))
            else:
                b=max(accum[x:x+l-1])-accum[x-1]
                ans=max(ans,max(k//l*d,(k//l-1)*d+a))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
n,k = map(int, input().split())
p = list(int(x)-1 for x in input().split())
c = list(map(int, input().split()))
score = -10**9
his = [0]*(n+1)
for i in range(n):
  rec = np.array([0])
  here = i
  cnt = 0
  for _ in range(k):
    here = p[here]
    rec = np.append(rec,c[here])
    cnt += 1
    if here == i:
      break
  rec = np.cumsum(rec)
  s = max(rec[1:])
  ps = rec[-1]*(k//cnt)+max(rec[0:k%cnt+1])
  s = max(ps,s)
  score = max(s,score)
print(score)

Fixed submission:
n,k=map(int,input().split())
p=list(map(int,input().split()))
c=list(map(int,input().split()))
ans=-10**18
flag=dict()
for i in range(n):
    if i in flag:
        continue
    cur=i
    accum=[]
    flag[cur]=0
    s=0
    while flag[cur]<2:
        cur=p[cur]-1
        s+=c[cur]
        accum.append(s)
        if cur not in flag:
            flag[cur]=0
        else:
            flag[cur]+=1
    l=len(accum)//2
    loop = accum[l-1]
    accum=[0]+accum
    for x in range(1,l+1):
        if loop<=0 or k//l<1:
            ans=max(ans,max(accum[x:x+min(k,l)])-accum[x-1])
        else:
            a=max(accum[x:x+l-1])-accum[x-1]
            d=accum[x+l-1]-accum[x-1]
            if k%l!=0:
                b=max(accum[x:x+k%l])-accum[x-1]
                ans=max(ans,max(k//l*d,k//l*d+b,(k//l-1)*d+a))
            else:
                b=max(accum[x:x+l-1])-accum[x-1]
                ans=max(ans,max(k//l*d,(k//l-1)*d+a))
print(ans)