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()))
import math
ans=-10**18
for i in range(N):
  x=i
  tot=0
  cycle=[]
  while True:
    x=P[x]
    cycle.append(C[x])
    tot+=C[x]
    if x==i:
      break
  L=len(cycle)
  t=0
  for i in range(L):
    t+=cycle[i]
    if i+1>K:
      break
    now=t
    if tot>0:
      e=(K-i-1)//L
      now+=tot*e
    ans=max(ans,now)

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 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 import math math ans=-10**18 ans -10**18 -10 18 for i in range(N):
  x=i
  tot=0
  cycle=[]
  while True:
    x=P[x]
    cycle.append(C[x])
    tot+=C[x]
    if x==i:
      break
  L=len(cycle)
  t=0
  for i in range(L):
    t+=cycle[i]
    if i+1>K:
      break
    now=t
    if tot>0:
      e=(K-i-1)//L
      now+=tot*e
    ans=max(ans,now) i range(N) range N x=i x i tot=0 tot 0 cycle=[] cycle [] while True:
    x=P[x]
    cycle.append(C[x])
    tot+=C[x]
    if x==i:
      break
   True x=P[x] x P[x] P x cycle.append(C[x]) cycle.append cycle append C[x] C x tot+=C[x] tot C[x] C x if x==i:
      break
   x==i x i break L=len(cycle) L len(cycle) len cycle t=0 t 0 for i in range(L):
    t+=cycle[i]
    if i+1>K:
      break
    now=t
    if tot>0:
      e=(K-i-1)//L
      now+=tot*e
    ans=max(ans,now) i range(L) range L t+=cycle[i] t cycle[i] cycle i if i+1>K:
      break
     i+1>K i+1 i 1 K break now=t now t if tot>0:
      e=(K-i-1)//L
      now+=tot*e
     tot>0 tot 0 e=(K-i-1)//L e (K-i-1)//L K-i-1 K-i K i 1 L now+=tot*e now tot*e tot e ans=max(ans,now) ans max(ans,now) max ans now print(ans) print ans tot=0 0 tot L=len(cycle) len(cycle) L N,K=map(int,input().split()) map(int,input().split()) N P=list(map(int,input().split())) list(map(int,input().split())) P t+=cycle[i] cycle[i] t ans=-10**18 -10**18 ans C=list(map(int,input().split())) list(map(int,input().split())) C now+=tot*e tot*e now t=0 0 t x=i i x e=(K-i-1)//L (K-i-1)//L e ans=max(ans,now) max(ans,now) ans tot+=C[x] C[x] tot now=t t now K=map(int,input().split()) map(int,input().split()) K P=[i-1 for i in P] [i-1 for i in P] P cycle=[] [] cycle x=P[x] P[x] x
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()))
import math
ans=-10**18
for i in range(N):
  x=i
  tot=0
  cycle=[]
  while True:
    x=P[x]
    cycle.append(C[x])
    tot+=C[x]
    if x==i:
      break
  L=len(cycle)
  t=0
  for i in range(L):
    t+=cycle[i]
    if i+1>K:
      break
    now=t
    if tot>0:
      e=(K-i-1)//L
      now+=tot*e
    ans=max(ans,now)

print(ans)