Problem p02585 (python)

Fixed submission:
N, K = map(int,input().split())
p = list(map(int,input().split()))
c = list(map(int,input().split()))
K = K-1
ans = -10**9-1
for h in range(N):
   i = h+1
   start = i
   circle = [c[i-1]]
   i = p[i-1]
   while i != start:
      score = circle[-1] + c[i-1]
      circle.append(score)
      i = p[i-1]
   l=len(circle)
   for j in range(min(l, K+1)):
      if circle[l-1] > 0:
         score = circle[j] + ((K - j)//l)*circle[l-1]
      else:
         score = circle[j]
      ans = max(score, ans)      
print(ans)

Buggy submission (verdict: Runtime Error):
N, K = map(int,input().split())
p = list(map(int,input().split()))
c = list(map(int,input().split()))
K = K-1
ans = -10**9-1
for h in range(N):
   i += 1
   start = i
   circle = [c[i-1]]
   i = p[i-1]
   while i != start:
      l = len(circle)
      score = circle[l-1] + c[i-1]
      circle.append(score)
      i = p[i-1]
   l=len(circle)
   for j in range(min(l, K+1)):
      if circle[l-1] > 0:
         score = circle[j] + ((K - j)//l)*circle[l-1]
      else:
         score = circle[j]
      ans = max(score, ans)      
print(ans)

Buggy submission (verdict: Runtime Error):
N, K = map(int,input().split())
p = list(map(int,input().split()))
c = list(map(int,input().split()))
K = K-1
ans = -10**9-1
for h in range(N):%Here, the initial number is fixed and 
   i += 1
   start = i
   circle = [c[i-1]]
   i = p[i-1]
   while i != start:
      l = len(circle)
      score = circle[l-1] + c[i-1]
      circle.append(score)
      i = p[i-1]
   l=len(circle)
   for j in range(min(l, K+1)):
      if circle[l-1] > 0:
         score = circle[j] + ((K - j)//l)*circle[l-1]
      else:
         score = circle[j]
      ans = max(score, ans)      
print(ans)

Buggy submission (verdict: Runtime Error):
N, K = map(int,input().split())
p = list(map(int,input().split()))
c = list(map(int,input().split()))
K = K-1
ans = -10**9-1
for _ in range(N):%Here, the initial number is fixed and 
   i += 1
   start = i
   circle = [c[i-1]]
   i = p[i-1]
   while i != start:
      l = len(circle)
      score = circle[l-1] + c[i-1]
      circle.append(score)
      i = p[i-1]
   l=len(circle)
   for j in range(min(l, K+1)):
      if circle[l-1] > 0:
         score = circle[j] + ((K - j)//l)*circle[l-1]
      else:
         score = circle[j]
      ans = max(score, ans)      
print(ans)

Buggy submission (verdict: Runtime Error):
N, K = map(int,input().split())
p = list(map(int,input().split()))
c = list(map(int,input().split()))
K = K-1
ans = -10**9-1
for i in range(N):
   i += 1
   start = i
   number = 1
   circle = [c[i-1]]
   i = p[i-1]
   while i != start:
      number += 1
      l = len(circle)
      score = circle[l-1] + c[i-1]
      circle.append(score)
      i = p[i-1]
   for j in range(min(l, K+1):
      if circle[l-1] > 0:
         score = circle[j] + ((K - j)//l)*circle[l-1]
      else:
         score = circle[j]
      ans = max(score, ans)      
print(ans)

Buggy submission (verdict: Runtime Error):
N, K = map(int,input().split())
p = list(map(int,input().split()))
c = list(map(int,input().split()))
K = K-1
ans = -10**9-1
for i in range(N):
   i += 1
   start = i
   circle = 1
   number = c[i-1]
   while i != start:
      i = p[i-1]
      circle += c[i-1]
      number += 1
   for j in range (min(number, K+1)):
      sum = 0
      for k in range(j+1):
         index = (i + k - 1)%number
         sum += c[index]
      if circle > 0:
         sum = circle*((K-j)//number) + sum
      ans = max(sum, ans)
print(ans)

Buggy submission (verdict: Runtime Error):

N, K = map(int,input().split())
p = list(map(int,input().split()))
c = list(map(int,input().split()))
K = K-1
ans = -10**9-1
for i in range(N):
   i += 1
   start = i
   circle = 1
   number = c[i-1]
   while i != start:
      i = p[i-1]
      circle += c[i-1]
      number += 1
   for j in range (min(number, K+1)):
      sum = 0
      for k in range(i, i+j+1):
         index = (i + j - 1)%number
         sum += c[index]
      if circle > 0:
         sum = circle*((K-j)//number) + sum
      ans = max(sum, ans)
print(ans)

Buggy submission (verdict: Runtime Error):
N, K = map(int,input().split())
p = list(map(int,input().split()))
c = list(map(int,input().split()))
K = K-1
ans = -10**9-1
for i in range(N):
   i += 1
   start = i
   circle = 1
   number = c[i-1]
   while i != start:
      circle += c[i-1]
      number += 1
      i = p[i-1]
   for j in range (min(number, K+1)):
      sum = 0
      for k in range(i, i+j+1):
         sum += c[(k-1)%number]
      if circle > 0:
         sum = circle*((K-j)//number) + sum
      ans = max(sum, ans)
print(ans)

Buggy submission (verdict: Runtime Error):
N, K = map(int,input().split())
p = list(map(int,input().split()))
c = list(map(int,input().split()))
K = K-1
ans = -10**9-1
for i in range(N):
   i += 1
   start = i
   circle = 1
   number = c[i-1]
   while i != start:
      circle += c[i-1]
      number += 1
      i = p[i-1]
   for j in range (min(number, K+1)):
      sum = 0
      for k in range(i, i+j+1):
         sum += c[k-1]
      if circle > 0:
         sum = circle*((K-j)//number) + sum
      ans = max(sum, ans)
print(ans)