n,k= map(int, input().split())
p= list(map(int, input().split()))
c= list(map(int, input().split()))


ans=-float('inf')


x=[0]*n
for i in range(n):
    if x[i]==0:
        x[i]=1
        y=p[i]-1
        # 累積和を格納
        aa=[0,c[i]]
        # ２周させる
        life=2
        for j in range(2*n+2):
            x[y]=1
            if life>0:
                aa.append(aa[-1]+c[y])
                y=p[y]-1
                if y==i:
                    life-=1
            else:
                break
        # 各回数での取れるスコアの最大値
        x1=(len(aa)-1)//2

        a=[-float('inf')]*(x1+1)

        for ii in range(len(aa)-1):
            for j in range(ii+1,len(aa)):
                if j-ii<=x1:
                    a[j - ii] = max(a[j - ii], aa[j] - aa[ii])

        v=k//x1
        w=k%x1

        if v==0:
            ans=max(ans,max(a[:k+1]))

        elif v==1:

            ans = max(ans, max(a), v * aa[x1] + max(a[:w + 1]))
        else:
            ans=max(ans,max(a),v * aa[x1] + max(a[:w + 1]),(v-1)*aa[x1]+max(a))





print(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=-float('inf') ans -float('inf') float('inf') float 'inf' x=[0]*n x [0]*n [0] 0 n for i in range(n):
    if x[i]==0:
        x[i]=1
        y=p[i]-1
        # 累積和を格納
        aa=[0,c[i]]
        # ２周させる
        life=2
        for j in range(2*n+2):
            x[y]=1
            if life>0:
                aa.append(aa[-1]+c[y])
                y=p[y]-1
                if y==i:
                    life-=1
            else:
                break
        # 各回数での取れるスコアの最大値
        x1=(len(aa)-1)//2

        a=[-float('inf')]*(x1+1)

        for ii in range(len(aa)-1):
            for j in range(ii+1,len(aa)):
                if j-ii<=x1:
                    a[j - ii] = max(a[j - ii], aa[j] - aa[ii])

        v=k//x1
        w=k%x1

        if v==0:
            ans=max(ans,max(a[:k+1]))

        elif v==1:

            ans = max(ans, max(a), v * aa[x1] + max(a[:w + 1]))
        else:
            ans=max(ans,max(a),v * aa[x1] + max(a[:w + 1]),(v-1)*aa[x1]+max(a)) i range(n) range n if x[i]==0:
        x[i]=1
        y=p[i]-1
        # 累積和を格納
        aa=[0,c[i]]
        # ２周させる
        life=2
        for j in range(2*n+2):
            x[y]=1
            if life>0:
                aa.append(aa[-1]+c[y])
                y=p[y]-1
                if y==i:
                    life-=1
            else:
                break
        # 各回数での取れるスコアの最大値
        x1=(len(aa)-1)//2

        a=[-float('inf')]*(x1+1)

        for ii in range(len(aa)-1):
            for j in range(ii+1,len(aa)):
                if j-ii<=x1:
                    a[j - ii] = max(a[j - ii], aa[j] - aa[ii])

        v=k//x1
        w=k%x1

        if v==0:
            ans=max(ans,max(a[:k+1]))

        elif v==1:

            ans = max(ans, max(a), v * aa[x1] + max(a[:w + 1]))
        else:
            ans=max(ans,max(a),v * aa[x1] + max(a[:w + 1]),(v-1)*aa[x1]+max(a)) x[i]==0 x[i] x i 0 x[i]=1 x[i] x i 1 y=p[i]-1 y p[i]-1 p[i] p i 1 aa=[0,c[i]] aa [0,c[i]] 0 c[i] c i life=2 life 2 for j in range(2*n+2):
            x[y]=1
            if life>0:
                aa.append(aa[-1]+c[y])
                y=p[y]-1
                if y==i:
                    life-=1
            else:
                break
        # 各回数での取れるスコアの最大値
         j range(2*n+2) range 2*n+2 2*n 2 n 2 x[y]=1 x[y] x y 1 if life>0:
                aa.append(aa[-1]+c[y])
                y=p[y]-1
                if y==i:
                    life-=1
            else:
                break
        # 各回数での取れるスコアの最大値
         life>0 life 0 aa.append(aa[-1]+c[y]) aa.append aa append aa[-1]+c[y] aa[-1] aa -1 c[y] c y y=p[y]-1 y p[y]-1 p[y] p y 1 if y==i:
                    life-=1
             y==i y i life-=1 life 1 break x1=(len(aa)-1)//2 x1 (len(aa)-1)//2 len(aa)-1 len(aa) len aa 1 2 a=[-float('inf')]*(x1+1) a [-float('inf')]*(x1+1) [-float('inf')] -float('inf') float('inf') float 'inf' x1+1 x1 1 for ii in range(len(aa)-1):
            for j in range(ii+1,len(aa)):
                if j-ii<=x1:
                    a[j - ii] = max(a[j - ii], aa[j] - aa[ii])

         ii range(len(aa)-1) range len(aa)-1 len(aa) len aa 1 for j in range(ii+1,len(aa)):
                if j-ii<=x1:
                    a[j - ii] = max(a[j - ii], aa[j] - aa[ii])

         j range(ii+1,len(aa)) range ii+1 ii 1 len(aa) len aa if j-ii<=x1:
                    a[j - ii] = max(a[j - ii], aa[j] - aa[ii])

         j-ii<=x1 j-ii j ii x1 a[j - ii] = max(a[j - ii], aa[j] - aa[ii]) a[j - ii] a j - ii j ii max(a[j - ii], aa[j] - aa[ii]) max a[j - ii] a j - ii j ii aa[j] - aa[ii] aa[j] aa j aa[ii] aa ii v=k//x1 v k//x1 k x1 w=k%x1 w k%x1 k x1 if v==0:
            ans=max(ans,max(a[:k+1]))

        elif v==1:

            ans = max(ans, max(a), v * aa[x1] + max(a[:w + 1]))
        else:
            ans=max(ans,max(a),v * aa[x1] + max(a[:w + 1]),(v-1)*aa[x1]+max(a)) v==0 v 0 ans=max(ans,max(a[:k+1])) ans max(ans,max(a[:k+1])) max ans max(a[:k+1]) max a[:k+1] a :k+1 k+1 k 1 elif v==1:

            ans = max(ans, max(a), v * aa[x1] + max(a[:w + 1]))
         v==1 v 1 ans = max(ans, max(a), v * aa[x1] + max(a[:w + 1])) ans max(ans, max(a), v * aa[x1] + max(a[:w + 1])) max ans max(a) max a v * aa[x1] + max(a[:w + 1]) v * aa[x1] v aa[x1] aa x1 max(a[:w + 1]) max a[:w + 1] a :w + 1 w + 1 w 1 ans=max(ans,max(a),v * aa[x1] + max(a[:w + 1]),(v-1)*aa[x1]+max(a)) ans max(ans,max(a),v * aa[x1] + max(a[:w + 1]),(v-1)*aa[x1]+max(a)) max ans max(a) max a v * aa[x1] + max(a[:w + 1]) v * aa[x1] v aa[x1] aa x1 max(a[:w + 1]) max a[:w + 1] a :w + 1 w + 1 w 1 (v-1)*aa[x1]+max(a) (v-1)*aa[x1] v-1 v 1 aa[x1] aa x1 max(a) max a print(ans) print ans v=k//x1 k//x1 v n,k= map(int, input().split()) map(int, input().split()) n aa=[0,c[i]] [0,c[i]] aa life-=1 1 life k= map(int, input().split()) map(int, input().split()) k ans=max(ans,max(a[:k+1])) max(ans,max(a[:k+1])) ans life=2 2 life p= list(map(int, input().split())) list(map(int, input().split())) p x1=(len(aa)-1)//2 (len(aa)-1)//2 x1 x=[0]*n [0]*n x y=p[y]-1 p[y]-1 y y=p[i]-1 p[i]-1 y w=k%x1 k%x1 w ans=-float('inf') -float('inf') ans ans = max(ans, max(a), v * aa[x1] + max(a[:w + 1])) max(ans, max(a), v * aa[x1] + max(a[:w + 1])) ans a=[-float('inf')]*(x1+1) [-float('inf')]*(x1+1) a c= list(map(int, input().split())) list(map(int, input().split())) c ans=max(ans,max(a),v * aa[x1] + max(a[:w + 1]),(v-1)*aa[x1]+max(a)) max(ans,max(a),v * aa[x1] + max(a[:w + 1]),(v-1)*aa[x1]+max(a)) ans
n,k= map(int, input().split())
p= list(map(int, input().split()))
c= list(map(int, input().split()))


ans=-float('inf')


x=[0]*n
for i in range(n):
    if x[i]==0:
        x[i]=1
        y=p[i]-1
        # 累積和を格納
        aa=[0,c[i]]
        # ２周させる
        life=2
        for j in range(2*n+2):
            x[y]=1
            if life>0:
                aa.append(aa[-1]+c[y])
                y=p[y]-1
                if y==i:
                    life-=1
            else:
                break
        # 各回数での取れるスコアの最大値
        x1=(len(aa)-1)//2

        a=[-float('inf')]*(x1+1)

        for ii in range(len(aa)-1):
            for j in range(ii+1,len(aa)):
                if j-ii<=x1:
                    a[j - ii] = max(a[j - ii], aa[j] - aa[ii])

        v=k//x1
        w=k%x1

        if v==0:
            ans=max(ans,max(a[:k+1]))

        elif v==1:

            ans = max(ans, max(a), v * aa[x1] + max(a[:w + 1]))
        else:
            ans=max(ans,max(a),v * aa[x1] + max(a[:w + 1]),(v-1)*aa[x1]+max(a))





print(ans)