ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n,k = nm()
p = nl()
c = nl()

for i in range(n):
  p[i]-=1

ans = -10**(18)
for si in range(n):
  x = si
  s = []
  tot = 0
  while(True):
    x = p[x]
    s.append(c[x])
    tot += c[x]
    if x == si:
      break
  l = len(s)
  t = 0
  for i in range(l):
    t += s[i]
    if i+1>k:
      break
    now = t
    if tot > 0:
      e = (k-(i+1))//l
      now += tot*e
    ans = max(ans,now)

print(ans) ni = lambda: int(input()) ni lambda: int(input()) int(input()) int input() input nm = lambda: map(int, input().split()) nm lambda: map(int, input().split()) map(int, input().split()) map int input().split() ().split () input split nl = lambda: list(map(int, input().split())) nl lambda: list(map(int, input().split())) list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split n,k = nm() n nm() nm k p = nl() p nl() nl c = nl() c nl() nl for i in range(n):
  p[i]-=1 i range(n) range n p[i]-=1 p[i] p i 1 ans = -10**(18) ans -10**(18) -10 18 for si in range(n):
  x = si
  s = []
  tot = 0
  while(True):
    x = p[x]
    s.append(c[x])
    tot += c[x]
    if x == si:
      break
  l = len(s)
  t = 0
  for i in range(l):
    t += s[i]
    if i+1>k:
      break
    now = t
    if tot > 0:
      e = (k-(i+1))//l
      now += tot*e
    ans = max(ans,now) si range(n) range n x = si x si s = [] s [] tot = 0 tot 0 while(True):
    x = p[x]
    s.append(c[x])
    tot += c[x]
    if x == si:
      break
   True x = p[x] x p[x] p x s.append(c[x]) s.append s append c[x] c x tot += c[x] tot c[x] c x if x == si:
      break
   x == si x si break l = len(s) l len(s) len s t = 0 t 0 for i in range(l):
    t += s[i]
    if i+1>k:
      break
    now = t
    if tot > 0:
      e = (k-(i+1))//l
      now += tot*e
    ans = max(ans,now) i range(l) range l t += s[i] t s[i] s i if i+1>k:
      break
     i+1>k i+1 i 1 k break now = t now t if tot > 0:
      e = (k-(i+1))//l
      now += tot*e
     tot > 0 tot 0 e = (k-(i+1))//l e (k-(i+1))//l k-(i+1) k i+1 i 1 l now += tot*e now tot*e tot e ans = max(ans,now) ans max(ans,now) max ans now print(ans) print ans ans = -10**(18) -10**(18) ans tot = 0 0 tot ni = lambda: int(input()) lambda: int(input()) ni t = 0 0 t x = si si x p = nl() nl() p x = p[x] p[x] x k = nm() nm() k now = t t now ans = max(ans,now) max(ans,now) ans nl = lambda: list(map(int, input().split())) lambda: list(map(int, input().split())) nl t += s[i] s[i] t nm = lambda: map(int, input().split()) lambda: map(int, input().split()) nm n,k = nm() nm() n s = [] [] s now += tot*e tot*e now l = len(s) len(s) l tot += c[x] c[x] tot e = (k-(i+1))//l (k-(i+1))//l e c = nl() nl() c
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n,k = nm()
p = nl()
c = nl()

for i in range(n):
  p[i]-=1

ans = -10**(18)
for si in range(n):
  x = si
  s = []
  tot = 0
  while(True):
    x = p[x]
    s.append(c[x])
    tot += c[x]
    if x == si:
      break
  l = len(s)
  t = 0
  for i in range(l):
    t += s[i]
    if i+1>k:
      break
    now = t
    if tot > 0:
      e = (k-(i+1))//l
      now += tot*e
    ans = max(ans,now)

print(ans)