n,k=map(int,input().split())
p=list(map(int,input().split()))
c=list(map(int,input().split()))
for i in range(n):
    p[i]-=1
ans=-10**18

for si in range(n):
    score=[]
    x=si
    while(1):
        x=p[x]
        score.append(c[x])
        if(x==si):
            break
    l=len(score)
    accum=[score[0]]
    for i in range(1,l):
        accum.append(score[i]+accum[i-1])
    total=sum(score)
    z=k//l
    if(total>0):
        for i in range(l):
            if(((z-1)*l+i)>=0):
                tmp=accum[i]+total*(z-1)
                ans=max(ans,tmp)
            if((z*l+i)<k):
                tmp=accum[i]+total*z
                ans=max(ans,tmp)
    else:
        for i in range(l):
            if(i<k):
                tmp=accum[i]
                ans=max(ans,tmp)
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 for i in range(n):
    p[i]-=1 i range(n) range n p[i]-=1 p[i] p i 1 ans=-10**18 ans -10**18 -10 18 for si in range(n):
    score=[]
    x=si
    while(1):
        x=p[x]
        score.append(c[x])
        if(x==si):
            break
    l=len(score)
    accum=[score[0]]
    for i in range(1,l):
        accum.append(score[i]+accum[i-1])
    total=sum(score)
    z=k//l
    if(total>0):
        for i in range(l):
            if(((z-1)*l+i)>=0):
                tmp=accum[i]+total*(z-1)
                ans=max(ans,tmp)
            if((z*l+i)<k):
                tmp=accum[i]+total*z
                ans=max(ans,tmp)
    else:
        for i in range(l):
            if(i<k):
                tmp=accum[i]
                ans=max(ans,tmp) si range(n) range n score=[] score [] x=si x si while(1):
        x=p[x]
        score.append(c[x])
        if(x==si):
            break
     1 x=p[x] x p[x] p x score.append(c[x]) score.append score append c[x] c x if(x==si):
            break
     x==si x si break l=len(score) l len(score) len score accum=[score[0]] accum [score[0]] score[0] score 0 for i in range(1,l):
        accum.append(score[i]+accum[i-1])
     i range(1,l) range 1 l accum.append(score[i]+accum[i-1]) accum.append accum append score[i]+accum[i-1] score[i] score i accum[i-1] accum i-1 i 1 total=sum(score) total sum(score) sum score z=k//l z k//l k l if(total>0):
        for i in range(l):
            if(((z-1)*l+i)>=0):
                tmp=accum[i]+total*(z-1)
                ans=max(ans,tmp)
            if((z*l+i)<k):
                tmp=accum[i]+total*z
                ans=max(ans,tmp)
    else:
        for i in range(l):
            if(i<k):
                tmp=accum[i]
                ans=max(ans,tmp) total>0 total 0 for i in range(l):
            if(((z-1)*l+i)>=0):
                tmp=accum[i]+total*(z-1)
                ans=max(ans,tmp)
            if((z*l+i)<k):
                tmp=accum[i]+total*z
                ans=max(ans,tmp)
     i range(l) range l if(((z-1)*l+i)>=0):
                tmp=accum[i]+total*(z-1)
                ans=max(ans,tmp)
             ((z-1)*l+i)>=0 (z-1)*l+i (z-1)*l z-1 z 1 l i 0 tmp=accum[i]+total*(z-1) tmp accum[i]+total*(z-1) accum[i] accum i total*(z-1) total z-1 z 1 ans=max(ans,tmp) ans max(ans,tmp) max ans tmp if((z*l+i)<k):
                tmp=accum[i]+total*z
                ans=max(ans,tmp)
     (z*l+i)<k z*l+i z*l z l i k tmp=accum[i]+total*z tmp accum[i]+total*z accum[i] accum i total*z total z ans=max(ans,tmp) ans max(ans,tmp) max ans tmp for i in range(l):
            if(i<k):
                tmp=accum[i]
                ans=max(ans,tmp) i range(l) range l if(i<k):
                tmp=accum[i]
                ans=max(ans,tmp) i<k i k tmp=accum[i] tmp accum[i] accum i ans=max(ans,tmp) ans max(ans,tmp) max ans tmp print(ans) print ans tmp=accum[i]+total*z accum[i]+total*z tmp x=p[x] p[x] x ans=-10**18 -10**18 ans l=len(score) len(score) l total=sum(score) sum(score) total c=list(map(int,input().split())) list(map(int,input().split())) c x=si si x tmp=accum[i] accum[i] tmp n,k=map(int,input().split()) map(int,input().split()) n ans=max(ans,tmp) max(ans,tmp) ans ans=max(ans,tmp) max(ans,tmp) ans k=map(int,input().split()) map(int,input().split()) k accum=[score[0]] [score[0]] accum ans=max(ans,tmp) max(ans,tmp) ans z=k//l k//l z score=[] [] score tmp=accum[i]+total*(z-1) accum[i]+total*(z-1) tmp p=list(map(int,input().split())) list(map(int,input().split())) p
n,k=map(int,input().split())
p=list(map(int,input().split()))
c=list(map(int,input().split()))
for i in range(n):
    p[i]-=1
ans=-10**18

for si in range(n):
    score=[]
    x=si
    while(1):
        x=p[x]
        score.append(c[x])
        if(x==si):
            break
    l=len(score)
    accum=[score[0]]
    for i in range(1,l):
        accum.append(score[i]+accum[i-1])
    total=sum(score)
    z=k//l
    if(total>0):
        for i in range(l):
            if(((z-1)*l+i)>=0):
                tmp=accum[i]+total*(z-1)
                ans=max(ans,tmp)
            if((z*l+i)<k):
                tmp=accum[i]+total*z
                ans=max(ans,tmp)
    else:
        for i in range(l):
            if(i<k):
                tmp=accum[i]
                ans=max(ans,tmp)
print(ans)