INF=10**20

N,K=map(int,input().split())

array=list(map(int,input().split()))
P=[]
C=list(map(int,input().split()))
for i in array:
    P.append(i-1)

ans=-1*INF

for i in range(N):
    v=i
    cycle_sum=0
    cycle_cnt=0

    while(True):
        cycle_cnt+=1
        cycle_sum+=C[v]
        v=P[v]
        if v==i:
            break

    path=0
    cnt=0

    while(True):
        cnt+=1
        path+=C[v]

        if cnt>K:
            break

        num=(K-cnt)//cycle_cnt
        score=path+max(0,cycle_sum)*num
        ans=max(ans,score)

        v=P[v]
        if v==i:
            break

print(ans) INF=10**20 INF 10**20 10 20 N,K=map(int,input().split()) N map(int,input().split()) map int input().split() ().split () input split K array=list(map(int,input().split())) array list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split P=[] P [] C=list(map(int,input().split())) C list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split for i in array:
    P.append(i-1) i array P.append(i-1) P.append P append i-1 i 1 ans=-1*INF ans -1*INF -1 INF for i in range(N):
    v=i
    cycle_sum=0
    cycle_cnt=0

    while(True):
        cycle_cnt+=1
        cycle_sum+=C[v]
        v=P[v]
        if v==i:
            break

    path=0
    cnt=0

    while(True):
        cnt+=1
        path+=C[v]

        if cnt>K:
            break

        num=(K-cnt)//cycle_cnt
        score=path+max(0,cycle_sum)*num
        ans=max(ans,score)

        v=P[v]
        if v==i:
            break i range(N) range N v=i v i cycle_sum=0 cycle_sum 0 cycle_cnt=0 cycle_cnt 0 while(True):
        cycle_cnt+=1
        cycle_sum+=C[v]
        v=P[v]
        if v==i:
            break

     True cycle_cnt+=1 cycle_cnt 1 cycle_sum+=C[v] cycle_sum C[v] C v v=P[v] v P[v] P v if v==i:
            break

     v==i v i break path=0 path 0 cnt=0 cnt 0 while(True):
        cnt+=1
        path+=C[v]

        if cnt>K:
            break

        num=(K-cnt)//cycle_cnt
        score=path+max(0,cycle_sum)*num
        ans=max(ans,score)

        v=P[v]
        if v==i:
            break True cnt+=1 cnt 1 path+=C[v] path C[v] C v if cnt>K:
            break

         cnt>K cnt K break num=(K-cnt)//cycle_cnt num (K-cnt)//cycle_cnt K-cnt K cnt cycle_cnt score=path+max(0,cycle_sum)*num score path+max(0,cycle_sum)*num path max(0,cycle_sum)*num max(0,cycle_sum) max 0 cycle_sum num ans=max(ans,score) ans max(ans,score) max ans score v=P[v] v P[v] P v if v==i:
            break v==i v i break print(ans) print ans K=map(int,input().split()) map(int,input().split()) K C=list(map(int,input().split())) list(map(int,input().split())) C v=P[v] P[v] v cycle_sum=0 0 cycle_sum ans=-1*INF -1*INF ans INF=10**20 10**20 INF P=[] [] P score=path+max(0,cycle_sum)*num path+max(0,cycle_sum)*num score v=P[v] P[v] v v=i i v num=(K-cnt)//cycle_cnt (K-cnt)//cycle_cnt num cycle_cnt+=1 1 cycle_cnt cnt=0 0 cnt array=list(map(int,input().split())) list(map(int,input().split())) array N,K=map(int,input().split()) map(int,input().split()) N path=0 0 path cnt+=1 1 cnt path+=C[v] C[v] path cycle_cnt=0 0 cycle_cnt ans=max(ans,score) max(ans,score) ans cycle_sum+=C[v] C[v] cycle_sum
INF=10**20

N,K=map(int,input().split())

array=list(map(int,input().split()))
P=[]
C=list(map(int,input().split()))
for i in array:
    P.append(i-1)

ans=-1*INF

for i in range(N):
    v=i
    cycle_sum=0
    cycle_cnt=0

    while(True):
        cycle_cnt+=1
        cycle_sum+=C[v]
        v=P[v]
        if v==i:
            break

    path=0
    cnt=0

    while(True):
        cnt+=1
        path+=C[v]

        if cnt>K:
            break

        num=(K-cnt)//cycle_cnt
        score=path+max(0,cycle_sum)*num
        ans=max(ans,score)

        v=P[v]
        if v==i:
            break

print(ans)