import sys
import resource
sys.setrecursionlimit(10000)
n,k=map(int,input().rstrip().split())
p=list(map(int,input().rstrip().split()))
c=list(map(int,input().rstrip().split()))
def find(start,now,up,max,sum,count,flag):
    if start==now:
        flag+=1
    if start==now and flag==2:
        return [max,sum,count]
    elif count==up:
        return [max,sum,count]
    else:
        count+=1
        sum+=c[p[now-1]-1]
        if max<sum:
            max=sum
        return find(start,p[now-1],up,max,sum,count,flag)
result=-10000000000
for i in range(1,n+1):
    m=find(i,i,n,c[p[i-1]-1],0,0,0)
    if m[2]>=k:
        m=find(i,i,k,c[p[i-1]-1],0,0,0)
        if m[0]>result:
            result=m[0]
        ans=result
    elif m[1]<=0:
        if m[0]>result:
            result=m[0]
        ans=result
    else:
        w=k%m[2]
        if w==0:
            w=m[2]
        spe=find(i,i,w,c[p[i-1]-1],0,0,0)[0]
        if m[1]+spe>m[0] and m[1]*(k-w)//m[2]+spe>result:
            result=m[1]*(k-w)//m[2]+spe
        elif m[1]+spe<=m[0] and m[1]*((k-w)//m[2]-1)+m[0]>result:
            result=m[1]*((k-w)//m[2]-1)+m[0]
        ans=result
print(ans) import sys sys import resource resource sys.setrecursionlimit(10000) sys.setrecursionlimit sys setrecursionlimit 10000 n,k=map(int,input().rstrip().split()) n map(int,input().rstrip().split()) map int input().rstrip().split() ().rstrip().split ().rstrip() ().rstrip () input rstrip split k p=list(map(int,input().rstrip().split())) p list(map(int,input().rstrip().split())) list map(int,input().rstrip().split()) map int input().rstrip().split() ().rstrip().split ().rstrip() ().rstrip () input rstrip split c=list(map(int,input().rstrip().split())) c list(map(int,input().rstrip().split())) list map(int,input().rstrip().split()) map int input().rstrip().split() ().rstrip().split ().rstrip() ().rstrip () input rstrip split def find(start,now,up,max,sum,count,flag):
    if start==now:
        flag+=1
    if start==now and flag==2:
        return [max,sum,count]
    elif count==up:
        return [max,sum,count]
    else:
        count+=1
        sum+=c[p[now-1]-1]
        if max<sum:
            max=sum
        return find(start,p[now-1],up,max,sum,count,flag) find if start==now:
        flag+=1
     start==now start now flag+=1 flag 1 if start==now and flag==2:
        return [max,sum,count]
    elif count==up:
        return [max,sum,count]
    else:
        count+=1
        sum+=c[p[now-1]-1]
        if max<sum:
            max=sum
        return find(start,p[now-1],up,max,sum,count,flag) start==now and flag==2 start==now start now flag==2 flag 2 return [max,sum,count] [max,sum,count] max sum count elif count==up:
        return [max,sum,count]
     count==up count up return [max,sum,count] [max,sum,count] max sum count count+=1 count 1 sum+=c[p[now-1]-1] sum c[p[now-1]-1] c p[now-1]-1 p[now-1] p now-1 now 1 1 if max<sum:
            max=sum
         max<sum max sum max=sum max sum return find(start,p[now-1],up,max,sum,count,flag) find(start,p[now-1],up,max,sum,count,flag) find start p[now-1] p now-1 now 1 up max sum count flag start start now now up up max max sum sum count count flag flag result=-10000000000 result -10000000000 for i in range(1,n+1):
    m=find(i,i,n,c[p[i-1]-1],0,0,0)
    if m[2]>=k:
        m=find(i,i,k,c[p[i-1]-1],0,0,0)
        if m[0]>result:
            result=m[0]
        ans=result
    elif m[1]<=0:
        if m[0]>result:
            result=m[0]
        ans=result
    else:
        w=k%m[2]
        if w==0:
            w=m[2]
        spe=find(i,i,w,c[p[i-1]-1],0,0,0)[0]
        if m[1]+spe>m[0] and m[1]*(k-w)//m[2]+spe>result:
            result=m[1]*(k-w)//m[2]+spe
        elif m[1]+spe<=m[0] and m[1]*((k-w)//m[2]-1)+m[0]>result:
            result=m[1]*((k-w)//m[2]-1)+m[0]
        ans=result i range(1,n+1) range 1 n+1 n 1 m=find(i,i,n,c[p[i-1]-1],0,0,0) m find(i,i,n,c[p[i-1]-1],0,0,0) find i i n c[p[i-1]-1] c p[i-1]-1 p[i-1] p i-1 i 1 1 0 0 0 if m[2]>=k:
        m=find(i,i,k,c[p[i-1]-1],0,0,0)
        if m[0]>result:
            result=m[0]
        ans=result
    elif m[1]<=0:
        if m[0]>result:
            result=m[0]
        ans=result
    else:
        w=k%m[2]
        if w==0:
            w=m[2]
        spe=find(i,i,w,c[p[i-1]-1],0,0,0)[0]
        if m[1]+spe>m[0] and m[1]*(k-w)//m[2]+spe>result:
            result=m[1]*(k-w)//m[2]+spe
        elif m[1]+spe<=m[0] and m[1]*((k-w)//m[2]-1)+m[0]>result:
            result=m[1]*((k-w)//m[2]-1)+m[0]
        ans=result m[2]>=k m[2] m 2 k m=find(i,i,k,c[p[i-1]-1],0,0,0) m find(i,i,k,c[p[i-1]-1],0,0,0) find i i k c[p[i-1]-1] c p[i-1]-1 p[i-1] p i-1 i 1 1 0 0 0 if m[0]>result:
            result=m[0]
         m[0]>result m[0] m 0 result result=m[0] result m[0] m 0 ans=result ans result elif m[1]<=0:
        if m[0]>result:
            result=m[0]
        ans=result
     m[1]<=0 m[1] m 1 0 if m[0]>result:
            result=m[0]
         m[0]>result m[0] m 0 result result=m[0] result m[0] m 0 ans=result ans result w=k%m[2] w k%m[2] k m[2] m 2 if w==0:
            w=m[2]
         w==0 w 0 w=m[2] w m[2] m 2 spe=find(i,i,w,c[p[i-1]-1],0,0,0)[0] spe find(i,i,w,c[p[i-1]-1],0,0,0)[0] (i,i,w,c[p[i-1]-1],0,0,0) find i i w c[p[i-1]-1] c p[i-1]-1 p[i-1] p i-1 i 1 1 0 0 0 0 if m[1]+spe>m[0] and m[1]*(k-w)//m[2]+spe>result:
            result=m[1]*(k-w)//m[2]+spe
        elif m[1]+spe<=m[0] and m[1]*((k-w)//m[2]-1)+m[0]>result:
            result=m[1]*((k-w)//m[2]-1)+m[0]
         m[1]+spe>m[0] and m[1]*(k-w)//m[2]+spe>result m[1]+spe>m[0] m[1]+spe m[1] m 1 spe m[0] m 0 m[1]*(k-w)//m[2]+spe>result m[1]*(k-w)//m[2]+spe m[1]*(k-w)//m[2] m[1]*(k-w) m[1] m 1 k-w k w m[2] m 2 spe result result=m[1]*(k-w)//m[2]+spe result m[1]*(k-w)//m[2]+spe m[1]*(k-w)//m[2] m[1]*(k-w) m[1] m 1 k-w k w m[2] m 2 spe elif m[1]+spe<=m[0] and m[1]*((k-w)//m[2]-1)+m[0]>result:
            result=m[1]*((k-w)//m[2]-1)+m[0]
         m[1]+spe<=m[0] and m[1]*((k-w)//m[2]-1)+m[0]>result m[1]+spe<=m[0] m[1]+spe m[1] m 1 spe m[0] m 0 m[1]*((k-w)//m[2]-1)+m[0]>result m[1]*((k-w)//m[2]-1)+m[0] m[1]*((k-w)//m[2]-1) m[1] m 1 (k-w)//m[2]-1 (k-w)//m[2] k-w k w m[2] m 2 1 m[0] m 0 result result=m[1]*((k-w)//m[2]-1)+m[0] result m[1]*((k-w)//m[2]-1)+m[0] m[1]*((k-w)//m[2]-1) m[1] m 1 (k-w)//m[2]-1 (k-w)//m[2] k-w k w m[2] m 2 1 m[0] m 0 ans=result ans result print(ans) print ans result=m[0] m[0] result spe=find(i,i,w,c[p[i-1]-1],0,0,0)[0] find(i,i,w,c[p[i-1]-1],0,0,0)[0] spe ans=result result ans result=m[1]*((k-w)//m[2]-1)+m[0] m[1]*((k-w)//m[2]-1)+m[0] result k=map(int,input().rstrip().split()) map(int,input().rstrip().split()) k ans=result result ans c=list(map(int,input().rstrip().split())) list(map(int,input().rstrip().split())) c p=list(map(int,input().rstrip().split())) list(map(int,input().rstrip().split())) p result=m[0] m[0] result w=k%m[2] k%m[2] w m=find(i,i,n,c[p[i-1]-1],0,0,0) find(i,i,n,c[p[i-1]-1],0,0,0) m result=m[1]*(k-w)//m[2]+spe m[1]*(k-w)//m[2]+spe result m=find(i,i,k,c[p[i-1]-1],0,0,0) find(i,i,k,c[p[i-1]-1],0,0,0) m result=-10000000000 -10000000000 result n,k=map(int,input().rstrip().split()) map(int,input().rstrip().split()) n w=m[2] m[2] w def find(start,now,up,max,sum,count,flag):
    if start==now:
        flag+=1
    if start==now and flag==2:
        return [max,sum,count]
    elif count==up:
        return [max,sum,count]
    else:
        count+=1
        sum+=c[p[now-1]-1]
        if max<sum:
            max=sum
        return find(start,p[now-1],up,max,sum,count,flag) def find(start,now,up,max,sum,count,flag):
    if start==now:
        flag+=1
    if start==now and flag==2:
        return [max,sum,count]
    elif count==up:
        return [max,sum,count]
    else:
        count+=1
        sum+=c[p[now-1]-1]
        if max<sum:
            max=sum
        return find(start,p[now-1],up,max,sum,count,flag) find ans=result result ans
import sys
import resource
sys.setrecursionlimit(10000)
n,k=map(int,input().rstrip().split())
p=list(map(int,input().rstrip().split()))
c=list(map(int,input().rstrip().split()))
def find(start,now,up,max,sum,count,flag):
    if start==now:
        flag+=1
    if start==now and flag==2:
        return [max,sum,count]
    elif count==up:
        return [max,sum,count]
    else:
        count+=1
        sum+=c[p[now-1]-1]
        if max<sum:
            max=sum
        return find(start,p[now-1],up,max,sum,count,flag)
result=-10000000000
for i in range(1,n+1):
    m=find(i,i,n,c[p[i-1]-1],0,0,0)
    if m[2]>=k:
        m=find(i,i,k,c[p[i-1]-1],0,0,0)
        if m[0]>result:
            result=m[0]
        ans=result
    elif m[1]<=0:
        if m[0]>result:
            result=m[0]
        ans=result
    else:
        w=k%m[2]
        if w==0:
            w=m[2]
        spe=find(i,i,w,c[p[i-1]-1],0,0,0)[0]
        if m[1]+spe>m[0] and m[1]*(k-w)//m[2]+spe>result:
            result=m[1]*(k-w)//m[2]+spe
        elif m[1]+spe<=m[0] and m[1]*((k-w)//m[2]-1)+m[0]>result:
            result=m[1]*((k-w)//m[2]-1)+m[0]
        ans=result
print(ans)