n,k=map(int,input().split())
P=list(map(int,input().split()))
P=[p-1 for p in P]
C=list(map(int,input().split()))

def main():
  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)
  return ans      


if __name__=='__main__':
  print(main()) 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 p-1 for p in P for p in P p P for p in P p-1 p 1 P=[p-1 for p in P] P [p-1 for p 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 def main():
  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)
  return ans       main 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 return ans ans if __name__=='__main__':
  print(main()) __name__=='__main__' __name__ '__main__' print(main()) print main() main P=list(map(int,input().split())) list(map(int,input().split())) P k=map(int,input().split()) map(int,input().split()) k C=list(map(int,input().split())) list(map(int,input().split())) C P=[p-1 for p in P] [p-1 for p in P] P n,k=map(int,input().split()) map(int,input().split()) n def main():
  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)
  return ans       def main():
  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)
  return ans       main
n,k=map(int,input().split())
P=list(map(int,input().split()))
P=[p-1 for p in P]
C=list(map(int,input().split()))

def main():
  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)
  return ans      


if __name__=='__main__':
  print(main())
