n,k = map(int,input().split())
p = list(map(int,input().split()))
c = list(map(int,input().split()))
p = [pi-1 for pi in p]

ans = -1e18
for i in range(n):
  cur = p[i]
  loop = [c[cur]]
  cnt = 1
  ans = max(ans,c[cur])
  while cur != i and cnt<k:
    cur = p[cur]
    tmp = loop[-1]+c[cur]
    loop.append(tmp)
    ans = max(ans,tmp)
    cnt += 1
  #print(loop)
  if cnt==k or loop[-1]<=0:continue
  a = k//len(loop) * loop[-1]
  b = (k//len(loop)-1) * loop[-1]
  ans = max(ans,a,b)
  for _ in range(k%len(loop)):
    a += c[cur]
    ans = max(ans,a)
    cur = p[cur]
  cur = i
  for _ in range(len(loop)):
    b += c[cur]
    ans = max(ans,b)
    cur = p[cur]
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 pi-1 for pi in p for pi in p pi p for pi in p pi-1 pi 1 p = [pi-1 for pi in p] p [pi-1 for pi in p] ans = -1e18 ans -1e18 for i in range(n):
  cur = p[i]
  loop = [c[cur]]
  cnt = 1
  ans = max(ans,c[cur])
  while cur != i and cnt<k:
    cur = p[cur]
    tmp = loop[-1]+c[cur]
    loop.append(tmp)
    ans = max(ans,tmp)
    cnt += 1
  #print(loop)
  if cnt==k or loop[-1]<=0:continue
  a = k//len(loop) * loop[-1]
  b = (k//len(loop)-1) * loop[-1]
  ans = max(ans,a,b)
  for _ in range(k%len(loop)):
    a += c[cur]
    ans = max(ans,a)
    cur = p[cur]
  cur = i
  for _ in range(len(loop)):
    b += c[cur]
    ans = max(ans,b)
    cur = p[cur] i range(n) range n cur = p[i] cur p[i] p i loop = [c[cur]] loop [c[cur]] c[cur] c cur cnt = 1 cnt 1 ans = max(ans,c[cur]) ans max(ans,c[cur]) max ans c[cur] c cur while cur != i and cnt<k:
    cur = p[cur]
    tmp = loop[-1]+c[cur]
    loop.append(tmp)
    ans = max(ans,tmp)
    cnt += 1
  #print(loop)
   cur != i and cnt<k cur != i cur i cnt<k cnt k cur = p[cur] cur p[cur] p cur tmp = loop[-1]+c[cur] tmp loop[-1]+c[cur] loop[-1] loop -1 c[cur] c cur loop.append(tmp) loop.append loop append tmp ans = max(ans,tmp) ans max(ans,tmp) max ans tmp cnt += 1 cnt 1 if cnt==k or loop[-1]<=0:continue
   cnt==k or loop[-1]<=0 cnt==k cnt k loop[-1]<=0 loop[-1] loop -1 0 a = k//len(loop) * loop[-1] a k//len(loop) * loop[-1] k//len(loop) k len(loop) len loop loop[-1] loop -1 b = (k//len(loop)-1) * loop[-1] b (k//len(loop)-1) * loop[-1] k//len(loop)-1 k//len(loop) k len(loop) len loop 1 loop[-1] loop -1 ans = max(ans,a,b) ans max(ans,a,b) max ans a b for _ in range(k%len(loop)):
    a += c[cur]
    ans = max(ans,a)
    cur = p[cur]
   _ range(k%len(loop)) range k%len(loop) k len(loop) len loop a += c[cur] a c[cur] c cur ans = max(ans,a) ans max(ans,a) max ans a cur = p[cur] cur p[cur] p cur cur = i cur i for _ in range(len(loop)):
    b += c[cur]
    ans = max(ans,b)
    cur = p[cur] _ range(len(loop)) range len(loop) len loop b += c[cur] b c[cur] c cur ans = max(ans,b) ans max(ans,b) max ans b cur = p[cur] cur p[cur] p cur print(ans) print ans cur = p[i] p[i] cur cur = i i cur cnt += 1 1 cnt a = k//len(loop) * loop[-1] k//len(loop) * loop[-1] a ans = max(ans,a,b) max(ans,a,b) ans cnt = 1 1 cnt p = [pi-1 for pi in p] [pi-1 for pi in p] p cur = p[cur] p[cur] cur cur = p[cur] p[cur] cur ans = -1e18 -1e18 ans k = map(int,input().split()) map(int,input().split()) k p = list(map(int,input().split())) list(map(int,input().split())) p ans = max(ans,tmp) max(ans,tmp) ans loop = [c[cur]] [c[cur]] loop b += c[cur] c[cur] b b = (k//len(loop)-1) * loop[-1] (k//len(loop)-1) * loop[-1] b a += c[cur] c[cur] a tmp = loop[-1]+c[cur] loop[-1]+c[cur] tmp ans = max(ans,a) max(ans,a) ans n,k = map(int,input().split()) map(int,input().split()) n c = list(map(int,input().split())) list(map(int,input().split())) c ans = max(ans,c[cur]) max(ans,c[cur]) ans ans = max(ans,b) max(ans,b) ans cur = p[cur] p[cur] cur
n,k = map(int,input().split())
p = list(map(int,input().split()))
c = list(map(int,input().split()))
p = [pi-1 for pi in p]

ans = -1e18
for i in range(n):
  cur = p[i]
  loop = [c[cur]]
  cnt = 1
  ans = max(ans,c[cur])
  while cur != i and cnt<k:
    cur = p[cur]
    tmp = loop[-1]+c[cur]
    loop.append(tmp)
    ans = max(ans,tmp)
    cnt += 1
  #print(loop)
  if cnt==k or loop[-1]<=0:continue
  a = k//len(loop) * loop[-1]
  b = (k//len(loop)-1) * loop[-1]
  ans = max(ans,a,b)
  for _ in range(k%len(loop)):
    a += c[cur]
    ans = max(ans,a)
    cur = p[cur]
  cur = i
  for _ in range(len(loop)):
    b += c[cur]
    ans = max(ans,b)
    cur = p[cur]
print(ans)