N,K=map(int,input().split())
P=list(map(int,input().split()))
C=list(map(int,input().split()))
ans=[]
for i in range(N):
    score=[]
    B=[]
    count=1
    nex=P[i]-1
    point=C[nex]
    B.append(C[nex])
    score.append(point)
    while count<=K:
        if nex==i:
            break
        nex=P[nex]-1
        point+=C[nex]
        score.append(point)
        B.append(C[nex])
        count+=1
    if count==K:
        ans.append(max(score))
    else:
        if point<=0:
            ans.append(max(score))
        else:
            a=K//count
            b=K%count
            maxpoint=point*(a-1)
            for j in range(b):
                maxpoint+=B[j]
            c=maxpoint
            for j in range(count):
                if b+j<count:
                    c+=B[b+j]
                else:
                    c+=B[b+j-count]
                maxpoint=max(maxpoint,c)
            ans.append(maxpoint)
print(max(ans)) N,K=map(int,input().split()) N map(int,input().split()) map int input().split() ().split () input split K P=list(map(int,input().split())) P list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () 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=[] ans [] for i in range(N):
    score=[]
    B=[]
    count=1
    nex=P[i]-1
    point=C[nex]
    B.append(C[nex])
    score.append(point)
    while count<=K:
        if nex==i:
            break
        nex=P[nex]-1
        point+=C[nex]
        score.append(point)
        B.append(C[nex])
        count+=1
    if count==K:
        ans.append(max(score))
    else:
        if point<=0:
            ans.append(max(score))
        else:
            a=K//count
            b=K%count
            maxpoint=point*(a-1)
            for j in range(b):
                maxpoint+=B[j]
            c=maxpoint
            for j in range(count):
                if b+j<count:
                    c+=B[b+j]
                else:
                    c+=B[b+j-count]
                maxpoint=max(maxpoint,c)
            ans.append(maxpoint) i range(N) range N score=[] score [] B=[] B [] count=1 count 1 nex=P[i]-1 nex P[i]-1 P[i] P i 1 point=C[nex] point C[nex] C nex B.append(C[nex]) B.append B append C[nex] C nex score.append(point) score.append score append point while count<=K:
        if nex==i:
            break
        nex=P[nex]-1
        point+=C[nex]
        score.append(point)
        B.append(C[nex])
        count+=1
     count<=K count K if nex==i:
            break
         nex==i nex i break nex=P[nex]-1 nex P[nex]-1 P[nex] P nex 1 point+=C[nex] point C[nex] C nex score.append(point) score.append score append point B.append(C[nex]) B.append B append C[nex] C nex count+=1 count 1 if count==K:
        ans.append(max(score))
    else:
        if point<=0:
            ans.append(max(score))
        else:
            a=K//count
            b=K%count
            maxpoint=point*(a-1)
            for j in range(b):
                maxpoint+=B[j]
            c=maxpoint
            for j in range(count):
                if b+j<count:
                    c+=B[b+j]
                else:
                    c+=B[b+j-count]
                maxpoint=max(maxpoint,c)
            ans.append(maxpoint) count==K count K ans.append(max(score)) ans.append ans append max(score) max score if point<=0:
            ans.append(max(score))
        else:
            a=K//count
            b=K%count
            maxpoint=point*(a-1)
            for j in range(b):
                maxpoint+=B[j]
            c=maxpoint
            for j in range(count):
                if b+j<count:
                    c+=B[b+j]
                else:
                    c+=B[b+j-count]
                maxpoint=max(maxpoint,c)
            ans.append(maxpoint) point<=0 point 0 ans.append(max(score)) ans.append ans append max(score) max score a=K//count a K//count K count b=K%count b K%count K count maxpoint=point*(a-1) maxpoint point*(a-1) point a-1 a 1 for j in range(b):
                maxpoint+=B[j]
             j range(b) range b maxpoint+=B[j] maxpoint B[j] B j c=maxpoint c maxpoint for j in range(count):
                if b+j<count:
                    c+=B[b+j]
                else:
                    c+=B[b+j-count]
                maxpoint=max(maxpoint,c)
             j range(count) range count if b+j<count:
                    c+=B[b+j]
                else:
                    c+=B[b+j-count]
                 b+j<count b+j b j count c+=B[b+j] c B[b+j] B b+j b j c+=B[b+j-count] c B[b+j-count] B b+j-count b+j b j count maxpoint=max(maxpoint,c) maxpoint max(maxpoint,c) max maxpoint c ans.append(maxpoint) ans.append ans append maxpoint print(max(ans)) print max(ans) max ans nex=P[i]-1 P[i]-1 nex point+=C[nex] C[nex] point N,K=map(int,input().split()) map(int,input().split()) N ans=[] [] ans maxpoint=point*(a-1) point*(a-1) maxpoint C=list(map(int,input().split())) list(map(int,input().split())) C c+=B[b+j-count] B[b+j-count] c score=[] [] score count+=1 1 count P=list(map(int,input().split())) list(map(int,input().split())) P count=1 1 count maxpoint=max(maxpoint,c) max(maxpoint,c) maxpoint point=C[nex] C[nex] point b=K%count K%count b c+=B[b+j] B[b+j] c K=map(int,input().split()) map(int,input().split()) K maxpoint+=B[j] B[j] maxpoint a=K//count K//count a c=maxpoint maxpoint c B=[] [] B nex=P[nex]-1 P[nex]-1 nex
N,K=map(int,input().split())
P=list(map(int,input().split()))
C=list(map(int,input().split()))
ans=[]
for i in range(N):
    score=[]
    B=[]
    count=1
    nex=P[i]-1
    point=C[nex]
    B.append(C[nex])
    score.append(point)
    while count<=K:
        if nex==i:
            break
        nex=P[nex]-1
        point+=C[nex]
        score.append(point)
        B.append(C[nex])
        count+=1
    if count==K:
        ans.append(max(score))
    else:
        if point<=0:
            ans.append(max(score))
        else:
            a=K//count
            b=K%count
            maxpoint=point*(a-1)
            for j in range(b):
                maxpoint+=B[j]
            c=maxpoint
            for j in range(count):
                if b+j<count:
                    c+=B[b+j]
                else:
                    c+=B[b+j-count]
                maxpoint=max(maxpoint,c)
            ans.append(maxpoint)
print(max(ans))