Buggy python submission (verdict: Runtime Error):
n,k = map(int, input().split())
p = list(map(int, input().split())) 
c = list(map(int, input().split()))
r = -10**9
for i in range(n):
  ans=c[i]
  e =[ans]
  f = i
  a = 1
  while a<k:
    i = p[i]-1
    if i == f:
      break
    a+=1
    ans+=c[i]
    e.append(ans)
  if e[-1]>0 and a<k:
    w = k%len(e)
    y = k//len(e)*e[-1]+max(e[:w])
    g=y
  else:
    g=max(e)
  r = max(r,g)
  
print(r)

Fixed submission:
n,k = map(int, input().split())
p = list(map(int, input().split())) 
c = list(map(int, input().split()))
r = -10**9
for i in range(n):
  ans=c[i]
  e =[ans]
  f = i
  a = 1
  while a<k:
    i = p[i]-1
    if i == f:
      break
    a+=1
    ans+=c[i]
    e.append(ans)
  if e[-1]>0 and a<k:
    w = k%len(e)
    if w!=0:
      y = k//len(e)*e[-1]+max(e[:w])
    else:
      y =( k//len(e)-1)*e[-1]+max(e)
    g=y
  else:
    g=max(e)
  r = max(r,g)
print(r)

Buggy python submission (verdict: Runtime Error):
n,k = map(int, input().split())
p = list(map(int, input().split())) 
c = list(map(int, input().split()))
r = -10**9
for i in range(n):
  ans=c[i]
  e =[ans]
  f = i
  a = 1
  while a<k:
    i = p[i]-1
    if i == f:
      break
    a+=1
    ans+=c[i]
    e.append(ans)
  if e[-1]>0 and a<k:
    w = k%len(e)
    y = k//len(e)*e[-1]+max(e[:w])
    g=y
  else:
    g=max(e)
  r = max(r,g)
  
print(r)

Fixed submission:
n,k = map(int, input().split())
p = list(map(int, input().split())) 
c = list(map(int, input().split()))
r = -10**9
for i in range(n):
  ans=c[i]
  e =[ans]
  f = i
  a = 1
  while a<k:
    i = p[i]-1
    if i == f:
      break
    a+=1
    ans+=c[i]
    e.append(ans)
  if e[-1]>0 and a<k:
    w = k%len(e)
    if w!=0:
      y = k//len(e)*e[-1]+max(e[:w])
    else:
      y =( k//len(e)-1)*e[-1]+max(e)
    g=y
  else:
    g=max(e)
  r = max(r,g)
print(r)

Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
P = list(map(int, input().split()))
for i in range(N):
  P[i] -= 1
C = list(map(int, input().split()))

cycle = [[] for i in range(N)]
loop = [0 for i in range(N)]
imax = [0 for i in range(N)]

for i in range(N):
  loop[i] += C[i]
  j = P[i]
  cycle[i] += [j]
  while(j != i):
    loop[i] += C[j]
    j = P[j]
    cycle[i] += [j]

for i in range(N):
  if K > len(cycle[i]) and loop[i] > 0:
    q = int(K/len(cycle[i]))
    r = K % len(cycle[i])
    iscore = [0 for k in range(len(cycle[i]))]
    for j in range(len(cycle[i])):
      for k in range(len(cycle[i]))[j:]:
        iscore[k] += C[cycle[i][j]]
    imax[i] = max(max(iscore), max(iscore[:r])+loop[i]) + loop[i]*(q-1)
  else:
    iscore = [0 for i in range(len(cycle[i]))]
    for j in range(len(cycle[i])):
      for k in range(len(cycle[i]))[j:]:
        iscore[k] += C[cycle[i][j]]
    imax[i] = max(iscore)
print(max(imax))

Fixed submission:
n,k = map(int, input().split())
p = list(map(int, input().split())) 
c = list(map(int, input().split()))
r = -10**9
for i in range(n):
  ans=c[i]
  e =[ans]
  f = i
  a = 1
  while a<k:
    i = p[i]-1
    if i == f:
      break
    a+=1
    ans+=c[i]
    e.append(ans)
  if e[-1]>0 and a<k:
    w = k%len(e)
    if w!=0:
      y = k//len(e)*e[-1]+max(e[:w])
    else:
      y =( k//len(e)-1)*e[-1]+max(e)
    g=y
  else:
    g=max(e)
  r = max(r,g)
print(r)

Buggy python submission (verdict: Runtime Error):
n,k = map(int, input().split())
p = list(map(int, input().split())) 
c = list(map(int, input().split()))
g = []
for i in range(n):
  ans=c[i]
  e =[ans]
  f = i
  a = 1
  while a<k:
    i = p[i]-1
    if i == f:
      break
    a+=1
    ans+=c[i]
    e.append(ans)
  if e[-1]>0 and a<k:
    w = k%len(e)
    y = k//len(e)*e[-1]+max(e[:w])
    g.append(y)
  else:
    g.append(max(e))
print(max(g))

Fixed submission:
n,k = map(int, input().split())
p = list(map(int, input().split())) 
c = list(map(int, input().split()))
r = -10**9
for i in range(n):
  ans=c[i]
  e =[ans]
  f = i
  a = 1
  while a<k:
    i = p[i]-1
    if i == f:
      break
    a+=1
    ans+=c[i]
    e.append(ans)
  if e[-1]>0 and a<k:
    w = k%len(e)
    if w!=0:
      y = k//len(e)*e[-1]+max(e[:w])
    else:
      y =( k//len(e)-1)*e[-1]+max(e)
    g=y
  else:
    g=max(e)
  r = max(r,g)
print(r)

Buggy python submission (verdict: Runtime Error):
n,k = map(int, input().split())
p = list(map(int, input().split())) 
c = list(map(int, input().split()))
g = []
for i in range(n):
  ans=c[i]
  e =[ans]
  f = i
  a = 1
  while a<k:
    i = p[i]-1
    if i == f:
      break
    a+=1
    ans+=c[i]
    e.append(ans)
  if e[-1]>0 and a!=k:
    w = k%len(e)
    y = k//len(e)*e[-1]+max(e[:w])
    g.append(y)
  else:
    g.append(max(e))
print(max(g))

Fixed submission:
n,k = map(int, input().split())
p = list(map(int, input().split())) 
c = list(map(int, input().split()))
r = -10**9
for i in range(n):
  ans=c[i]
  e =[ans]
  f = i
  a = 1
  while a<k:
    i = p[i]-1
    if i == f:
      break
    a+=1
    ans+=c[i]
    e.append(ans)
  if e[-1]>0 and a<k:
    w = k%len(e)
    if w!=0:
      y = k//len(e)*e[-1]+max(e[:w])
    else:
      y =( k//len(e)-1)*e[-1]+max(e)
    g=y
  else:
    g=max(e)
  r = max(r,g)
print(r)