Buggy python submission (verdict: Runtime Error):
N,K=map(int,input().split())
P=list(map(int,input().split()))
C=list(map(int,input().split()))
points=[[] for i in range(N)]
for i in range(N):
  tmpl=[0]
  loc=i
  for j in range(1,N+1):
    tmpl+=[tmpl[j-1]+C[loc]]
    loc=P[loc]-1
  points[i]=tmpl
repeat=K//N
left=K%N
maxlist=[]
for i in range(N):
  if points[i][N]<0:
    maxlist+=[max(points[i][1:left+1])]
  else:
    tmp=repeat*points[i][N]+max(points[i][1:left+1])
    maxlist+=[tmp]
print(max(maxlist))
  

Fixed submission:
n,k=map(int,input().split())
p=list(map(int,input().split()))
c=list(map(int,input().split()))
for i in range(n):
    p[i]-=1
ans=max(c)
for i in range(n):
    f=i
    r=0
    l=[]
    while True:
        f=p[f]
        l.append(c[f])
        r+=c[f]
        if f==i:
            break
    t=0
    for j in range(len(l)):
        t+=l[j]
        ret=t
        if r>0:
            e=(k-j-1)//len(l)
            ret+=r*e
        ans=max(ans,ret)
print(ans)

Buggy python submission (verdict: Runtime Error):
N,K=map(int,input().split())
P=list(map(int,input().split()))
C=list(map(int,input().split()))
points=[[] for i in range(N)]
for i in range(N):
  tmpl=[0]
  loc=i
  for j in range(1,N+1):
    tmpl+=[tmpl[j-1]+C[loc]]
    loc=P[loc]-1
  points[i]=tmpl
repeat=K//N
left=K%N
maxlist=[]
for i in range(N):
  if points[i][N]<0:
    maxlist+=[max(points[i][1:left+1])]
  else:
    tmp=repeat*points[i][N]+max(points[i][1:left+1])
    maxlist+=[tmp]
print(max(maxlist))
  

Fixed submission:
n,k=map(int,input().split())
p=list(map(int,input().split()))
c=list(map(int,input().split()))
for i in range(n):
    p[i]-=1
ans=max(c)
for i in range(n):
    f=i
    r=0
    l=[]
    while True:
        f=p[f]
        l.append(c[f])
        r+=c[f]
        if f==i:
            break
    t=0
    for j in range(len(l)):
        t+=l[j]
        ret=t
        if r>0:
            e=(k-j-1)//len(l)
            ret+=r*e
        ans=max(ans,ret)
print(ans)

Buggy python submission (verdict: Runtime Error):
N,K=map(int,input().split())
P=list(map(int,input().split()))
C=list(map(int,input().split()))
points=[[] for i in range(N)]
for i in range(N):
  tmpl=[0]
  loc=i
  for j in range(1,N+1):
    tmpl+=[tmpl[j-1]+C[i]]
    loc=P[i]
  points[i]=tmpl
repeat=K//N
left=K%N
ans=-float('inf')
maxlist=[]
for i in range(N):
  if points[i][N]<0:
    maxlist+=[max(points[i][1:left+1])]
  else:
    tmp=repeat*points[i][N]+max(points[i][1:left+1])
    maxlist+=[tmp]
print(max(maxlist))
  


Fixed submission:
n,k=map(int,input().split())
p=list(map(int,input().split()))
c=list(map(int,input().split()))
for i in range(n):
    p[i]-=1
ans=max(c)
for i in range(n):
    f=i
    r=0
    l=[]
    while True:
        f=p[f]
        l.append(c[f])
        r+=c[f]
        if f==i:
            break
    t=0
    for j in range(len(l)):
        t+=l[j]
        ret=t
        if r>0:
            e=(k-j-1)//len(l)
            ret+=r*e
        ans=max(ans,ret)
print(ans)

Buggy python submission (verdict: Runtime Error):
N,K=map(int,input().split())
P=list(map(int,input().split()))
C=list(map(int,input().split()))
points=[[] for i in range(N)]
for i in range(N):
  tmpl=[0]
  loc=i
  for j in range(1,N+1):
    tmpl+=[tmpl[j-1]+C[i]]
    loc=P[i]
  points[i]=tmpl
repeat=K//N
left=K%N
ans=-float('inf')
maxlist=[]
for i in range(N):
  if points[i][N]<0:
    maxlist+=[max(points[i][1:left+1])]
  else:
    tmp=repeat*points[i][N]+max(points[i][1:left+1])
    maxlist+=[tmp]
print(max(maxlist))
  

Fixed submission:
n,k=map(int,input().split())
p=list(map(int,input().split()))
c=list(map(int,input().split()))
for i in range(n):
    p[i]-=1
ans=max(c)
for i in range(n):
    f=i
    r=0
    l=[]
    while True:
        f=p[f]
        l.append(c[f])
        r+=c[f]
        if f==i:
            break
    t=0
    for j in range(len(l)):
        t+=l[j]
        ret=t
        if r>0:
            e=(k-j-1)//len(l)
            ret+=r*e
        ans=max(ans,ret)
print(ans)