n,k=map(int,input().split())
p=list(map(int,input().split()))
c=list(map(int,input().split()))
ans=-10**18
flag=dict()
for i in range(n):
    if i in flag:
        continue
    cur=i
    accum=[]
    flag[cur]=0
    s=0
    while flag[cur]<2:
        cur=p[cur]-1
        s+=c[cur]
        accum.append(s)
        if cur not in flag:
            flag[cur]=0
        else:
            flag[cur]+=1
    l=len(accum)//2
    loop = accum[l-1]
    accum=[0]+accum
    for x in range(1,l+1):
        if loop<=0 or k//l<1:
            ans=max(ans,max(accum[x:x+min(k,l)])-accum[x-1])
        else:
            a=max(accum[x:x+l-1])-accum[x-1]
            d=accum[x+l-1]-accum[x-1]
            if k%l!=0:
                b=max(accum[x:x+k%l])-accum[x-1]
                ans=max(ans,max(k//l*d,k//l*d+b,(k//l-1)*d+a))
            else:
                b=max(accum[x:x+l-1])-accum[x-1]
                ans=max(ans,max(k//l*d,(k//l-1)*d+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=-10**18 ans -10**18 -10 18 flag=dict() flag dict() dict for i in range(n):
    if i in flag:
        continue
    cur=i
    accum=[]
    flag[cur]=0
    s=0
    while flag[cur]<2:
        cur=p[cur]-1
        s+=c[cur]
        accum.append(s)
        if cur not in flag:
            flag[cur]=0
        else:
            flag[cur]+=1
    l=len(accum)//2
    loop = accum[l-1]
    accum=[0]+accum
    for x in range(1,l+1):
        if loop<=0 or k//l<1:
            ans=max(ans,max(accum[x:x+min(k,l)])-accum[x-1])
        else:
            a=max(accum[x:x+l-1])-accum[x-1]
            d=accum[x+l-1]-accum[x-1]
            if k%l!=0:
                b=max(accum[x:x+k%l])-accum[x-1]
                ans=max(ans,max(k//l*d,k//l*d+b,(k//l-1)*d+a))
            else:
                b=max(accum[x:x+l-1])-accum[x-1]
                ans=max(ans,max(k//l*d,(k//l-1)*d+a)) i range(n) range n if i in flag:
        continue
     i in flag i flag continue cur=i cur i accum=[] accum [] flag[cur]=0 flag[cur] flag cur 0 s=0 s 0 while flag[cur]<2:
        cur=p[cur]-1
        s+=c[cur]
        accum.append(s)
        if cur not in flag:
            flag[cur]=0
        else:
            flag[cur]+=1
     flag[cur]<2 flag[cur] flag cur 2 cur=p[cur]-1 cur p[cur]-1 p[cur] p cur 1 s+=c[cur] s c[cur] c cur accum.append(s) accum.append accum append s if cur not in flag:
            flag[cur]=0
        else:
            flag[cur]+=1
     cur not in flag cur flag flag[cur]=0 flag[cur] flag cur 0 flag[cur]+=1 flag[cur] flag cur 1 l=len(accum)//2 l len(accum)//2 len(accum) len accum 2 loop = accum[l-1] loop accum[l-1] accum l-1 l 1 accum=[0]+accum accum [0]+accum [0] 0 accum for x in range(1,l+1):
        if loop<=0 or k//l<1:
            ans=max(ans,max(accum[x:x+min(k,l)])-accum[x-1])
        else:
            a=max(accum[x:x+l-1])-accum[x-1]
            d=accum[x+l-1]-accum[x-1]
            if k%l!=0:
                b=max(accum[x:x+k%l])-accum[x-1]
                ans=max(ans,max(k//l*d,k//l*d+b,(k//l-1)*d+a))
            else:
                b=max(accum[x:x+l-1])-accum[x-1]
                ans=max(ans,max(k//l*d,(k//l-1)*d+a)) x range(1,l+1) range 1 l+1 l 1 if loop<=0 or k//l<1:
            ans=max(ans,max(accum[x:x+min(k,l)])-accum[x-1])
        else:
            a=max(accum[x:x+l-1])-accum[x-1]
            d=accum[x+l-1]-accum[x-1]
            if k%l!=0:
                b=max(accum[x:x+k%l])-accum[x-1]
                ans=max(ans,max(k//l*d,k//l*d+b,(k//l-1)*d+a))
            else:
                b=max(accum[x:x+l-1])-accum[x-1]
                ans=max(ans,max(k//l*d,(k//l-1)*d+a)) loop<=0 or k//l<1 loop<=0 loop 0 k//l<1 k//l k l 1 ans=max(ans,max(accum[x:x+min(k,l)])-accum[x-1]) ans max(ans,max(accum[x:x+min(k,l)])-accum[x-1]) max ans max(accum[x:x+min(k,l)])-accum[x-1] max(accum[x:x+min(k,l)]) max accum[x:x+min(k,l)] accum x:x+min(k,l) x x+min(k,l) x min(k,l) min k l accum[x-1] accum x-1 x 1 a=max(accum[x:x+l-1])-accum[x-1] a max(accum[x:x+l-1])-accum[x-1] max(accum[x:x+l-1]) max accum[x:x+l-1] accum x:x+l-1 x x+l-1 x+l x l 1 accum[x-1] accum x-1 x 1 d=accum[x+l-1]-accum[x-1] d accum[x+l-1]-accum[x-1] accum[x+l-1] accum x+l-1 x+l x l 1 accum[x-1] accum x-1 x 1 if k%l!=0:
                b=max(accum[x:x+k%l])-accum[x-1]
                ans=max(ans,max(k//l*d,k//l*d+b,(k//l-1)*d+a))
            else:
                b=max(accum[x:x+l-1])-accum[x-1]
                ans=max(ans,max(k//l*d,(k//l-1)*d+a)) k%l!=0 k%l k l 0 b=max(accum[x:x+k%l])-accum[x-1] b max(accum[x:x+k%l])-accum[x-1] max(accum[x:x+k%l]) max accum[x:x+k%l] accum x:x+k%l x x+k%l x k%l k l accum[x-1] accum x-1 x 1 ans=max(ans,max(k//l*d,k//l*d+b,(k//l-1)*d+a)) ans max(ans,max(k//l*d,k//l*d+b,(k//l-1)*d+a)) max ans max(k//l*d,k//l*d+b,(k//l-1)*d+a) max k//l*d k//l k l d k//l*d+b k//l*d k//l k l d b (k//l-1)*d+a (k//l-1)*d k//l-1 k//l k l 1 d a b=max(accum[x:x+l-1])-accum[x-1] b max(accum[x:x+l-1])-accum[x-1] max(accum[x:x+l-1]) max accum[x:x+l-1] accum x:x+l-1 x x+l-1 x+l x l 1 accum[x-1] accum x-1 x 1 ans=max(ans,max(k//l*d,(k//l-1)*d+a)) ans max(ans,max(k//l*d,(k//l-1)*d+a)) max ans max(k//l*d,(k//l-1)*d+a) max k//l*d k//l k l d (k//l-1)*d+a (k//l-1)*d k//l-1 k//l k l 1 d a print(ans) print ans ans=max(ans,max(accum[x:x+min(k,l)])-accum[x-1]) max(ans,max(accum[x:x+min(k,l)])-accum[x-1]) ans accum=[] [] accum k=map(int,input().split()) map(int,input().split()) k l=len(accum)//2 len(accum)//2 l b=max(accum[x:x+l-1])-accum[x-1] max(accum[x:x+l-1])-accum[x-1] b cur=i i cur ans=max(ans,max(k//l*d,(k//l-1)*d+a)) max(ans,max(k//l*d,(k//l-1)*d+a)) ans n,k=map(int,input().split()) map(int,input().split()) n s+=c[cur] c[cur] s loop = accum[l-1] accum[l-1] loop b=max(accum[x:x+k%l])-accum[x-1] max(accum[x:x+k%l])-accum[x-1] b c=list(map(int,input().split())) list(map(int,input().split())) c d=accum[x+l-1]-accum[x-1] accum[x+l-1]-accum[x-1] d accum=[0]+accum [0]+accum accum s=0 0 s cur=p[cur]-1 p[cur]-1 cur ans=-10**18 -10**18 ans a=max(accum[x:x+l-1])-accum[x-1] max(accum[x:x+l-1])-accum[x-1] a flag=dict() dict() flag ans=max(ans,max(k//l*d,k//l*d+b,(k//l-1)*d+a)) max(ans,max(k//l*d,k//l*d+b,(k//l-1)*d+a)) ans 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()))
ans=-10**18
flag=dict()
for i in range(n):
    if i in flag:
        continue
    cur=i
    accum=[]
    flag[cur]=0
    s=0
    while flag[cur]<2:
        cur=p[cur]-1
        s+=c[cur]
        accum.append(s)
        if cur not in flag:
            flag[cur]=0
        else:
            flag[cur]+=1
    l=len(accum)//2
    loop = accum[l-1]
    accum=[0]+accum
    for x in range(1,l+1):
        if loop<=0 or k//l<1:
            ans=max(ans,max(accum[x:x+min(k,l)])-accum[x-1])
        else:
            a=max(accum[x:x+l-1])-accum[x-1]
            d=accum[x+l-1]-accum[x-1]
            if k%l!=0:
                b=max(accum[x:x+k%l])-accum[x-1]
                ans=max(ans,max(k//l*d,k//l*d+b,(k//l-1)*d+a))
            else:
                b=max(accum[x:x+l-1])-accum[x-1]
                ans=max(ans,max(k//l*d,(k//l-1)*d+a))
print(ans)