# 解答

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  

visit = [False for _ in range(N)]
loops = []

for i in range(N):
  if visit[i]:
    continue
  now = i
  loop = [C[now]]
  visit[now] = True
  while not visit[P[now]]:
    now = P[now]
    loop.append(C[now])
    visit[now] = True
  loops.append(loop)

ans = -float("inf")
for loop in loops:
  n = len(loop)
  r = K%n
  m = K//n
  if r==0:
    r = n
    m -= 1
  suml = sum(loop)
  if K > n and suml <= 0:
    r = n
    m = 0
  cumsum = [0] + loop + loop[:r-1]
  for i in range(n+r-1):
    cumsum[i+1] = cumsum[i] + cumsum[i+1]
  maxC = -float("inf")
  for i in range(n):
    for j in range(i+1, i+r+1):
      maxC = max(maxC, cumsum[j]-cumsum[i])
  ans = max(ans, maxC + suml*m)
print(ans) N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K P = list(map(int, input().split())) P list(map(int, input().split())) list map(int, input().split()) map int 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 for i in range(N):
  P[i] -= 1   i range(N) range N P[i] -= 1 P[i] P i 1 False for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) False visit = [False for _ in range(N)] visit [False for _ in range(N)] loops = [] loops [] for i in range(N):
  if visit[i]:
    continue
  now = i
  loop = [C[now]]
  visit[now] = True
  while not visit[P[now]]:
    now = P[now]
    loop.append(C[now])
    visit[now] = True
  loops.append(loop) i range(N) range N if visit[i]:
    continue
   visit[i] visit i continue now = i now i loop = [C[now]] loop [C[now]] C[now] C now visit[now] = True visit[now] visit now True while not visit[P[now]]:
    now = P[now]
    loop.append(C[now])
    visit[now] = True
   not visit[P[now]] visit[P[now]] visit P[now] P now now = P[now] now P[now] P now loop.append(C[now]) loop.append loop append C[now] C now visit[now] = True visit[now] visit now True loops.append(loop) loops.append loops append loop ans = -float("inf") ans -float("inf") float("inf") float "inf" for loop in loops:
  n = len(loop)
  r = K%n
  m = K//n
  if r==0:
    r = n
    m -= 1
  suml = sum(loop)
  if K > n and suml <= 0:
    r = n
    m = 0
  cumsum = [0] + loop + loop[:r-1]
  for i in range(n+r-1):
    cumsum[i+1] = cumsum[i] + cumsum[i+1]
  maxC = -float("inf")
  for i in range(n):
    for j in range(i+1, i+r+1):
      maxC = max(maxC, cumsum[j]-cumsum[i])
  ans = max(ans, maxC + suml*m) loop loops n = len(loop) n len(loop) len loop r = K%n r K%n K n m = K//n m K//n K n if r==0:
    r = n
    m -= 1
   r==0 r 0 r = n r n m -= 1 m 1 suml = sum(loop) suml sum(loop) sum loop if K > n and suml <= 0:
    r = n
    m = 0
   K > n and suml <= 0 K > n K n suml <= 0 suml 0 r = n r n m = 0 m 0 cumsum = [0] + loop + loop[:r-1] cumsum [0] + loop + loop[:r-1] [0] + loop [0] 0 loop loop[:r-1] loop :r-1 r-1 r 1 for i in range(n+r-1):
    cumsum[i+1] = cumsum[i] + cumsum[i+1]
   i range(n+r-1) range n+r-1 n+r n r 1 cumsum[i+1] = cumsum[i] + cumsum[i+1] cumsum[i+1] cumsum i+1 i 1 cumsum[i] + cumsum[i+1] cumsum[i] cumsum i cumsum[i+1] cumsum i+1 i 1 maxC = -float("inf") maxC -float("inf") float("inf") float "inf" for i in range(n):
    for j in range(i+1, i+r+1):
      maxC = max(maxC, cumsum[j]-cumsum[i])
   i range(n) range n for j in range(i+1, i+r+1):
      maxC = max(maxC, cumsum[j]-cumsum[i])
   j range(i+1, i+r+1) range i+1 i 1 i+r+1 i+r i r 1 maxC = max(maxC, cumsum[j]-cumsum[i]) maxC max(maxC, cumsum[j]-cumsum[i]) max maxC cumsum[j]-cumsum[i] cumsum[j] cumsum j cumsum[i] cumsum i ans = max(ans, maxC + suml*m) ans max(ans, maxC + suml*m) max ans maxC + suml*m maxC suml*m suml m print(ans) print ans r = n n r r = K%n K%n r r = n n r C = list(map(int, input().split())) list(map(int, input().split())) C P = list(map(int, input().split())) list(map(int, input().split())) P K = map(int, input().split()) map(int, input().split()) K n = len(loop) len(loop) n now = P[now] P[now] now suml = sum(loop) sum(loop) suml now = i i now maxC = -float("inf") -float("inf") maxC loop = [C[now]] [C[now]] loop m -= 1 1 m N, K = map(int, input().split()) map(int, input().split()) N cumsum = [0] + loop + loop[:r-1] [0] + loop + loop[:r-1] cumsum visit = [False for _ in range(N)] [False for _ in range(N)] visit maxC = max(maxC, cumsum[j]-cumsum[i]) max(maxC, cumsum[j]-cumsum[i]) maxC loops = [] [] loops ans = -float("inf") -float("inf") ans m = K//n K//n m m = 0 0 m ans = max(ans, maxC + suml*m) max(ans, maxC + suml*m) ans
# 解答

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  

visit = [False for _ in range(N)]
loops = []

for i in range(N):
  if visit[i]:
    continue
  now = i
  loop = [C[now]]
  visit[now] = True
  while not visit[P[now]]:
    now = P[now]
    loop.append(C[now])
    visit[now] = True
  loops.append(loop)

ans = -float("inf")
for loop in loops:
  n = len(loop)
  r = K%n
  m = K//n
  if r==0:
    r = n
    m -= 1
  suml = sum(loop)
  if K > n and suml <= 0:
    r = n
    m = 0
  cumsum = [0] + loop + loop[:r-1]
  for i in range(n+r-1):
    cumsum[i+1] = cumsum[i] + cumsum[i+1]
  maxC = -float("inf")
  for i in range(n):
    for j in range(i+1, i+r+1):
      maxC = max(maxC, cumsum[j]-cumsum[i])
  ans = max(ans, maxC + suml*m)
print(ans)