N,K=map(int,input().split())
P=[0]+list(map(int,input().split()))
C=[0]+list(map(int,input().split()))
D=[0 for i in range(N+1)]
D[0]=5

ans=-10**20
for i in range(1,N+1):
    if D[i]==0:
        D[i]=1
        p=P[i]
        cnt=-10**20
        L=[0]
        l=[i]
        for j in range(10**7):
            if D[p]==5:
                break
            else:
                D[p]+=1
                L.append(L[-1]+C[p])
                l.append(p)
                p=P[p]
        #print(L)
        #print(l)
        loop_len=len(L)//5
        loop_cnt=L[loop_len]
        #print(loop_len)
        #print(loop_cnt)
        R=K//loop_len
        M=K%loop_len
        loop_max=-10**20
        if R>=2 and loop_cnt>0:
            cnt+=(R-2)*loop_cnt
            for x in range(loop_len):
                for y in range(1,M+2*loop_len+1):
                    loop_max=max(loop_max,L[y+x]-L[x])
            cnt=max(cnt,loop_cnt*(R-2)+loop_max)
            #print(L,cnt)
            ans=max(ans,cnt)
        else:
            cnt=-10**20
            for x in range(loop_len):
                for y in range(1,min(K,2*loop_len)+1):
                    cnt=max(cnt,L[y+x]-L[x])
                    #print(L,cnt,x,y,R)
            ans=max(ans,cnt)
#print(D)
print(ans) N,K=map(int,input().split()) N map(int,input().split()) map int input().split() ().split () input split K P=[0]+list(map(int,input().split())) P [0]+list(map(int,input().split())) [0] 0 list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split C=[0]+list(map(int,input().split())) C [0]+list(map(int,input().split())) [0] 0 list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split 0 for i in range(N+1) for i in range(N+1) i range(N+1) range N+1 N 1 for i in range(N+1) 0 D=[0 for i in range(N+1)] D [0 for i in range(N+1)] D[0]=5 D[0] D 0 5 ans=-10**20 ans -10**20 -10 20 for i in range(1,N+1):
    if D[i]==0:
        D[i]=1
        p=P[i]
        cnt=-10**20
        L=[0]
        l=[i]
        for j in range(10**7):
            if D[p]==5:
                break
            else:
                D[p]+=1
                L.append(L[-1]+C[p])
                l.append(p)
                p=P[p]
        #print(L)
        #print(l)
        loop_len=len(L)//5
        loop_cnt=L[loop_len]
        #print(loop_len)
        #print(loop_cnt)
        R=K//loop_len
        M=K%loop_len
        loop_max=-10**20
        if R>=2 and loop_cnt>0:
            cnt+=(R-2)*loop_cnt
            for x in range(loop_len):
                for y in range(1,M+2*loop_len+1):
                    loop_max=max(loop_max,L[y+x]-L[x])
            cnt=max(cnt,loop_cnt*(R-2)+loop_max)
            #print(L,cnt)
            ans=max(ans,cnt)
        else:
            cnt=-10**20
            for x in range(loop_len):
                for y in range(1,min(K,2*loop_len)+1):
                    cnt=max(cnt,L[y+x]-L[x])
                    #print(L,cnt,x,y,R)
            ans=max(ans,cnt)
#print(D) i range(1,N+1) range 1 N+1 N 1 if D[i]==0:
        D[i]=1
        p=P[i]
        cnt=-10**20
        L=[0]
        l=[i]
        for j in range(10**7):
            if D[p]==5:
                break
            else:
                D[p]+=1
                L.append(L[-1]+C[p])
                l.append(p)
                p=P[p]
        #print(L)
        #print(l)
        loop_len=len(L)//5
        loop_cnt=L[loop_len]
        #print(loop_len)
        #print(loop_cnt)
        R=K//loop_len
        M=K%loop_len
        loop_max=-10**20
        if R>=2 and loop_cnt>0:
            cnt+=(R-2)*loop_cnt
            for x in range(loop_len):
                for y in range(1,M+2*loop_len+1):
                    loop_max=max(loop_max,L[y+x]-L[x])
            cnt=max(cnt,loop_cnt*(R-2)+loop_max)
            #print(L,cnt)
            ans=max(ans,cnt)
        else:
            cnt=-10**20
            for x in range(loop_len):
                for y in range(1,min(K,2*loop_len)+1):
                    cnt=max(cnt,L[y+x]-L[x])
                    #print(L,cnt,x,y,R)
            ans=max(ans,cnt)
#print(D) D[i]==0 D[i] D i 0 D[i]=1 D[i] D i 1 p=P[i] p P[i] P i cnt=-10**20 cnt -10**20 -10 20 L=[0] L [0] 0 l=[i] l [i] i for j in range(10**7):
            if D[p]==5:
                break
            else:
                D[p]+=1
                L.append(L[-1]+C[p])
                l.append(p)
                p=P[p]
        #print(L)
        #print(l)
         j range(10**7) range 10**7 10 7 if D[p]==5:
                break
            else:
                D[p]+=1
                L.append(L[-1]+C[p])
                l.append(p)
                p=P[p]
        #print(L)
        #print(l)
         D[p]==5 D[p] D p 5 break D[p]+=1 D[p] D p 1 L.append(L[-1]+C[p]) L.append L append L[-1]+C[p] L[-1] L -1 C[p] C p l.append(p) l.append l append p p=P[p] p P[p] P p loop_len=len(L)//5 loop_len len(L)//5 len(L) len L 5 loop_cnt=L[loop_len] loop_cnt L[loop_len] L loop_len R=K//loop_len R K//loop_len K loop_len M=K%loop_len M K%loop_len K loop_len loop_max=-10**20 loop_max -10**20 -10 20 if R>=2 and loop_cnt>0:
            cnt+=(R-2)*loop_cnt
            for x in range(loop_len):
                for y in range(1,M+2*loop_len+1):
                    loop_max=max(loop_max,L[y+x]-L[x])
            cnt=max(cnt,loop_cnt*(R-2)+loop_max)
            #print(L,cnt)
            ans=max(ans,cnt)
        else:
            cnt=-10**20
            for x in range(loop_len):
                for y in range(1,min(K,2*loop_len)+1):
                    cnt=max(cnt,L[y+x]-L[x])
                    #print(L,cnt,x,y,R)
            ans=max(ans,cnt)
#print(D) R>=2 and loop_cnt>0 R>=2 R 2 loop_cnt>0 loop_cnt 0 cnt+=(R-2)*loop_cnt cnt (R-2)*loop_cnt R-2 R 2 loop_cnt for x in range(loop_len):
                for y in range(1,M+2*loop_len+1):
                    loop_max=max(loop_max,L[y+x]-L[x])
             x range(loop_len) range loop_len for y in range(1,M+2*loop_len+1):
                    loop_max=max(loop_max,L[y+x]-L[x])
             y range(1,M+2*loop_len+1) range 1 M+2*loop_len+1 M+2*loop_len M 2*loop_len 2 loop_len 1 loop_max=max(loop_max,L[y+x]-L[x]) loop_max max(loop_max,L[y+x]-L[x]) max loop_max L[y+x]-L[x] L[y+x] L y+x y x L[x] L x cnt=max(cnt,loop_cnt*(R-2)+loop_max) cnt max(cnt,loop_cnt*(R-2)+loop_max) max cnt loop_cnt*(R-2)+loop_max loop_cnt*(R-2) loop_cnt R-2 R 2 loop_max ans=max(ans,cnt) ans max(ans,cnt) max ans cnt cnt=-10**20 cnt -10**20 -10 20 for x in range(loop_len):
                for y in range(1,min(K,2*loop_len)+1):
                    cnt=max(cnt,L[y+x]-L[x])
                    #print(L,cnt,x,y,R)
             x range(loop_len) range loop_len for y in range(1,min(K,2*loop_len)+1):
                    cnt=max(cnt,L[y+x]-L[x])
                    #print(L,cnt,x,y,R)
             y range(1,min(K,2*loop_len)+1) range 1 min(K,2*loop_len)+1 min(K,2*loop_len) min K 2*loop_len 2 loop_len 1 cnt=max(cnt,L[y+x]-L[x]) cnt max(cnt,L[y+x]-L[x]) max cnt L[y+x]-L[x] L[y+x] L y+x y x L[x] L x ans=max(ans,cnt) ans max(ans,cnt) max ans cnt print(ans) print ans N,K=map(int,input().split()) map(int,input().split()) N ans=max(ans,cnt) max(ans,cnt) ans P=[0]+list(map(int,input().split())) [0]+list(map(int,input().split())) P ans=max(ans,cnt) max(ans,cnt) ans cnt+=(R-2)*loop_cnt (R-2)*loop_cnt cnt L=[0] [0] L M=K%loop_len K%loop_len M loop_max=-10**20 -10**20 loop_max C=[0]+list(map(int,input().split())) [0]+list(map(int,input().split())) C p=P[i] P[i] p cnt=-10**20 -10**20 cnt loop_cnt=L[loop_len] L[loop_len] loop_cnt loop_max=max(loop_max,L[y+x]-L[x]) max(loop_max,L[y+x]-L[x]) loop_max cnt=max(cnt,loop_cnt*(R-2)+loop_max) max(cnt,loop_cnt*(R-2)+loop_max) cnt K=map(int,input().split()) map(int,input().split()) K cnt=max(cnt,L[y+x]-L[x]) max(cnt,L[y+x]-L[x]) cnt l=[i] [i] l p=P[p] P[p] p D=[0 for i in range(N+1)] [0 for i in range(N+1)] D loop_len=len(L)//5 len(L)//5 loop_len R=K//loop_len K//loop_len R ans=-10**20 -10**20 ans cnt=-10**20 -10**20 cnt
N,K=map(int,input().split())
P=[0]+list(map(int,input().split()))
C=[0]+list(map(int,input().split()))
D=[0 for i in range(N+1)]
D[0]=5

ans=-10**20
for i in range(1,N+1):
    if D[i]==0:
        D[i]=1
        p=P[i]
        cnt=-10**20
        L=[0]
        l=[i]
        for j in range(10**7):
            if D[p]==5:
                break
            else:
                D[p]+=1
                L.append(L[-1]+C[p])
                l.append(p)
                p=P[p]
        #print(L)
        #print(l)
        loop_len=len(L)//5
        loop_cnt=L[loop_len]
        #print(loop_len)
        #print(loop_cnt)
        R=K//loop_len
        M=K%loop_len
        loop_max=-10**20
        if R>=2 and loop_cnt>0:
            cnt+=(R-2)*loop_cnt
            for x in range(loop_len):
                for y in range(1,M+2*loop_len+1):
                    loop_max=max(loop_max,L[y+x]-L[x])
            cnt=max(cnt,loop_cnt*(R-2)+loop_max)
            #print(L,cnt)
            ans=max(ans,cnt)
        else:
            cnt=-10**20
            for x in range(loop_len):
                for y in range(1,min(K,2*loop_len)+1):
                    cnt=max(cnt,L[y+x]-L[x])
                    #print(L,cnt,x,y,R)
            ans=max(ans,cnt)
#print(D)
print(ans)