from math import log2
n,k=map(int,input().split())
p=list(map(int,input().split()))
c=list(map(int,input().split()))

k-=1

m=int(log2(k)) if 1<k else 1
nxt=[[0]*n for _ in range(m+1)]
point=[[0]*n for _ in range(m+1)]
for i in range(n):
  p[i]-=1
  nxt[0][i]=p[i]
  point[0][i]=c[p[i]]
for j in range(m):
  for i in range(n):
    nxt[j+1][i]=nxt[j][nxt[j][i]]
    point[j+1][i]=point[j][i]+point[j][nxt[j][i]]
min_nk=min(n,k)
ans=-10**18
for s in range(n):
  pla=p[s]
  cur=c[pla]
  if ans<cur:
    ans=cur
  for _ in range(min_nk):
    pla=p[pla]
    cur+=c[pla]
    if ans<cur:
      ans=cur
  if n<k:
    pla=p[s]
    cur=c[pla]
    for i in range(m+1):
      if (k-n)&(1<<i):
        cur+=point[i][pla]
        pla=nxt[i][pla]
    res=cur
    for _ in range(n):
      pla=p[pla]
      cur+=c[pla]
      if res<cur:
        res=cur
    if ans<res:
      ans=res
print(ans) from math import log2 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 k-=1 k 1 m=int(log2(k)) if 1<k else 1 m int(log2(k)) if 1<k else 1 1<k 1 k int(log2(k)) int log2(k) log2 k 1 [0]*n for _ in range(m+1) for _ in range(m+1) _ range(m+1) range m+1 m 1 for _ in range(m+1) [0]*n [0] 0 n nxt=[[0]*n for _ in range(m+1)] nxt [[0]*n for _ in range(m+1)] [0]*n for _ in range(m+1) for _ in range(m+1) _ range(m+1) range m+1 m 1 for _ in range(m+1) [0]*n [0] 0 n point=[[0]*n for _ in range(m+1)] point [[0]*n for _ in range(m+1)] for i in range(n):
  p[i]-=1
  nxt[0][i]=p[i]
  point[0][i]=c[p[i]] i range(n) range n p[i]-=1 p[i] p i 1 nxt[0][i]=p[i] nxt[0][i] [0] nxt 0 i p[i] p i point[0][i]=c[p[i]] point[0][i] [0] point 0 i c[p[i]] c p[i] p i for j in range(m):
  for i in range(n):
    nxt[j+1][i]=nxt[j][nxt[j][i]]
    point[j+1][i]=point[j][i]+point[j][nxt[j][i]] j range(m) range m for i in range(n):
    nxt[j+1][i]=nxt[j][nxt[j][i]]
    point[j+1][i]=point[j][i]+point[j][nxt[j][i]] i range(n) range n nxt[j+1][i]=nxt[j][nxt[j][i]] nxt[j+1][i] [j+1] nxt j+1 j 1 i nxt[j][nxt[j][i]] [j] nxt j nxt[j][i] [j] nxt j i point[j+1][i]=point[j][i]+point[j][nxt[j][i]] point[j+1][i] [j+1] point j+1 j 1 i point[j][i]+point[j][nxt[j][i]] point[j][i] [j] point j i point[j][nxt[j][i]] [j] point j nxt[j][i] [j] nxt j i min_nk=min(n,k) min_nk min(n,k) min n k ans=-10**18 ans -10**18 -10 18 for s in range(n):
  pla=p[s]
  cur=c[pla]
  if ans<cur:
    ans=cur
  for _ in range(min_nk):
    pla=p[pla]
    cur+=c[pla]
    if ans<cur:
      ans=cur
  if n<k:
    pla=p[s]
    cur=c[pla]
    for i in range(m+1):
      if (k-n)&(1<<i):
        cur+=point[i][pla]
        pla=nxt[i][pla]
    res=cur
    for _ in range(n):
      pla=p[pla]
      cur+=c[pla]
      if res<cur:
        res=cur
    if ans<res:
      ans=res s range(n) range n pla=p[s] pla p[s] p s cur=c[pla] cur c[pla] c pla if ans<cur:
    ans=cur
   ans<cur ans cur ans=cur ans cur for _ in range(min_nk):
    pla=p[pla]
    cur+=c[pla]
    if ans<cur:
      ans=cur
   _ range(min_nk) range min_nk pla=p[pla] pla p[pla] p pla cur+=c[pla] cur c[pla] c pla if ans<cur:
      ans=cur
   ans<cur ans cur ans=cur ans cur if n<k:
    pla=p[s]
    cur=c[pla]
    for i in range(m+1):
      if (k-n)&(1<<i):
        cur+=point[i][pla]
        pla=nxt[i][pla]
    res=cur
    for _ in range(n):
      pla=p[pla]
      cur+=c[pla]
      if res<cur:
        res=cur
    if ans<res:
      ans=res n<k n k pla=p[s] pla p[s] p s cur=c[pla] cur c[pla] c pla for i in range(m+1):
      if (k-n)&(1<<i):
        cur+=point[i][pla]
        pla=nxt[i][pla]
     i range(m+1) range m+1 m 1 if (k-n)&(1<<i):
        cur+=point[i][pla]
        pla=nxt[i][pla]
     (k-n)&(1<<i) k-n k n 1<<i 1 i cur+=point[i][pla] cur point[i][pla] [i] point i pla pla=nxt[i][pla] pla nxt[i][pla] [i] nxt i pla res=cur res cur for _ in range(n):
      pla=p[pla]
      cur+=c[pla]
      if res<cur:
        res=cur
     _ range(n) range n pla=p[pla] pla p[pla] p pla cur+=c[pla] cur c[pla] c pla if res<cur:
        res=cur
     res<cur res cur res=cur res cur if ans<res:
      ans=res ans<res ans res ans=res ans res print(ans) print ans point=[[0]*n for _ in range(m+1)] [[0]*n for _ in range(m+1)] point c=list(map(int,input().split())) list(map(int,input().split())) c pla=p[pla] p[pla] pla ans=cur cur ans ans=-10**18 -10**18 ans cur+=point[i][pla] point[i][pla] cur min_nk=min(n,k) min(n,k) min_nk ans=cur cur ans cur=c[pla] c[pla] cur res=cur cur res cur=c[pla] c[pla] cur pla=p[s] p[s] pla cur+=c[pla] c[pla] cur cur+=c[pla] c[pla] cur m=int(log2(k)) if 1<k else 1 int(log2(k)) if 1<k else 1 m nxt=[[0]*n for _ in range(m+1)] [[0]*n for _ in range(m+1)] nxt n,k=map(int,input().split()) map(int,input().split()) n k-=1 1 k ans=res res ans k=map(int,input().split()) map(int,input().split()) k pla=p[pla] p[pla] pla p=list(map(int,input().split())) list(map(int,input().split())) p pla=nxt[i][pla] nxt[i][pla] pla pla=p[s] p[s] pla res=cur cur res
from math import log2
n,k=map(int,input().split())
p=list(map(int,input().split()))
c=list(map(int,input().split()))

k-=1

m=int(log2(k)) if 1<k else 1
nxt=[[0]*n for _ in range(m+1)]
point=[[0]*n for _ in range(m+1)]
for i in range(n):
  p[i]-=1
  nxt[0][i]=p[i]
  point[0][i]=c[p[i]]
for j in range(m):
  for i in range(n):
    nxt[j+1][i]=nxt[j][nxt[j][i]]
    point[j+1][i]=point[j][i]+point[j][nxt[j][i]]
min_nk=min(n,k)
ans=-10**18
for s in range(n):
  pla=p[s]
  cur=c[pla]
  if ans<cur:
    ans=cur
  for _ in range(min_nk):
    pla=p[pla]
    cur+=c[pla]
    if ans<cur:
      ans=cur
  if n<k:
    pla=p[s]
    cur=c[pla]
    for i in range(m+1):
      if (k-n)&(1<<i):
        cur+=point[i][pla]
        pla=nxt[i][pla]
    res=cur
    for _ in range(n):
      pla=p[pla]
      cur+=c[pla]
      if res<cur:
        res=cur
    if ans<res:
      ans=res
print(ans)