n,k=map(int,input().split())
p=list(map(int,input().split()))
c=list(map(int,input().split()))
root=[]
mi=set(range(n))
while mi:
  v=mi.pop()
  r=[c[v]]
  while p[v]-1 in mi:
    r.append(c[p[v]-1])
    mi.discard(p[v]-1)
    v=p[v]-1
  root.append(r)
inf=float('inf')
ans=-inf
for r in root:
  ansr=-inf
  k1=k%len(r)
  k0=k//len(r)
  cr=[0]
  tmp=0
  for ri in r:
    tmp+=ri
    cr.append(tmp)
  sumr=cr[-1]
  for ri in r:
    tmp+=ri
    cr.append(tmp)
  for i in range(len(r)):
    for j in range(i,i+min(k,len(r))):
      ansr=max(ansr,cr[j+1]-cr[i])
  if sumr>0:
    ans=max(ans,ansr+sumr*(k0-1))
  else:
    ans=max(ans,ansr)
  if sumr>0:
    ansr=0
    for i in range(len(r)):
      for j in range(i,i+k1):
        ansr=max(ansr,cr[j+1]-cr[i])    
    ans=max(ans,ansr+sumr*k0)
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 c=list(map(int,input().split())) c list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split root=[] root [] mi=set(range(n)) mi set(range(n)) set range(n) range n while mi:
  v=mi.pop()
  r=[c[v]]
  while p[v]-1 in mi:
    r.append(c[p[v]-1])
    mi.discard(p[v]-1)
    v=p[v]-1
  root.append(r) mi v=mi.pop() v mi.pop() mi.pop mi pop r=[c[v]] r [c[v]] c[v] c v while p[v]-1 in mi:
    r.append(c[p[v]-1])
    mi.discard(p[v]-1)
    v=p[v]-1
   p[v]-1 in mi p[v]-1 p[v] p v 1 mi r.append(c[p[v]-1]) r.append r append c[p[v]-1] c p[v]-1 p[v] p v 1 mi.discard(p[v]-1) mi.discard mi discard p[v]-1 p[v] p v 1 v=p[v]-1 v p[v]-1 p[v] p v 1 root.append(r) root.append root append r inf=float('inf') inf float('inf') float 'inf' ans=-inf ans -inf inf for r in root:
  ansr=-inf
  k1=k%len(r)
  k0=k//len(r)
  cr=[0]
  tmp=0
  for ri in r:
    tmp+=ri
    cr.append(tmp)
  sumr=cr[-1]
  for ri in r:
    tmp+=ri
    cr.append(tmp)
  for i in range(len(r)):
    for j in range(i,i+min(k,len(r))):
      ansr=max(ansr,cr[j+1]-cr[i])
  if sumr>0:
    ans=max(ans,ansr+sumr*(k0-1))
  else:
    ans=max(ans,ansr)
  if sumr>0:
    ansr=0
    for i in range(len(r)):
      for j in range(i,i+k1):
        ansr=max(ansr,cr[j+1]-cr[i])    
    ans=max(ans,ansr+sumr*k0) r root ansr=-inf ansr -inf inf k1=k%len(r) k1 k%len(r) k len(r) len r k0=k//len(r) k0 k//len(r) k len(r) len r cr=[0] cr [0] 0 tmp=0 tmp 0 for ri in r:
    tmp+=ri
    cr.append(tmp)
   ri r tmp+=ri tmp ri cr.append(tmp) cr.append cr append tmp sumr=cr[-1] sumr cr[-1] cr -1 for ri in r:
    tmp+=ri
    cr.append(tmp)
   ri r tmp+=ri tmp ri cr.append(tmp) cr.append cr append tmp for i in range(len(r)):
    for j in range(i,i+min(k,len(r))):
      ansr=max(ansr,cr[j+1]-cr[i])
   i range(len(r)) range len(r) len r for j in range(i,i+min(k,len(r))):
      ansr=max(ansr,cr[j+1]-cr[i])
   j range(i,i+min(k,len(r))) range i i+min(k,len(r)) i min(k,len(r)) min k len(r) len r ansr=max(ansr,cr[j+1]-cr[i]) ansr max(ansr,cr[j+1]-cr[i]) max ansr cr[j+1]-cr[i] cr[j+1] cr j+1 j 1 cr[i] cr i if sumr>0:
    ans=max(ans,ansr+sumr*(k0-1))
  else:
    ans=max(ans,ansr)
   sumr>0 sumr 0 ans=max(ans,ansr+sumr*(k0-1)) ans max(ans,ansr+sumr*(k0-1)) max ans ansr+sumr*(k0-1) ansr sumr*(k0-1) sumr k0-1 k0 1 ans=max(ans,ansr) ans max(ans,ansr) max ans ansr if sumr>0:
    ansr=0
    for i in range(len(r)):
      for j in range(i,i+k1):
        ansr=max(ansr,cr[j+1]-cr[i])    
    ans=max(ans,ansr+sumr*k0) sumr>0 sumr 0 ansr=0 ansr 0 for i in range(len(r)):
      for j in range(i,i+k1):
        ansr=max(ansr,cr[j+1]-cr[i])    
     i range(len(r)) range len(r) len r for j in range(i,i+k1):
        ansr=max(ansr,cr[j+1]-cr[i])    
     j range(i,i+k1) range i i+k1 i k1 ansr=max(ansr,cr[j+1]-cr[i]) ansr max(ansr,cr[j+1]-cr[i]) max ansr cr[j+1]-cr[i] cr[j+1] cr j+1 j 1 cr[i] cr i ans=max(ans,ansr+sumr*k0) ans max(ans,ansr+sumr*k0) max ans ansr+sumr*k0 ansr sumr*k0 sumr k0 print(ans) print ans tmp+=ri ri tmp v=mi.pop() mi.pop() v tmp=0 0 tmp sumr=cr[-1] cr[-1] sumr r=[c[v]] [c[v]] r ans=-inf -inf ans ansr=max(ansr,cr[j+1]-cr[i]) max(ansr,cr[j+1]-cr[i]) ansr root=[] [] root ansr=max(ansr,cr[j+1]-cr[i]) max(ansr,cr[j+1]-cr[i]) ansr tmp+=ri ri tmp ans=max(ans,ansr+sumr*(k0-1)) max(ans,ansr+sumr*(k0-1)) ans n,k=map(int,input().split()) map(int,input().split()) n c=list(map(int,input().split())) list(map(int,input().split())) c ansr=-inf -inf ansr v=p[v]-1 p[v]-1 v p=list(map(int,input().split())) list(map(int,input().split())) p k0=k//len(r) k//len(r) k0 ansr=0 0 ansr ans=max(ans,ansr) max(ans,ansr) ans inf=float('inf') float('inf') inf k1=k%len(r) k%len(r) k1 mi=set(range(n)) set(range(n)) mi cr=[0] [0] cr ans=max(ans,ansr+sumr*k0) max(ans,ansr+sumr*k0) ans k=map(int,input().split()) map(int,input().split()) k
n,k=map(int,input().split())
p=list(map(int,input().split()))
c=list(map(int,input().split()))
root=[]
mi=set(range(n))
while mi:
  v=mi.pop()
  r=[c[v]]
  while p[v]-1 in mi:
    r.append(c[p[v]-1])
    mi.discard(p[v]-1)
    v=p[v]-1
  root.append(r)
inf=float('inf')
ans=-inf
for r in root:
  ansr=-inf
  k1=k%len(r)
  k0=k//len(r)
  cr=[0]
  tmp=0
  for ri in r:
    tmp+=ri
    cr.append(tmp)
  sumr=cr[-1]
  for ri in r:
    tmp+=ri
    cr.append(tmp)
  for i in range(len(r)):
    for j in range(i,i+min(k,len(r))):
      ansr=max(ansr,cr[j+1]-cr[i])
  if sumr>0:
    ans=max(ans,ansr+sumr*(k0-1))
  else:
    ans=max(ans,ansr)
  if sumr>0:
    ansr=0
    for i in range(len(r)):
      for j in range(i,i+k1):
        ansr=max(ansr,cr[j+1]-cr[i])    
    ans=max(ans,ansr+sumr*k0)
print(ans)
