N,K=map(int,input().split())
P=[int(a)-1 for a in input().split()]
C=[int(a) for a in input().split()]
ans=-float("inf")
for si in range(N):
  x=si
  S=[]
  tot=0
  while True:
    x=P[x]
    S.append(C[x])
    tot+=C[x]
    if x==si:
      break
  L=len(S)
  t=0
  for i in range(L):
    t+=S[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 int(a)-1 for a in input().split() for a in input().split() a input().split() ().split () input split for a in input().split() int(a)-1 int(a) int a 1 P=[int(a)-1 for a in input().split()] P [int(a)-1 for a in input().split()] int(a) for a in input().split() for a in input().split() a input().split() ().split () input split for a in input().split() int(a) int a C=[int(a) for a in input().split()] C [int(a) for a in input().split()] ans=-float("inf") ans -float("inf") float("inf") float "inf" for si in range(N):
  x=si
  S=[]
  tot=0
  while True:
    x=P[x]
    S.append(C[x])
    tot+=C[x]
    if x==si:
      break
  L=len(S)
  t=0
  for i in range(L):
    t+=S[i]
    if i+1>K:
      break
    now=t
    if tot>0:
      e=(K-i-1)//L
      now+=tot*e
    ans=max(ans,now)
     si range(N) range N x=si x si S=[] S [] tot=0 tot 0 while True:
    x=P[x]
    S.append(C[x])
    tot+=C[x]
    if x==si:
      break
   True x=P[x] x P[x] P x S.append(C[x]) S.append S append C[x] C x tot+=C[x] tot C[x] C x if x==si:
      break
   x==si x si break L=len(S) L len(S) len S t=0 t 0 for i in range(L):
    t+=S[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+=S[i] t S[i] S 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 C=[int(a) for a in input().split()] [int(a) for a in input().split()] C now=t t now N,K=map(int,input().split()) map(int,input().split()) N ans=-float("inf") -float("inf") ans P=[int(a)-1 for a in input().split()] [int(a)-1 for a in input().split()] P x=P[x] P[x] x L=len(S) len(S) L e=(K-i-1)//L (K-i-1)//L e t=0 0 t ans=max(ans,now) max(ans,now) ans S=[] [] S tot=0 0 tot K=map(int,input().split()) map(int,input().split()) K tot+=C[x] C[x] tot now+=tot*e tot*e now t+=S[i] S[i] t x=si si x
N,K=map(int,input().split())
P=[int(a)-1 for a in input().split()]
C=[int(a) for a in input().split()]
ans=-float("inf")
for si in range(N):
  x=si
  S=[]
  tot=0
  while True:
    x=P[x]
    S.append(C[x])
    tot+=C[x]
    if x==si:
      break
  L=len(S)
  t=0
  for i in range(L):
    t+=S[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)