n,k=map(int,input().split())
p=list(map(int,input().split()))
c=list(map(int,input().split()))
now=-100000000000
for i in range(1,n+1):
    retu=[]
    sum1=0
    sum2=0
    sum3=0
    max1=-1000000000000
    max2=-1000000000000
    flag=0
    x=i
    while(flag==0):
        x=p[x-1]
        sum1+=c[x-1]
        retu.append(c[x-1])
        if x==i:
            flag=1
    if len(retu)>=k: #操作回数が周期未満
        for j in range(k):
            sum2+=retu[j]
            if sum2>max1:
                max1=sum2
        ans=max1
    elif sum1<=0: #周期和が負
        for j in range(len(retu)):
            sum3+=retu[j]
            if sum3>max2:
                max2=sum3
        ans=max2
    else:
        w=k%len(retu)
        
        if w==0:
            w=len(retu)
        for j in range(w):
            sum2+=retu[j]
            if sum2>max1:
                max1=sum2
        for j in range(len(retu)):
            sum3+=retu[j]
            if sum3>max2:
                max2=sum3
        if max1+sum1>max2:
            ans=sum1*(k-w)//len(retu)+max1
        else:
            ans=sum1*((k-w)//len(retu)-1)+max2
    now=max(now,ans)
print(now) 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 now=-100000000000 now -100000000000 for i in range(1,n+1):
    retu=[]
    sum1=0
    sum2=0
    sum3=0
    max1=-1000000000000
    max2=-1000000000000
    flag=0
    x=i
    while(flag==0):
        x=p[x-1]
        sum1+=c[x-1]
        retu.append(c[x-1])
        if x==i:
            flag=1
    if len(retu)>=k: #操作回数が周期未満
        for j in range(k):
            sum2+=retu[j]
            if sum2>max1:
                max1=sum2
        ans=max1
    elif sum1<=0: #周期和が負
        for j in range(len(retu)):
            sum3+=retu[j]
            if sum3>max2:
                max2=sum3
        ans=max2
    else:
        w=k%len(retu)
        
        if w==0:
            w=len(retu)
        for j in range(w):
            sum2+=retu[j]
            if sum2>max1:
                max1=sum2
        for j in range(len(retu)):
            sum3+=retu[j]
            if sum3>max2:
                max2=sum3
        if max1+sum1>max2:
            ans=sum1*(k-w)//len(retu)+max1
        else:
            ans=sum1*((k-w)//len(retu)-1)+max2
    now=max(now,ans) i range(1,n+1) range 1 n+1 n 1 retu=[] retu [] sum1=0 sum1 0 sum2=0 sum2 0 sum3=0 sum3 0 max1=-1000000000000 max1 -1000000000000 max2=-1000000000000 max2 -1000000000000 flag=0 flag 0 x=i x i while(flag==0):
        x=p[x-1]
        sum1+=c[x-1]
        retu.append(c[x-1])
        if x==i:
            flag=1
     flag==0 flag 0 x=p[x-1] x p[x-1] p x-1 x 1 sum1+=c[x-1] sum1 c[x-1] c x-1 x 1 retu.append(c[x-1]) retu.append retu append c[x-1] c x-1 x 1 if x==i:
            flag=1
     x==i x i flag=1 flag 1 if len(retu)>=k: #操作回数が周期未満
        for j in range(k):
            sum2+=retu[j]
            if sum2>max1:
                max1=sum2
        ans=max1
    elif sum1<=0: #周期和が負
        for j in range(len(retu)):
            sum3+=retu[j]
            if sum3>max2:
                max2=sum3
        ans=max2
    else:
        w=k%len(retu)
        
        if w==0:
            w=len(retu)
        for j in range(w):
            sum2+=retu[j]
            if sum2>max1:
                max1=sum2
        for j in range(len(retu)):
            sum3+=retu[j]
            if sum3>max2:
                max2=sum3
        if max1+sum1>max2:
            ans=sum1*(k-w)//len(retu)+max1
        else:
            ans=sum1*((k-w)//len(retu)-1)+max2
     len(retu)>=k len(retu) len retu k for j in range(k):
            sum2+=retu[j]
            if sum2>max1:
                max1=sum2
         j range(k) range k sum2+=retu[j] sum2 retu[j] retu j if sum2>max1:
                max1=sum2
         sum2>max1 sum2 max1 max1=sum2 max1 sum2 ans=max1 ans max1 elif sum1<=0: #周期和が負
        for j in range(len(retu)):
            sum3+=retu[j]
            if sum3>max2:
                max2=sum3
        ans=max2
     sum1<=0 sum1 0 for j in range(len(retu)):
            sum3+=retu[j]
            if sum3>max2:
                max2=sum3
         j range(len(retu)) range len(retu) len retu sum3+=retu[j] sum3 retu[j] retu j if sum3>max2:
                max2=sum3
         sum3>max2 sum3 max2 max2=sum3 max2 sum3 ans=max2 ans max2 w=k%len(retu) w k%len(retu) k len(retu) len retu if w==0:
            w=len(retu)
         w==0 w 0 w=len(retu) w len(retu) len retu for j in range(w):
            sum2+=retu[j]
            if sum2>max1:
                max1=sum2
         j range(w) range w sum2+=retu[j] sum2 retu[j] retu j if sum2>max1:
                max1=sum2
         sum2>max1 sum2 max1 max1=sum2 max1 sum2 for j in range(len(retu)):
            sum3+=retu[j]
            if sum3>max2:
                max2=sum3
         j range(len(retu)) range len(retu) len retu sum3+=retu[j] sum3 retu[j] retu j if sum3>max2:
                max2=sum3
         sum3>max2 sum3 max2 max2=sum3 max2 sum3 if max1+sum1>max2:
            ans=sum1*(k-w)//len(retu)+max1
        else:
            ans=sum1*((k-w)//len(retu)-1)+max2
     max1+sum1>max2 max1+sum1 max1 sum1 max2 ans=sum1*(k-w)//len(retu)+max1 ans sum1*(k-w)//len(retu)+max1 sum1*(k-w)//len(retu) sum1*(k-w) sum1 k-w k w len(retu) len retu max1 ans=sum1*((k-w)//len(retu)-1)+max2 ans sum1*((k-w)//len(retu)-1)+max2 sum1*((k-w)//len(retu)-1) sum1 (k-w)//len(retu)-1 (k-w)//len(retu) k-w k w len(retu) len retu 1 max2 now=max(now,ans) now max(now,ans) max now ans print(now) print now sum3=0 0 sum3 flag=0 0 flag max2=sum3 sum3 max2 max2=-1000000000000 -1000000000000 max2 n,k=map(int,input().split()) map(int,input().split()) n now=max(now,ans) max(now,ans) now sum2+=retu[j] retu[j] sum2 x=i i x sum1=0 0 sum1 max1=sum2 sum2 max1 w=len(retu) len(retu) w p=list(map(int,input().split())) list(map(int,input().split())) p ans=max1 max1 ans ans=sum1*((k-w)//len(retu)-1)+max2 sum1*((k-w)//len(retu)-1)+max2 ans x=p[x-1] p[x-1] x c=list(map(int,input().split())) list(map(int,input().split())) c sum3+=retu[j] retu[j] sum3 ans=max2 max2 ans w=k%len(retu) k%len(retu) w sum1+=c[x-1] c[x-1] sum1 sum3+=retu[j] retu[j] sum3 max2=sum3 sum3 max2 flag=1 1 flag now=-100000000000 -100000000000 now sum2+=retu[j] retu[j] sum2 max1=-1000000000000 -1000000000000 max1 k=map(int,input().split()) map(int,input().split()) k max1=sum2 sum2 max1 retu=[] [] retu ans=sum1*(k-w)//len(retu)+max1 sum1*(k-w)//len(retu)+max1 ans sum2=0 0 sum2
n,k=map(int,input().split())
p=list(map(int,input().split()))
c=list(map(int,input().split()))
now=-100000000000
for i in range(1,n+1):
    retu=[]
    sum1=0
    sum2=0
    sum3=0
    max1=-1000000000000
    max2=-1000000000000
    flag=0
    x=i
    while(flag==0):
        x=p[x-1]
        sum1+=c[x-1]
        retu.append(c[x-1])
        if x==i:
            flag=1
    if len(retu)>=k: #操作回数が周期未満
        for j in range(k):
            sum2+=retu[j]
            if sum2>max1:
                max1=sum2
        ans=max1
    elif sum1<=0: #周期和が負
        for j in range(len(retu)):
            sum3+=retu[j]
            if sum3>max2:
                max2=sum3
        ans=max2
    else:
        w=k%len(retu)
        
        if w==0:
            w=len(retu)
        for j in range(w):
            sum2+=retu[j]
            if sum2>max1:
                max1=sum2
        for j in range(len(retu)):
            sum3+=retu[j]
            if sum3>max2:
                max2=sum3
        if max1+sum1>max2:
            ans=sum1*(k-w)//len(retu)+max1
        else:
            ans=sum1*((k-w)//len(retu)-1)+max2
    now=max(now,ans)
print(now)