f=lambda:[*map(int,input().split())]
n,k=f()
p,c=f(),f()
p=[i-1 for i in p]
a=-10**9
for i in range(n):
  x,l,s,t=i,[],0,0
  while 1:
    x=p[x]
    l+=[c[x]]
    s+=c[x]
    if x==i: break
  m=len(l)
  for j in range(m):
    if j+1>k: break
    t+=l[j]
    a=max(a,t+(k-j-1)//m*s*(s>0))
print(a) f=lambda:[*map(int,input().split())] f lambda:[*map(int,input().split())] [*map(int,input().split())] *map(int,input().split()) map(int,input().split()) map int input().split() ().split () input split n,k=f() n f() f k p,c=f(),f() p f() f c f() f i-1 for i in p for i in p i p for i in p i-1 i 1 p=[i-1 for i in p] p [i-1 for i in p] a=-10**9 a -10**9 -10 9 for i in range(n):
  x,l,s,t=i,[],0,0
  while 1:
    x=p[x]
    l+=[c[x]]
    s+=c[x]
    if x==i: break
  m=len(l)
  for j in range(m):
    if j+1>k: break
    t+=l[j]
    a=max(a,t+(k-j-1)//m*s*(s>0)) i range(n) range n x,l,s,t=i,[],0,0 x i l [] s 0 t 0 while 1:
    x=p[x]
    l+=[c[x]]
    s+=c[x]
    if x==i: break
   1 x=p[x] x p[x] p x l+=[c[x]] l [c[x]] c[x] c x s+=c[x] s c[x] c x if x==i: break
   x==i x i m=len(l) m len(l) len l for j in range(m):
    if j+1>k: break
    t+=l[j]
    a=max(a,t+(k-j-1)//m*s*(s>0)) j range(m) range m if j+1>k: break
     j+1>k j+1 j 1 k t+=l[j] t l[j] l j a=max(a,t+(k-j-1)//m*s*(s>0)) a max(a,t+(k-j-1)//m*s*(s>0)) max a t+(k-j-1)//m*s*(s>0) t (k-j-1)//m*s*(s>0) (k-j-1)//m*s (k-j-1)//m k-j-1 k-j k j 1 m s s>0 s 0 print(a) print a f=lambda:[*map(int,input().split())] lambda:[*map(int,input().split())] f c=f(),f() f() c p=[i-1 for i in p] [i-1 for i in p] p x=p[x] p[x] x t+=l[j] l[j] t l+=[c[x]] [c[x]] l x,l,s,t=i,[],0,0 i x a=max(a,t+(k-j-1)//m*s*(s>0)) max(a,t+(k-j-1)//m*s*(s>0)) a l,s,t=i,[],0,0 [] l s+=c[x] c[x] s n,k=f() f() n p,c=f(),f() f() p k=f() f() k a=-10**9 -10**9 a s,t=i,[],0,0 0 s t=i,[],0,0 0 t m=len(l) len(l) m
f=lambda:[*map(int,input().split())]
n,k=f()
p,c=f(),f()
p=[i-1 for i in p]
a=-10**9
for i in range(n):
  x,l,s,t=i,[],0,0
  while 1:
    x=p[x]
    l+=[c[x]]
    s+=c[x]
    if x==i: break
  m=len(l)
  for j in range(m):
    if j+1>k: break
    t+=l[j]
    a=max(a,t+(k-j-1)//m*s*(s>0))
print(a)