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

ans=-10**20
for i in range(n):
    loop=1
    j=p[i]-1
    acm=[c[j]]
    while j!=i:
        j=p[j]-1
        acm.append(acm[-1]+c[j])
        loop+=1
    
    if acm[-1]>0:
        if k<=loop:
            cur=max(acm[:k])
        else:
            cur=(k//loop)*acm[-1]
            if k%loop>0:
                cur=max(cur,cur+max(acm[:k%loop]))
            else:
                cur-=acm[-1]
                cur+=max(acm)
    else:
        cur=max(acm[:min(k,loop)])
    
    ans=max(cur,ans)
    
print(ans) n,k=map(int,input().split()) n map(int,input().split()) map int input().split() ().split () input split k *p,=map(int,input().split()) *p p map(int,input().split()) map int input().split() ().split () input split *c,=map(int,input().split()) *c c map(int,input().split()) map int input().split() ().split () input split ans=-10**20 ans -10**20 -10 20 for i in range(n):
    loop=1
    j=p[i]-1
    acm=[c[j]]
    while j!=i:
        j=p[j]-1
        acm.append(acm[-1]+c[j])
        loop+=1
    
    if acm[-1]>0:
        if k<=loop:
            cur=max(acm[:k])
        else:
            cur=(k//loop)*acm[-1]
            if k%loop>0:
                cur=max(cur,cur+max(acm[:k%loop]))
            else:
                cur-=acm[-1]
                cur+=max(acm)
    else:
        cur=max(acm[:min(k,loop)])
    
    ans=max(cur,ans)
     i range(n) range n loop=1 loop 1 j=p[i]-1 j p[i]-1 p[i] p i 1 acm=[c[j]] acm [c[j]] c[j] c j while j!=i:
        j=p[j]-1
        acm.append(acm[-1]+c[j])
        loop+=1
    
     j!=i j i j=p[j]-1 j p[j]-1 p[j] p j 1 acm.append(acm[-1]+c[j]) acm.append acm append acm[-1]+c[j] acm[-1] acm -1 c[j] c j loop+=1 loop 1 if acm[-1]>0:
        if k<=loop:
            cur=max(acm[:k])
        else:
            cur=(k//loop)*acm[-1]
            if k%loop>0:
                cur=max(cur,cur+max(acm[:k%loop]))
            else:
                cur-=acm[-1]
                cur+=max(acm)
    else:
        cur=max(acm[:min(k,loop)])
    
     acm[-1]>0 acm[-1] acm -1 0 if k<=loop:
            cur=max(acm[:k])
        else:
            cur=(k//loop)*acm[-1]
            if k%loop>0:
                cur=max(cur,cur+max(acm[:k%loop]))
            else:
                cur-=acm[-1]
                cur+=max(acm)
     k<=loop k loop cur=max(acm[:k]) cur max(acm[:k]) max acm[:k] acm :k k cur=(k//loop)*acm[-1] cur (k//loop)*acm[-1] k//loop k loop acm[-1] acm -1 if k%loop>0:
                cur=max(cur,cur+max(acm[:k%loop]))
            else:
                cur-=acm[-1]
                cur+=max(acm)
     k%loop>0 k%loop k loop 0 cur=max(cur,cur+max(acm[:k%loop])) cur max(cur,cur+max(acm[:k%loop])) max cur cur+max(acm[:k%loop]) cur max(acm[:k%loop]) max acm[:k%loop] acm :k%loop k%loop k loop cur-=acm[-1] cur acm[-1] acm -1 cur+=max(acm) cur max(acm) max acm cur=max(acm[:min(k,loop)]) cur max(acm[:min(k,loop)]) max acm[:min(k,loop)] acm :min(k,loop) min(k,loop) min k loop ans=max(cur,ans) ans max(cur,ans) max cur ans print(ans) print ans k=map(int,input().split()) map(int,input().split()) k cur=max(cur,cur+max(acm[:k%loop])) max(cur,cur+max(acm[:k%loop])) cur j=p[j]-1 p[j]-1 j cur-=acm[-1] acm[-1] cur cur+=max(acm) max(acm) cur ans=max(cur,ans) max(cur,ans) ans cur=max(acm[:min(k,loop)]) max(acm[:min(k,loop)]) cur loop+=1 1 loop cur=max(acm[:k]) max(acm[:k]) cur ans=-10**20 -10**20 ans j=p[i]-1 p[i]-1 j acm=[c[j]] [c[j]] acm loop=1 1 loop n,k=map(int,input().split()) map(int,input().split()) n cur=(k//loop)*acm[-1] (k//loop)*acm[-1] cur
n,k=map(int,input().split())
*p,=map(int,input().split())
*c,=map(int,input().split())

ans=-10**20
for i in range(n):
    loop=1
    j=p[i]-1
    acm=[c[j]]
    while j!=i:
        j=p[j]-1
        acm.append(acm[-1]+c[j])
        loop+=1
    
    if acm[-1]>0:
        if k<=loop:
            cur=max(acm[:k])
        else:
            cur=(k//loop)*acm[-1]
            if k%loop>0:
                cur=max(cur,cur+max(acm[:k%loop]))
            else:
                cur-=acm[-1]
                cur+=max(acm)
    else:
        cur=max(acm[:min(k,loop)])
    
    ans=max(cur,ans)
    
print(ans)