n,k=map(int,input().split())
p=list(map(int,input().split()))
c=list(map(int,input().split()))
ttl=[]
cnt=[]
count=0
for i in range(n):
  nxt=i+1
  grp=[]
  count=0
  pnt=[]
  pnt.append(0)
  for j in range(n):
    nxt=p[nxt-1]
    pnt.append(pnt[j]+c[nxt-1])
    count+=1
    if nxt==i+1:
      del pnt[0]
      ttl.append(pnt)
      cnt.append(count)
      break
ans=[]
for i in range(n):
  peak=ttl[i].index(max(ttl[i]))
  if k<=cnt[i]:
    ans.append(max(ttl[i][:k]))
  else:
    if ttl[i][-1]<0:
      ans.append(ttl[i][peak])
    else:
      if k%cnt[i]==0:
        ans.append(((k-peak-1)//cnt[i])*ttl[i][-1]+ttl[i][peak])
      else:
        ans.append(max((k//cnt[i])*ttl[i][-1]+max(ttl[i][0:k%cnt[i]]),((k-peak-1)//cnt[i])*ttl[i][-1]+ttl[i][peak]))
print(max(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 ttl=[] ttl [] cnt=[] cnt [] count=0 count 0 for i in range(n):
  nxt=i+1
  grp=[]
  count=0
  pnt=[]
  pnt.append(0)
  for j in range(n):
    nxt=p[nxt-1]
    pnt.append(pnt[j]+c[nxt-1])
    count+=1
    if nxt==i+1:
      del pnt[0]
      ttl.append(pnt)
      cnt.append(count)
      break i range(n) range n nxt=i+1 nxt i+1 i 1 grp=[] grp [] count=0 count 0 pnt=[] pnt [] pnt.append(0) pnt.append pnt append 0 for j in range(n):
    nxt=p[nxt-1]
    pnt.append(pnt[j]+c[nxt-1])
    count+=1
    if nxt==i+1:
      del pnt[0]
      ttl.append(pnt)
      cnt.append(count)
      break j range(n) range n nxt=p[nxt-1] nxt p[nxt-1] p nxt-1 nxt 1 pnt.append(pnt[j]+c[nxt-1]) pnt.append pnt append pnt[j]+c[nxt-1] pnt[j] pnt j c[nxt-1] c nxt-1 nxt 1 count+=1 count 1 if nxt==i+1:
      del pnt[0]
      ttl.append(pnt)
      cnt.append(count)
      break nxt==i+1 nxt i+1 i 1 del pnt[0] pnt[0] pnt 0 ttl.append(pnt) ttl.append ttl append pnt cnt.append(count) cnt.append cnt append count break ans=[] ans [] for i in range(n):
  peak=ttl[i].index(max(ttl[i]))
  if k<=cnt[i]:
    ans.append(max(ttl[i][:k]))
  else:
    if ttl[i][-1]<0:
      ans.append(ttl[i][peak])
    else:
      if k%cnt[i]==0:
        ans.append(((k-peak-1)//cnt[i])*ttl[i][-1]+ttl[i][peak])
      else:
        ans.append(max((k//cnt[i])*ttl[i][-1]+max(ttl[i][0:k%cnt[i]]),((k-peak-1)//cnt[i])*ttl[i][-1]+ttl[i][peak])) i range(n) range n peak=ttl[i].index(max(ttl[i])) peak ttl[i].index(max(ttl[i])) [i].index [i] ttl i index max(ttl[i]) max ttl[i] ttl i if k<=cnt[i]:
    ans.append(max(ttl[i][:k]))
  else:
    if ttl[i][-1]<0:
      ans.append(ttl[i][peak])
    else:
      if k%cnt[i]==0:
        ans.append(((k-peak-1)//cnt[i])*ttl[i][-1]+ttl[i][peak])
      else:
        ans.append(max((k//cnt[i])*ttl[i][-1]+max(ttl[i][0:k%cnt[i]]),((k-peak-1)//cnt[i])*ttl[i][-1]+ttl[i][peak])) k<=cnt[i] k cnt[i] cnt i ans.append(max(ttl[i][:k])) ans.append ans append max(ttl[i][:k]) max ttl[i][:k] [i] ttl i :k k if ttl[i][-1]<0:
      ans.append(ttl[i][peak])
    else:
      if k%cnt[i]==0:
        ans.append(((k-peak-1)//cnt[i])*ttl[i][-1]+ttl[i][peak])
      else:
        ans.append(max((k//cnt[i])*ttl[i][-1]+max(ttl[i][0:k%cnt[i]]),((k-peak-1)//cnt[i])*ttl[i][-1]+ttl[i][peak])) ttl[i][-1]<0 ttl[i][-1] [i] ttl i -1 0 ans.append(ttl[i][peak]) ans.append ans append ttl[i][peak] [i] ttl i peak if k%cnt[i]==0:
        ans.append(((k-peak-1)//cnt[i])*ttl[i][-1]+ttl[i][peak])
      else:
        ans.append(max((k//cnt[i])*ttl[i][-1]+max(ttl[i][0:k%cnt[i]]),((k-peak-1)//cnt[i])*ttl[i][-1]+ttl[i][peak])) k%cnt[i]==0 k%cnt[i] k cnt[i] cnt i 0 ans.append(((k-peak-1)//cnt[i])*ttl[i][-1]+ttl[i][peak]) ans.append ans append ((k-peak-1)//cnt[i])*ttl[i][-1]+ttl[i][peak] ((k-peak-1)//cnt[i])*ttl[i][-1] (k-peak-1)//cnt[i] k-peak-1 k-peak k peak 1 cnt[i] cnt i ttl[i][-1] [i] ttl i -1 ttl[i][peak] [i] ttl i peak ans.append(max((k//cnt[i])*ttl[i][-1]+max(ttl[i][0:k%cnt[i]]),((k-peak-1)//cnt[i])*ttl[i][-1]+ttl[i][peak])) ans.append ans append max((k//cnt[i])*ttl[i][-1]+max(ttl[i][0:k%cnt[i]]),((k-peak-1)//cnt[i])*ttl[i][-1]+ttl[i][peak]) max (k//cnt[i])*ttl[i][-1]+max(ttl[i][0:k%cnt[i]]) (k//cnt[i])*ttl[i][-1] k//cnt[i] k cnt[i] cnt i ttl[i][-1] [i] ttl i -1 max(ttl[i][0:k%cnt[i]]) max ttl[i][0:k%cnt[i]] [i] ttl i 0:k%cnt[i] 0 k%cnt[i] k cnt[i] cnt i ((k-peak-1)//cnt[i])*ttl[i][-1]+ttl[i][peak] ((k-peak-1)//cnt[i])*ttl[i][-1] (k-peak-1)//cnt[i] k-peak-1 k-peak k peak 1 cnt[i] cnt i ttl[i][-1] [i] ttl i -1 ttl[i][peak] [i] ttl i peak print(max(ans)) print max(ans) max ans nxt=i+1 i+1 nxt count=0 0 count nxt=p[nxt-1] p[nxt-1] nxt c=list(map(int,input().split())) list(map(int,input().split())) c n,k=map(int,input().split()) map(int,input().split()) n peak=ttl[i].index(max(ttl[i])) ttl[i].index(max(ttl[i])) peak p=list(map(int,input().split())) list(map(int,input().split())) p count=0 0 count cnt=[] [] cnt grp=[] [] grp count+=1 1 count ttl=[] [] ttl pnt=[] [] pnt ans=[] [] 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()))
ttl=[]
cnt=[]
count=0
for i in range(n):
  nxt=i+1
  grp=[]
  count=0
  pnt=[]
  pnt.append(0)
  for j in range(n):
    nxt=p[nxt-1]
    pnt.append(pnt[j]+c[nxt-1])
    count+=1
    if nxt==i+1:
      del pnt[0]
      ttl.append(pnt)
      cnt.append(count)
      break
ans=[]
for i in range(n):
  peak=ttl[i].index(max(ttl[i]))
  if k<=cnt[i]:
    ans.append(max(ttl[i][:k]))
  else:
    if ttl[i][-1]<0:
      ans.append(ttl[i][peak])
    else:
      if k%cnt[i]==0:
        ans.append(((k-peak-1)//cnt[i])*ttl[i][-1]+ttl[i][peak])
      else:
        ans.append(max((k//cnt[i])*ttl[i][-1]+max(ttl[i][0:k%cnt[i]]),((k-peak-1)//cnt[i])*ttl[i][-1]+ttl[i][peak]))
print(max(ans))