(n,k),p,c=[[*map(int,t.split())]for t in open(0)]
a=-9e9
for i in range(n):
 s=f=m=0;x,*l=i,
 while~f:x=p[x]-1;s+=c[x];l+=s,;m+=1;f-=x==i
 for j in range(min(m,k)):a=max(a,l[j]+(~j+k)//m*s*(s>0))
print(a) [*map(int,t.split())]for t in open(0) for t in open(0) t open(0) open 0 for t in open(0) [*map(int,t.split())] *map(int,t.split()) map(int,t.split()) map int t.split() t.split t split (n,k),p,c=[[*map(int,t.split())]for t in open(0)] (n,k) n k [[*map(int,t.split())]for t in open(0)] p c a=-9e9 a -9e9 for i in range(n):
 s=f=m=0;x,*l=i,
 while~f:x=p[x]-1;s+=c[x];l+=s,;m+=1;f-=x==i
 for j in range(min(m,k)):a=max(a,l[j]+(~j+k)//m*s*(s>0)) i range(n) range n s=f=m=0 s 0 =f=m=0 f 0 m x,*l=i, x i *l l while~f:x=p[x]-1;s+=c[x];l+=s,;m+=1;f-=x==i
  ~f f for j in range(min(m,k)):a=max(a,l[j]+(~j+k)//m*s*(s>0) j range(min(m,k)) range min(m,k) min m k print(a) print a s=f=m=0 0 s c=[[*map(int,t.split())]for t in open(0)] [[*map(int,t.split())]for t in open(0)] c f=m=0 0 f x,*l=i, i x a=-9e9 -9e9 a m=0 0 m p,c=[[*map(int,t.split())]for t in open(0)] [[*map(int,t.split())]for t in open(0)] p
(n,k),p,c=[[*map(int,t.split())]for t in open(0)]
a=-9e9
for i in range(n):
 s=f=m=0;x,*l=i,
 while~f:x=p[x]-1;s+=c[x];l+=s,;m+=1;f-=x==i
 for j in range(min(m,k)):a=max(a,l[j]+(~j+k)//m*s*(s>0))
print(a)