N,K=map(int,input().split())
P=list(map(int,input().split()))
C=list(map(int,input().split()))

dummy=set()
loop=[]
X=set(range(N))
while len(dummy)!=N:
  a=[]
  k=min(X-dummy)
  a.append(k)
  dummy.add(k)
  l=P[k]-1
  while l!=k:
    a.append(l)
    dummy.add(l)
    l=P[l]-1
  loop.append(a)

ans=-(10**300)
for l in loop:
  temp=0
  s=len(l)
  ssum=0
  k=0
  for i in range(s):
    ssum+=C[l[i]]
  if ssum>0:
    temp+=((K-1)//s)*ssum
    k=K-((K-1)//s)*s
  else:
    k=min(K,s)
  kmax=-(10**300)
  for ii in range(s):
    tempscore=temp
    for jj in range(1,k+1):
      tempscore+=C[l[(ii+jj)%s]]
      if tempscore>kmax:
        kmax=tempscore
  if kmax>ans:
    ans=kmax
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 dummy=set() dummy set() set loop=[] loop [] X=set(range(N)) X set(range(N)) set range(N) range N while len(dummy)!=N:
  a=[]
  k=min(X-dummy)
  a.append(k)
  dummy.add(k)
  l=P[k]-1
  while l!=k:
    a.append(l)
    dummy.add(l)
    l=P[l]-1
  loop.append(a) len(dummy)!=N len(dummy) len dummy N a=[] a [] k=min(X-dummy) k min(X-dummy) min X-dummy X dummy a.append(k) a.append a append k dummy.add(k) dummy.add dummy add k l=P[k]-1 l P[k]-1 P[k] P k 1 while l!=k:
    a.append(l)
    dummy.add(l)
    l=P[l]-1
   l!=k l k a.append(l) a.append a append l dummy.add(l) dummy.add dummy add l l=P[l]-1 l P[l]-1 P[l] P l 1 loop.append(a) loop.append loop append a ans=-(10**300) ans -(10**300) 10**300 10 300 for l in loop:
  temp=0
  s=len(l)
  ssum=0
  k=0
  for i in range(s):
    ssum+=C[l[i]]
  if ssum>0:
    temp+=((K-1)//s)*ssum
    k=K-((K-1)//s)*s
  else:
    k=min(K,s)
  kmax=-(10**300)
  for ii in range(s):
    tempscore=temp
    for jj in range(1,k+1):
      tempscore+=C[l[(ii+jj)%s]]
      if tempscore>kmax:
        kmax=tempscore
  if kmax>ans:
    ans=kmax l loop temp=0 temp 0 s=len(l) s len(l) len l ssum=0 ssum 0 k=0 k 0 for i in range(s):
    ssum+=C[l[i]]
   i range(s) range s ssum+=C[l[i]] ssum C[l[i]] C l[i] l i if ssum>0:
    temp+=((K-1)//s)*ssum
    k=K-((K-1)//s)*s
  else:
    k=min(K,s)
   ssum>0 ssum 0 temp+=((K-1)//s)*ssum temp ((K-1)//s)*ssum (K-1)//s K-1 K 1 s ssum k=K-((K-1)//s)*s k K-((K-1)//s)*s K ((K-1)//s)*s (K-1)//s K-1 K 1 s s k=min(K,s) k min(K,s) min K s kmax=-(10**300) kmax -(10**300) 10**300 10 300 for ii in range(s):
    tempscore=temp
    for jj in range(1,k+1):
      tempscore+=C[l[(ii+jj)%s]]
      if tempscore>kmax:
        kmax=tempscore
   ii range(s) range s tempscore=temp tempscore temp for jj in range(1,k+1):
      tempscore+=C[l[(ii+jj)%s]]
      if tempscore>kmax:
        kmax=tempscore
   jj range(1,k+1) range 1 k+1 k 1 tempscore+=C[l[(ii+jj)%s]] tempscore C[l[(ii+jj)%s]] C l[(ii+jj)%s] l (ii+jj)%s ii+jj ii jj s if tempscore>kmax:
        kmax=tempscore
   tempscore>kmax tempscore kmax kmax=tempscore kmax tempscore if kmax>ans:
    ans=kmax kmax>ans kmax ans ans=kmax ans kmax print(ans) print ans k=K-((K-1)//s)*s K-((K-1)//s)*s k kmax=tempscore tempscore kmax ans=kmax kmax ans k=0 0 k kmax=-(10**300) -(10**300) kmax s=len(l) len(l) s tempscore=temp temp tempscore ans=-(10**300) -(10**300) ans k=min(X-dummy) min(X-dummy) k K=map(int,input().split()) map(int,input().split()) K ssum=0 0 ssum N,K=map(int,input().split()) map(int,input().split()) N k=min(K,s) min(K,s) k loop=[] [] loop P=list(map(int,input().split())) list(map(int,input().split())) P temp=0 0 temp X=set(range(N)) set(range(N)) X l=P[l]-1 P[l]-1 l ssum+=C[l[i]] C[l[i]] ssum l=P[k]-1 P[k]-1 l C=list(map(int,input().split())) list(map(int,input().split())) C dummy=set() set() dummy a=[] [] a tempscore+=C[l[(ii+jj)%s]] C[l[(ii+jj)%s]] tempscore temp+=((K-1)//s)*ssum ((K-1)//s)*ssum temp
N,K=map(int,input().split())
P=list(map(int,input().split()))
C=list(map(int,input().split()))

dummy=set()
loop=[]
X=set(range(N))
while len(dummy)!=N:
  a=[]
  k=min(X-dummy)
  a.append(k)
  dummy.add(k)
  l=P[k]-1
  while l!=k:
    a.append(l)
    dummy.add(l)
    l=P[l]-1
  loop.append(a)

ans=-(10**300)
for l in loop:
  temp=0
  s=len(l)
  ssum=0
  k=0
  for i in range(s):
    ssum+=C[l[i]]
  if ssum>0:
    temp+=((K-1)//s)*ssum
    k=K-((K-1)//s)*s
  else:
    k=min(K,s)
  kmax=-(10**300)
  for ii in range(s):
    tempscore=temp
    for jj in range(1,k+1):
      tempscore+=C[l[(ii+jj)%s]]
      if tempscore>kmax:
        kmax=tempscore
  if kmax>ans:
    ans=kmax
print(ans)
