#import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
import heapq
from fractions  import gcd
#input=sys.stdin.readline
#import bisect

n,k=map(int,input().split())
p=list(map(int,input().split()))
c=list(map(int,input().split()))
check=[-1]*n
finalans=-10**20
for i in range(n):
    if check[i]!=-1:
        continue
    loop=[i]
    now=i
    while p[now]!=i+1:
        check[now]=1
        loop.append(p[now]-1)
        now=p[now]-1
    L=len(loop)
    point=[0]*(L*2)
    point[0]=c[loop[0]]
    for j in range(1,L*2):
        if j<L:
            point[j]=point[j-1]+c[loop[j]]
        else:
            point[j]=point[j-1]+c[loop[j-L]]
    m=-10**20
    if point[L-1]>0:
        r=k%L
        if r==0:
            r=L
        for j in range(1,r+1):
            for t in range(j-1,j+L-1):
                cand=point[t]-point[t-j] if t-j>=0 else point[t]
                m=max(m,cand)
        ans=point[L-1]*(k//L)+m if r!=L else point[L-1]*((k//L)-1)+m
    else:
        for j in range(1,L+1):
            for t in range(j-1,j+L-1):
                cand=point[t]-point[t-j] if t-j>=0 else point[t]
                m=max(m,cand)
        ans=m
    finalans=max(ans,finalans)   
print(finalans) import math math import itertools itertools from collections import deque from collections import Counter import heapq heapq from fractions  import gcd 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 check=[-1]*n check [-1]*n [-1] -1 n finalans=-10**20 finalans -10**20 -10 20 for i in range(n):
    if check[i]!=-1:
        continue
    loop=[i]
    now=i
    while p[now]!=i+1:
        check[now]=1
        loop.append(p[now]-1)
        now=p[now]-1
    L=len(loop)
    point=[0]*(L*2)
    point[0]=c[loop[0]]
    for j in range(1,L*2):
        if j<L:
            point[j]=point[j-1]+c[loop[j]]
        else:
            point[j]=point[j-1]+c[loop[j-L]]
    m=-10**20
    if point[L-1]>0:
        r=k%L
        if r==0:
            r=L
        for j in range(1,r+1):
            for t in range(j-1,j+L-1):
                cand=point[t]-point[t-j] if t-j>=0 else point[t]
                m=max(m,cand)
        ans=point[L-1]*(k//L)+m if r!=L else point[L-1]*((k//L)-1)+m
    else:
        for j in range(1,L+1):
            for t in range(j-1,j+L-1):
                cand=point[t]-point[t-j] if t-j>=0 else point[t]
                m=max(m,cand)
        ans=m
    finalans=max(ans,finalans)    i range(n) range n if check[i]!=-1:
        continue
     check[i]!=-1 check[i] check i -1 continue loop=[i] loop [i] i now=i now i while p[now]!=i+1:
        check[now]=1
        loop.append(p[now]-1)
        now=p[now]-1
     p[now]!=i+1 p[now] p now i+1 i 1 check[now]=1 check[now] check now 1 loop.append(p[now]-1) loop.append loop append p[now]-1 p[now] p now 1 now=p[now]-1 now p[now]-1 p[now] p now 1 L=len(loop) L len(loop) len loop point=[0]*(L*2) point [0]*(L*2) [0] 0 L*2 L 2 point[0]=c[loop[0]] point[0] point 0 c[loop[0]] c loop[0] loop 0 for j in range(1,L*2):
        if j<L:
            point[j]=point[j-1]+c[loop[j]]
        else:
            point[j]=point[j-1]+c[loop[j-L]]
     j range(1,L*2) range 1 L*2 L 2 if j<L:
            point[j]=point[j-1]+c[loop[j]]
        else:
            point[j]=point[j-1]+c[loop[j-L]]
     j<L j L point[j]=point[j-1]+c[loop[j]] point[j] point j point[j-1]+c[loop[j]] point[j-1] point j-1 j 1 c[loop[j]] c loop[j] loop j point[j]=point[j-1]+c[loop[j-L]] point[j] point j point[j-1]+c[loop[j-L]] point[j-1] point j-1 j 1 c[loop[j-L]] c loop[j-L] loop j-L j L m=-10**20 m -10**20 -10 20 if point[L-1]>0:
        r=k%L
        if r==0:
            r=L
        for j in range(1,r+1):
            for t in range(j-1,j+L-1):
                cand=point[t]-point[t-j] if t-j>=0 else point[t]
                m=max(m,cand)
        ans=point[L-1]*(k//L)+m if r!=L else point[L-1]*((k//L)-1)+m
    else:
        for j in range(1,L+1):
            for t in range(j-1,j+L-1):
                cand=point[t]-point[t-j] if t-j>=0 else point[t]
                m=max(m,cand)
        ans=m
     point[L-1]>0 point[L-1] point L-1 L 1 0 r=k%L r k%L k L if r==0:
            r=L
         r==0 r 0 r=L r L for j in range(1,r+1):
            for t in range(j-1,j+L-1):
                cand=point[t]-point[t-j] if t-j>=0 else point[t]
                m=max(m,cand)
         j range(1,r+1) range 1 r+1 r 1 for t in range(j-1,j+L-1):
                cand=point[t]-point[t-j] if t-j>=0 else point[t]
                m=max(m,cand)
         t range(j-1,j+L-1) range j-1 j 1 j+L-1 j+L j L 1 cand=point[t]-point[t-j] if t-j>=0 else point[t] cand point[t]-point[t-j] if t-j>=0 else point[t] t-j>=0 t-j t j 0 point[t]-point[t-j] point[t] point t point[t-j] point t-j t j point[t] point t m=max(m,cand) m max(m,cand) max m cand ans=point[L-1]*(k//L)+m if r!=L else point[L-1]*((k//L)-1)+m ans point[L-1]*(k//L)+m if r!=L else point[L-1]*((k//L)-1)+m r!=L r L point[L-1]*(k//L)+m point[L-1]*(k//L) point[L-1] point L-1 L 1 k//L k L m point[L-1]*((k//L)-1)+m point[L-1]*((k//L)-1) point[L-1] point L-1 L 1 (k//L)-1 k//L k L 1 m for j in range(1,L+1):
            for t in range(j-1,j+L-1):
                cand=point[t]-point[t-j] if t-j>=0 else point[t]
                m=max(m,cand)
         j range(1,L+1) range 1 L+1 L 1 for t in range(j-1,j+L-1):
                cand=point[t]-point[t-j] if t-j>=0 else point[t]
                m=max(m,cand)
         t range(j-1,j+L-1) range j-1 j 1 j+L-1 j+L j L 1 cand=point[t]-point[t-j] if t-j>=0 else point[t] cand point[t]-point[t-j] if t-j>=0 else point[t] t-j>=0 t-j t j 0 point[t]-point[t-j] point[t] point t point[t-j] point t-j t j point[t] point t m=max(m,cand) m max(m,cand) max m cand ans=m ans m finalans=max(ans,finalans) finalans max(ans,finalans) max ans finalans print(finalans) print finalans check=[-1]*n [-1]*n check m=max(m,cand) max(m,cand) m k=map(int,input().split()) map(int,input().split()) k L=len(loop) len(loop) L m=-10**20 -10**20 m loop=[i] [i] loop finalans=max(ans,finalans) max(ans,finalans) finalans r=L L r cand=point[t]-point[t-j] if t-j>=0 else point[t] point[t]-point[t-j] if t-j>=0 else point[t] cand now=i i now m=max(m,cand) max(m,cand) m ans=point[L-1]*(k//L)+m if r!=L else point[L-1]*((k//L)-1)+m point[L-1]*(k//L)+m if r!=L else point[L-1]*((k//L)-1)+m ans point=[0]*(L*2) [0]*(L*2) point r=k%L k%L r cand=point[t]-point[t-j] if t-j>=0 else point[t] point[t]-point[t-j] if t-j>=0 else point[t] cand now=p[now]-1 p[now]-1 now p=list(map(int,input().split())) list(map(int,input().split())) p n,k=map(int,input().split()) map(int,input().split()) n c=list(map(int,input().split())) list(map(int,input().split())) c finalans=-10**20 -10**20 finalans ans=m m ans
#import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
import heapq
from fractions  import gcd
#input=sys.stdin.readline
#import bisect

n,k=map(int,input().split())
p=list(map(int,input().split()))
c=list(map(int,input().split()))
check=[-1]*n
finalans=-10**20
for i in range(n):
    if check[i]!=-1:
        continue
    loop=[i]
    now=i
    while p[now]!=i+1:
        check[now]=1
        loop.append(p[now]-1)
        now=p[now]-1
    L=len(loop)
    point=[0]*(L*2)
    point[0]=c[loop[0]]
    for j in range(1,L*2):
        if j<L:
            point[j]=point[j-1]+c[loop[j]]
        else:
            point[j]=point[j-1]+c[loop[j-L]]
    m=-10**20
    if point[L-1]>0:
        r=k%L
        if r==0:
            r=L
        for j in range(1,r+1):
            for t in range(j-1,j+L-1):
                cand=point[t]-point[t-j] if t-j>=0 else point[t]
                m=max(m,cand)
        ans=point[L-1]*(k//L)+m if r!=L else point[L-1]*((k//L)-1)+m
    else:
        for j in range(1,L+1):
            for t in range(j-1,j+L-1):
                cand=point[t]-point[t-j] if t-j>=0 else point[t]
                m=max(m,cand)
        ans=m
    finalans=max(ans,finalans)   
print(finalans)