n,k=map(int,input().split())
p=[int(i)-1for i in input().split()]
c=list(map(int,input().split()))
ans=-10**9
for i in range(n):
    start=i
    cycle=[0]
    for _ in range(n):
        i=p[i]
        cycle+=[cycle[-1]+c[i]]
        if i==start:break
    S,L=cycle[-1],len(cycle)-1
    if S<=0:ans=max(ans,max(cycle[1:k+1]))
    else:
        if k>L:ans=max(ans,(k//L-1)*S+max(max(cycle),max(cycle[:k%L+1])+S)) 
        else:ans=max(ans,max(cycle[:k+1]))
print(ans) n,k=map(int,input().split()) n map(int,input().split()) map int input().split() ().split () input split k int(i)-1for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i)-1 int(i) int i 1 p=[int(i)-1for i in input().split()] p [int(i)-1for i in 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 ans=-10**9 ans -10**9 -10 9 for i in range(n):
    start=i
    cycle=[0]
    for _ in range(n):
        i=p[i]
        cycle+=[cycle[-1]+c[i]]
        if i==start:break
    S,L=cycle[-1],len(cycle)-1
    if S<=0:ans=max(ans,max(cycle[1:k+1]))
    else:
        if k>L:ans=max(ans,(k//L-1)*S+max(max(cycle),max(cycle[:k%L+1])+S)) 
        else:ans=max(ans,max(cycle[:k+1])) i range(n) range n start=i start i cycle=[0] cycle [0] 0 for _ in range(n):
        i=p[i]
        cycle+=[cycle[-1]+c[i]]
        if i==start:break
     _ range(n) range n i=p[i] i p[i] p i cycle+=[cycle[-1]+c[i]] cycle [cycle[-1]+c[i]] cycle[-1]+c[i] cycle[-1] cycle -1 c[i] c i if i==start:break
     i==start i start S,L=cycle[-1],len(cycle)-1 S cycle[-1] cycle -1 L len(cycle)-1 len(cycle) len cycle 1 if S<=0:ans=max(ans,max(cycle[1:k+1]))
    else:
        if k>L:ans=max(ans,(k//L-1)*S+max(max(cycle),max(cycle[:k%L+1])+S)) 
        else:ans=max(ans,max(cycle[:k+1])) S<=0 S 0 if k>L:ans=max(ans,(k//L-1)*S+max(max(cycle),max(cycle[:k%L+1])+S)) 
        else:ans=max(ans,max(cycle[:k+1])) k>L k L print(ans) print ans S,L=cycle[-1],len(cycle)-1 cycle[-1] S c=list(map(int,input().split())) list(map(int,input().split())) c ans=-10**9 -10**9 ans i=p[i] p[i] i n,k=map(int,input().split()) map(int,input().split()) n start=i i start cycle=[0] [0] cycle k=map(int,input().split()) map(int,input().split()) k L=cycle[-1],len(cycle)-1 len(cycle)-1 L p=[int(i)-1for i in input().split()] [int(i)-1for i in input().split()] p cycle+=[cycle[-1]+c[i]] [cycle[-1]+c[i]] cycle
n,k=map(int,input().split())
p=[int(i)-1for i in input().split()]
c=list(map(int,input().split()))
ans=-10**9
for i in range(n):
    start=i
    cycle=[0]
    for _ in range(n):
        i=p[i]
        cycle+=[cycle[-1]+c[i]]
        if i==start:break
    S,L=cycle[-1],len(cycle)-1
    if S<=0:ans=max(ans,max(cycle[1:k+1]))
    else:
        if k>L:ans=max(ans,(k//L-1)*S+max(max(cycle),max(cycle[:k%L+1])+S)) 
        else:ans=max(ans,max(cycle[:k+1]))
print(ans)