Buggy python submission (verdict: Runtime Error):
import sys
import numpy as np
input = sys.stdin.readline

n, k = map(int, input().split())
parr = list(map(int, input().split()))
carr = list(map(int, input().split()))
scores = []

for i in range(n):
  cur = i
  # 1ループで通るノードの数とスコア
  roop_num = 0
  roop_score = 0
  while True:
    cur = parr[cur] - 1
    roop_num += 1
    roop_score += carr[cur]
    if cur == i:
      break
  tmp_max = -10**9
  # ループしたときにスコアが減るとき
  # kが1ループの回数より小さいとき
  if roop_score <= 0 or roop_num > k:
    score = 0
    max_iter = min(roop_num, k)
  else:
    # ループする回数
    n = k // roop_num
    score = roop_score*n
    max_iter = k - roop_num*n
  for _ in range(max_iter):
      cur = parr[cur] - 1
      score += carr[cur]
      if score > tmp_max:
        tmp_max = score
  scores += [tmp_max]

print(max(scores))

Fixed submission:
import sys
input = sys.stdin.readline

n, k = map(int, input().split())
parr = list(map(int, input().split()))
carr = list(map(int, input().split()))
scores = []

for i in range(n):
  cur = i
  # 1ループで通るノードの数とスコア
  roop_num = 0
  roop_score = 0
  while True:
    cur = parr[cur] - 1
    roop_num += 1
    roop_score += carr[cur]
    if cur == i:
      break
  tmp_max = -10**9
  # ループしたときにスコアが減るとき
  # kが1ループの回数より小さいとき
  if roop_score <= 0:
    score = 0
    max_iter = roop_num
  elif k <= roop_num:
    score = 0
    max_iter = k
  else:
    # ループする回数
    num = k // roop_num - 1
    score = roop_score*num
    max_iter = k - roop_num*num
  cur = i
  for _ in range(max_iter):
      cur = parr[cur] - 1
      score += carr[cur]
      if score > tmp_max:
        tmp_max = score
  scores += [tmp_max]

print(max(scores))

Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

n, k = map(int, input().split())
parr = list(map(int, input().split()))
carr = list(map(int, input().split()))

dp = [[0]*n for _ in range(k+1)]
maximum = -10**9

for i in range(1, k+1):
    for j in range(n):
      tmp = dp[i-1][j] + carr[j]
      dp[i][parr[j]-1] = tmp
      if tmp > maximum:
        maximum = tmp

print(int(maximum))

Fixed submission:
import sys
input = sys.stdin.readline

n, k = map(int, input().split())
parr = list(map(int, input().split()))
carr = list(map(int, input().split()))
scores = []

for i in range(n):
  cur = i
  # 1ループで通るノードの数とスコア
  roop_num = 0
  roop_score = 0
  while True:
    cur = parr[cur] - 1
    roop_num += 1
    roop_score += carr[cur]
    if cur == i:
      break
  tmp_max = -10**9
  # ループしたときにスコアが減るとき
  # kが1ループの回数より小さいとき
  if roop_score <= 0:
    score = 0
    max_iter = roop_num
  elif k <= roop_num:
    score = 0
    max_iter = k
  else:
    # ループする回数
    num = k // roop_num - 1
    score = roop_score*num
    max_iter = k - roop_num*num
  cur = i
  for _ in range(max_iter):
      cur = parr[cur] - 1
      score += carr[cur]
      if score > tmp_max:
        tmp_max = score
  scores += [tmp_max]

print(max(scores))

Buggy python submission (verdict: Runtime Error):
import sys
import numpy as np
input = sys.stdin.readline

n, k = map(int, input().split())
parr = list(map(int, input().split()))
carr = list(map(int, input().split()))

dp = np.zeros((k+1, n))
maximum = -10**9

for i in range(1, k+1):
    for j in range(n):
      tmp = dp[i-1][j] + carr[j]
      dp[i][parr[j]-1] = tmp
      if tmp > maximum:
        maximum = tmp

print(int(maximum))

Fixed submission:
import sys
input = sys.stdin.readline

n, k = map(int, input().split())
parr = list(map(int, input().split()))
carr = list(map(int, input().split()))
scores = []

for i in range(n):
  cur = i
  # 1ループで通るノードの数とスコア
  roop_num = 0
  roop_score = 0
  while True:
    cur = parr[cur] - 1
    roop_num += 1
    roop_score += carr[cur]
    if cur == i:
      break
  tmp_max = -10**9
  # ループしたときにスコアが減るとき
  # kが1ループの回数より小さいとき
  if roop_score <= 0:
    score = 0
    max_iter = roop_num
  elif k <= roop_num:
    score = 0
    max_iter = k
  else:
    # ループする回数
    num = k // roop_num - 1
    score = roop_score*num
    max_iter = k - roop_num*num
  cur = i
  for _ in range(max_iter):
      cur = parr[cur] - 1
      score += carr[cur]
      if score > tmp_max:
        tmp_max = score
  scores += [tmp_max]

print(max(scores))