from collections import defaultdict
def f(s):
  return int(s) - 1

N, K = map(int, input().split())
P = list(map(f, input().split()))
C = list(map(int, input().split()))

loop = []

first = -1
x = -1
loop_mem = []
cnt = 0
score = 0

rem = set(range(N))

while len(rem) > 0:
  if first < 0:
    first = rem.pop()
    x = first
  loop_mem.append(x)
  if x != first:
    rem.remove(x)
  cnt += 1
  score += C[x]
  if first != P[x]:
    x = P[x]
  else:
    loop.append((loop_mem, cnt, score))
    first = -1
    loop_mem = []
    cnt = 0
    score = 0

ans = -1e10
for loop_mem, cnt, score in loop:
  imax = [-1e10 for _ in range(cnt)]
  imax[0] = 0
  for x in loop_mem:
    tmp = 0
    for j in range(1, cnt):
      tmp += C[x]
      imax[j] = max(imax[j], tmp)
      x = P[x]
  for j in range(2, cnt):
    imax[j] = max(imax[j-1], imax[j])
  if score <= 0:
    ans = max(ans, imax[-1])
  else:
    M = K // cnt
    m = K % cnt
    if M >= 1:
      tmp = max(score * M + max(imax[m], 0), score * (M-1) + imax[-1])
    else:
      tmp = imax[m]
    ans = max(ans, tmp)

print(ans) from collections import defaultdict def f(s):
  return int(s) - 1 f return int(s) - 1 int(s) - 1 int(s) int s 1 s s N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K P = list(map(f, input().split())) P list(map(f, input().split())) list map(f, input().split()) map f 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 loop = [] loop [] first = -1 first -1 x = -1 x -1 loop_mem = [] loop_mem [] cnt = 0 cnt 0 score = 0 score 0 rem = set(range(N)) rem set(range(N)) set range(N) range N while len(rem) > 0:
  if first < 0:
    first = rem.pop()
    x = first
  loop_mem.append(x)
  if x != first:
    rem.remove(x)
  cnt += 1
  score += C[x]
  if first != P[x]:
    x = P[x]
  else:
    loop.append((loop_mem, cnt, score))
    first = -1
    loop_mem = []
    cnt = 0
    score = 0 len(rem) > 0 len(rem) len rem 0 if first < 0:
    first = rem.pop()
    x = first
   first < 0 first 0 first = rem.pop() first rem.pop() rem.pop rem pop x = first x first loop_mem.append(x) loop_mem.append loop_mem append x if x != first:
    rem.remove(x)
   x != first x first rem.remove(x) rem.remove rem remove x cnt += 1 cnt 1 score += C[x] score C[x] C x if first != P[x]:
    x = P[x]
  else:
    loop.append((loop_mem, cnt, score))
    first = -1
    loop_mem = []
    cnt = 0
    score = 0 first != P[x] first P[x] P x x = P[x] x P[x] P x loop.append((loop_mem, cnt, score)) loop.append loop append (loop_mem, cnt, score) loop_mem cnt score first = -1 first -1 loop_mem = [] loop_mem [] cnt = 0 cnt 0 score = 0 score 0 ans = -1e10 ans -1e10 for loop_mem, cnt, score in loop:
  imax = [-1e10 for _ in range(cnt)]
  imax[0] = 0
  for x in loop_mem:
    tmp = 0
    for j in range(1, cnt):
      tmp += C[x]
      imax[j] = max(imax[j], tmp)
      x = P[x]
  for j in range(2, cnt):
    imax[j] = max(imax[j-1], imax[j])
  if score <= 0:
    ans = max(ans, imax[-1])
  else:
    M = K // cnt
    m = K % cnt
    if M >= 1:
      tmp = max(score * M + max(imax[m], 0), score * (M-1) + imax[-1])
    else:
      tmp = imax[m]
    ans = max(ans, tmp) loop_mem cnt score loop -1e10 for _ in range(cnt) for _ in range(cnt) _ range(cnt) range cnt for _ in range(cnt) -1e10 imax = [-1e10 for _ in range(cnt)] imax [-1e10 for _ in range(cnt)] imax[0] = 0 imax[0] imax 0 0 for x in loop_mem:
    tmp = 0
    for j in range(1, cnt):
      tmp += C[x]
      imax[j] = max(imax[j], tmp)
      x = P[x]
   x loop_mem tmp = 0 tmp 0 for j in range(1, cnt):
      tmp += C[x]
      imax[j] = max(imax[j], tmp)
      x = P[x]
   j range(1, cnt) range 1 cnt tmp += C[x] tmp C[x] C x imax[j] = max(imax[j], tmp) imax[j] imax j max(imax[j], tmp) max imax[j] imax j tmp x = P[x] x P[x] P x for j in range(2, cnt):
    imax[j] = max(imax[j-1], imax[j])
   j range(2, cnt) range 2 cnt imax[j] = max(imax[j-1], imax[j]) imax[j] imax j max(imax[j-1], imax[j]) max imax[j-1] imax j-1 j 1 imax[j] imax j if score <= 0:
    ans = max(ans, imax[-1])
  else:
    M = K // cnt
    m = K % cnt
    if M >= 1:
      tmp = max(score * M + max(imax[m], 0), score * (M-1) + imax[-1])
    else:
      tmp = imax[m]
    ans = max(ans, tmp) score <= 0 score 0 ans = max(ans, imax[-1]) ans max(ans, imax[-1]) max ans imax[-1] imax -1 M = K // cnt M K // cnt K cnt m = K % cnt m K % cnt K cnt if M >= 1:
      tmp = max(score * M + max(imax[m], 0), score * (M-1) + imax[-1])
    else:
      tmp = imax[m]
     M >= 1 M 1 tmp = max(score * M + max(imax[m], 0), score * (M-1) + imax[-1]) tmp max(score * M + max(imax[m], 0), score * (M-1) + imax[-1]) max score * M + max(imax[m], 0) score * M score M max(imax[m], 0) max imax[m] imax m 0 score * (M-1) + imax[-1] score * (M-1) score M-1 M 1 imax[-1] imax -1 tmp = imax[m] tmp imax[m] imax m ans = max(ans, tmp) ans max(ans, tmp) max ans tmp print(ans) print ans rem = set(range(N)) set(range(N)) rem m = K % cnt K % cnt m loop = [] [] loop ans = max(ans, imax[-1]) max(ans, imax[-1]) ans cnt = 0 0 cnt tmp = max(score * M + max(imax[m], 0), score * (M-1) + imax[-1]) max(score * M + max(imax[m], 0), score * (M-1) + imax[-1]) tmp loop_mem = [] [] loop_mem x = first first x M = K // cnt K // cnt M imax = [-1e10 for _ in range(cnt)] [-1e10 for _ in range(cnt)] imax cnt += 1 1 cnt tmp += C[x] C[x] tmp score = 0 0 score ans = max(ans, tmp) max(ans, tmp) ans loop_mem = [] [] loop_mem score = 0 0 score first = -1 -1 first N, K = map(int, input().split()) map(int, input().split()) N K = map(int, input().split()) map(int, input().split()) K P = list(map(f, input().split())) list(map(f, input().split())) P C = list(map(int, input().split())) list(map(int, input().split())) C tmp = 0 0 tmp tmp = imax[m] imax[m] tmp first = rem.pop() rem.pop() first x = P[x] P[x] x def f(s):
  return int(s) - 1 def f(s):
  return int(s) - 1 f x = -1 -1 x x = P[x] P[x] x cnt = 0 0 cnt score += C[x] C[x] score first = -1 -1 first ans = -1e10 -1e10 ans
from collections import defaultdict
def f(s):
  return int(s) - 1

N, K = map(int, input().split())
P = list(map(f, input().split()))
C = list(map(int, input().split()))

loop = []

first = -1
x = -1
loop_mem = []
cnt = 0
score = 0

rem = set(range(N))

while len(rem) > 0:
  if first < 0:
    first = rem.pop()
    x = first
  loop_mem.append(x)
  if x != first:
    rem.remove(x)
  cnt += 1
  score += C[x]
  if first != P[x]:
    x = P[x]
  else:
    loop.append((loop_mem, cnt, score))
    first = -1
    loop_mem = []
    cnt = 0
    score = 0

ans = -1e10
for loop_mem, cnt, score in loop:
  imax = [-1e10 for _ in range(cnt)]
  imax[0] = 0
  for x in loop_mem:
    tmp = 0
    for j in range(1, cnt):
      tmp += C[x]
      imax[j] = max(imax[j], tmp)
      x = P[x]
  for j in range(2, cnt):
    imax[j] = max(imax[j-1], imax[j])
  if score <= 0:
    ans = max(ans, imax[-1])
  else:
    M = K // cnt
    m = K % cnt
    if M >= 1:
      tmp = max(score * M + max(imax[m], 0), score * (M-1) + imax[-1])
    else:
      tmp = imax[m]
    ans = max(ans, tmp)

print(ans)
