from collections import deque
import sys, copy,itertools
input = sys.stdin.readline

n, k = map(int,input().split())
#P = list(map(int,input().split()))
P = list(map(lambda x : int(x) - 1, input().split())) #ひとつづつずらす
C = list(map(int,input().split()))
SCORE = [0]*(n+1)

max_score = -10**12
for i in range(n):
  x = i
  TEMPMAX = [-10**9] * (n+1)
  for j in range(1, n+1):
    SCORE[j] = SCORE[j-1] + C[P[x]]
    TEMPMAX[j]= max(TEMPMAX[j-1], SCORE[j])
    x = P[x]
    if x == i:
      break
  if j >= k:
    max_score = max(max_score,TEMPMAX[k])
  elif SCORE[j] < 0:
    max_score = max(max_score, TEMPMAX[j])
  else:
    m = k//j
    a = SCORE[j] *(m-1) + TEMPMAX[j]
    b = SCORE[j] *m + TEMPMAX[k % j]
    max_score = max(max_score, a, b)
print(max_score) from collections import deque import sys, copy,itertools sys copy itertools input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline n, k = map(int,input().split()) n map(int,input().split()) map int input().split() ().split () input split k P = list(map(lambda x : int(x) - 1, input().split())) P list(map(lambda x : int(x) - 1, input().split())) list map(lambda x : int(x) - 1, input().split()) map lambda x : int(x) - 1 int(x) - 1 int(x) int x 1 x input().split() ().split () input split C = list(map(int,input().split())) C list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split SCORE = [0]*(n+1) SCORE [0]*(n+1) [0] 0 n+1 n 1 max_score = -10**12 max_score -10**12 -10 12 for i in range(n):
  x = i
  TEMPMAX = [-10**9] * (n+1)
  for j in range(1, n+1):
    SCORE[j] = SCORE[j-1] + C[P[x]]
    TEMPMAX[j]= max(TEMPMAX[j-1], SCORE[j])
    x = P[x]
    if x == i:
      break
  if j >= k:
    max_score = max(max_score,TEMPMAX[k])
  elif SCORE[j] < 0:
    max_score = max(max_score, TEMPMAX[j])
  else:
    m = k//j
    a = SCORE[j] *(m-1) + TEMPMAX[j]
    b = SCORE[j] *m + TEMPMAX[k % j]
    max_score = max(max_score, a, b) i range(n) range n x = i x i TEMPMAX = [-10**9] * (n+1) TEMPMAX [-10**9] * (n+1) [-10**9] -10**9 -10 9 n+1 n 1 for j in range(1, n+1):
    SCORE[j] = SCORE[j-1] + C[P[x]]
    TEMPMAX[j]= max(TEMPMAX[j-1], SCORE[j])
    x = P[x]
    if x == i:
      break
   j range(1, n+1) range 1 n+1 n 1 SCORE[j] = SCORE[j-1] + C[P[x]] SCORE[j] SCORE j SCORE[j-1] + C[P[x]] SCORE[j-1] SCORE j-1 j 1 C[P[x]] C P[x] P x TEMPMAX[j]= max(TEMPMAX[j-1], SCORE[j]) TEMPMAX[j] TEMPMAX j max(TEMPMAX[j-1], SCORE[j]) max TEMPMAX[j-1] TEMPMAX j-1 j 1 SCORE[j] SCORE j x = P[x] x P[x] P x if x == i:
      break
   x == i x i break if j >= k:
    max_score = max(max_score,TEMPMAX[k])
  elif SCORE[j] < 0:
    max_score = max(max_score, TEMPMAX[j])
  else:
    m = k//j
    a = SCORE[j] *(m-1) + TEMPMAX[j]
    b = SCORE[j] *m + TEMPMAX[k % j]
    max_score = max(max_score, a, b) j >= k j k max_score = max(max_score,TEMPMAX[k]) max_score max(max_score,TEMPMAX[k]) max max_score TEMPMAX[k] TEMPMAX k elif SCORE[j] < 0:
    max_score = max(max_score, TEMPMAX[j])
   SCORE[j] < 0 SCORE[j] SCORE j 0 max_score = max(max_score, TEMPMAX[j]) max_score max(max_score, TEMPMAX[j]) max max_score TEMPMAX[j] TEMPMAX j m = k//j m k//j k j a = SCORE[j] *(m-1) + TEMPMAX[j] a SCORE[j] *(m-1) + TEMPMAX[j] SCORE[j] *(m-1) SCORE[j] SCORE j m-1 m 1 TEMPMAX[j] TEMPMAX j b = SCORE[j] *m + TEMPMAX[k % j] b SCORE[j] *m + TEMPMAX[k % j] SCORE[j] *m SCORE[j] SCORE j m TEMPMAX[k % j] TEMPMAX k % j k j max_score = max(max_score, a, b) max_score max(max_score, a, b) max max_score a b print(max_score) print max_score m = k//j k//j m x = i i x a = SCORE[j] *(m-1) + TEMPMAX[j] SCORE[j] *(m-1) + TEMPMAX[j] a max_score = max(max_score, a, b) max(max_score, a, b) max_score b = SCORE[j] *m + TEMPMAX[k % j] SCORE[j] *m + TEMPMAX[k % j] b input = sys.stdin.readline sys.stdin.readline input n, k = map(int,input().split()) map(int,input().split()) n C = list(map(int,input().split())) list(map(int,input().split())) C max_score = max(max_score,TEMPMAX[k]) max(max_score,TEMPMAX[k]) max_score P = list(map(lambda x : int(x) - 1, input().split())) list(map(lambda x : int(x) - 1, input().split())) P k = map(int,input().split()) map(int,input().split()) k x = P[x] P[x] x TEMPMAX = [-10**9] * (n+1) [-10**9] * (n+1) TEMPMAX SCORE = [0]*(n+1) [0]*(n+1) SCORE max_score = -10**12 -10**12 max_score max_score = max(max_score, TEMPMAX[j]) max(max_score, TEMPMAX[j]) max_score
from collections import deque
import sys, copy,itertools
input = sys.stdin.readline

n, k = map(int,input().split())
#P = list(map(int,input().split()))
P = list(map(lambda x : int(x) - 1, input().split())) #ひとつづつずらす
C = list(map(int,input().split()))
SCORE = [0]*(n+1)

max_score = -10**12
for i in range(n):
  x = i
  TEMPMAX = [-10**9] * (n+1)
  for j in range(1, n+1):
    SCORE[j] = SCORE[j-1] + C[P[x]]
    TEMPMAX[j]= max(TEMPMAX[j-1], SCORE[j])
    x = P[x]
    if x == i:
      break
  if j >= k:
    max_score = max(max_score,TEMPMAX[k])
  elif SCORE[j] < 0:
    max_score = max(max_score, TEMPMAX[j])
  else:
    m = k//j
    a = SCORE[j] *(m-1) + TEMPMAX[j]
    b = SCORE[j] *m + TEMPMAX[k % j]
    max_score = max(max_score, a, b)
print(max_score)