import sys
mod=10**9+7 ; inf=float("inf")
from math import sqrt, ceil
from collections import deque, Counter, defaultdict #すべてのkeyが用意されてる defaultdict(int)で初期化
input=lambda: sys.stdin.readline().strip()
sys.setrecursionlimit(11451419)
from decimal import ROUND_HALF_UP,Decimal  #変換後の末尾桁を0や0.01で指定
  #Decimal((str(0.5)).quantize(Decimal('0'), rounding=ROUND_HALF_UP))
from functools import lru_cache
#メモ化再帰defの冒頭に毎回 @lru_cache(maxsize=10**10)
#引数にlistはだめ
#######ここまでテンプレ#######
#ソート、"a"+"b"、再帰ならPython3の方がいい
#######ここから天ぷら########


n,k=map(int,input().split())
P=list(map(int,input().split()))
P=[i-1 for i in P]
C=list(map(int,input().split()))
ans=-inf
for i in range(n):
    now=i
    total=0
    roop= 0
    po=deque([0])
    for kai in range(1,k+1):
        now=P[now]
        total += C[now]
        roop+=1
        po.append(total)
        ans=max(ans, total)
        if now == i:
            la= kai;break

    else: continue
    if total<0 : continue
    po=list(po)
    ans=max(ans, total* (k//roop) + max(po[:k%roop+1]), total*(k//roop-1) + max(po))
    #print(total, roop,po ,ans)
print(ans) import sys sys mod=10**9+7 mod 10**9+7 10**9 10 9 7 inf=float("inf") inf float("inf") float "inf" from math import sqrt, ceil from collections import deque, Counter, defaultdict input=lambda: sys.stdin.readline().strip() input lambda: sys.stdin.readline().strip() sys.stdin.readline().strip() sys.stdin.readline().strip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline strip sys.setrecursionlimit(11451419) sys.setrecursionlimit sys setrecursionlimit 11451419 from decimal import ROUND_HALF_UP,Decimal from functools import lru_cache 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 i-1 for i in P for i in P i P for i in P i-1 i 1 P=[i-1 for i in P] P [i-1 for i in P] 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=-inf ans -inf inf for i in range(n):
    now=i
    total=0
    roop= 0
    po=deque([0])
    for kai in range(1,k+1):
        now=P[now]
        total += C[now]
        roop+=1
        po.append(total)
        ans=max(ans, total)
        if now == i:
            la= kai;break

    else: continue
    if total<0 : continue
    po=list(po)
    ans=max(ans, total* (k//roop) + max(po[:k%roop+1]), total*(k//roop-1) + max(po))
    #print(total, roop,po ,ans) i range(n) range n now=i now i total=0 total 0 roop= 0 roop 0 po=deque([0]) po deque([0]) deque [0] 0 for kai in range(1,k+1):
        now=P[now]
        total += C[now]
        roop+=1
        po.append(total)
        ans=max(ans, total)
        if now == i:
            la= kai;break

    else: continue
     kai range(1,k+1) range 1 k+1 k 1 now=P[now] now P[now] P now total += C[now] total C[now] C now roop+=1 roop 1 po.append(total) po.append po append total ans=max(ans, total) ans max(ans, total) max ans total if now == i:
            la= kai;break

     now == i now i la= kai la kai break if total<0 : continue
     total<0 total 0 po=list(po) po list(po) list po ans=max(ans, total* (k//roop) + max(po[:k%roop+1]), total*(k//roop-1) + max(po)) ans max(ans, total* (k//roop) + max(po[:k%roop+1]), total*(k//roop-1) + max(po)) max ans total* (k//roop) + max(po[:k%roop+1]) total* (k//roop) total k//roop k roop max(po[:k%roop+1]) max po[:k%roop+1] po :k%roop+1 k%roop+1 k%roop k roop 1 total*(k//roop-1) + max(po) total*(k//roop-1) total k//roop-1 k//roop k roop 1 max(po) max po print(ans) print ans now=P[now] P[now] now inf=float("inf") float("inf") inf po=list(po) list(po) po ans=max(ans, total) max(ans, total) ans ans=max(ans, total* (k//roop) + max(po[:k%roop+1]), total*(k//roop-1) + max(po)) max(ans, total* (k//roop) + max(po[:k%roop+1]), total*(k//roop-1) + max(po)) ans roop= 0 0 roop input=lambda: sys.stdin.readline().strip() lambda: sys.stdin.readline().strip() input now=i i now roop+=1 1 roop mod=10**9+7 10**9+7 mod ans=-inf -inf ans la= kai kai la total += C[now] C[now] total k=map(int,input().split()) map(int,input().split()) k C=list(map(int,input().split())) list(map(int,input().split())) C total=0 0 total P=list(map(int,input().split())) list(map(int,input().split())) P P=[i-1 for i in P] [i-1 for i in P] P po=deque([0]) deque([0]) po n,k=map(int,input().split()) map(int,input().split()) n
import sys
mod=10**9+7 ; inf=float("inf")
from math import sqrt, ceil
from collections import deque, Counter, defaultdict #すべてのkeyが用意されてる defaultdict(int)で初期化
input=lambda: sys.stdin.readline().strip()
sys.setrecursionlimit(11451419)
from decimal import ROUND_HALF_UP,Decimal  #変換後の末尾桁を0や0.01で指定
  #Decimal((str(0.5)).quantize(Decimal('0'), rounding=ROUND_HALF_UP))
from functools import lru_cache
#メモ化再帰defの冒頭に毎回 @lru_cache(maxsize=10**10)
#引数にlistはだめ
#######ここまでテンプレ#######
#ソート、"a"+"b"、再帰ならPython3の方がいい
#######ここから天ぷら########


n,k=map(int,input().split())
P=list(map(int,input().split()))
P=[i-1 for i in P]
C=list(map(int,input().split()))
ans=-inf
for i in range(n):
    now=i
    total=0
    roop= 0
    po=deque([0])
    for kai in range(1,k+1):
        now=P[now]
        total += C[now]
        roop+=1
        po.append(total)
        ans=max(ans, total)
        if now == i:
            la= kai;break

    else: continue
    if total<0 : continue
    po=list(po)
    ans=max(ans, total* (k//roop) + max(po[:k%roop+1]), total*(k//roop-1) + max(po))
    #print(total, roop,po ,ans)
print(ans)