n,k=map(int,input().split())
p=list(map(int,input().split()))
c=list(map(int,input().split()))

ans=-float('inf')
for i in range(n):
  T=1
  index=p[i]-1
  T_score=c[index]
  flag=True
  while flag:
    T+=1
    index=p[index]-1
    T_score+=c[index]
    if index==i:
      flag=False
  #場合分け開始
  if T>=k:
    score_list=[c[p[i]-1]]+[0]*(k-1)
    index=p[i]-1
    for s in range(1,k):
      index=p[index]-1
      score_list[s]=score_list[s-1]+c[index]
    score=max(score_list)
  else:
    if T_score<=0:
      score_list=[c[p[i]-1]]+[0]*(T-1)
      index=p[i]-1
      for s in range(1,T):       
        index=p[index]-1
        score_list[s]=score_list[s-1]+c[index]        
      score=max(score_list)
    else:
      m1=k//T
      m2=k%T
      score_list=[c[p[i]-1]]+[0]*(T+m2-1)
      index=p[i]-1
      for s in range(1,T+m2):
        index=p[index]-1
        score_list[s]=score_list[s-1]+c[index]
      score=max(score_list)+T_score*(m1-1)
  ans=max(ans,score)
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 ans=-float('inf') ans -float('inf') float('inf') float 'inf' for i in range(n):
  T=1
  index=p[i]-1
  T_score=c[index]
  flag=True
  while flag:
    T+=1
    index=p[index]-1
    T_score+=c[index]
    if index==i:
      flag=False
  #場合分け開始
  if T>=k:
    score_list=[c[p[i]-1]]+[0]*(k-1)
    index=p[i]-1
    for s in range(1,k):
      index=p[index]-1
      score_list[s]=score_list[s-1]+c[index]
    score=max(score_list)
  else:
    if T_score<=0:
      score_list=[c[p[i]-1]]+[0]*(T-1)
      index=p[i]-1
      for s in range(1,T):       
        index=p[index]-1
        score_list[s]=score_list[s-1]+c[index]        
      score=max(score_list)
    else:
      m1=k//T
      m2=k%T
      score_list=[c[p[i]-1]]+[0]*(T+m2-1)
      index=p[i]-1
      for s in range(1,T+m2):
        index=p[index]-1
        score_list[s]=score_list[s-1]+c[index]
      score=max(score_list)+T_score*(m1-1)
  ans=max(ans,score) i range(n) range n T=1 T 1 index=p[i]-1 index p[i]-1 p[i] p i 1 T_score=c[index] T_score c[index] c index flag=True flag True while flag:
    T+=1
    index=p[index]-1
    T_score+=c[index]
    if index==i:
      flag=False
  #場合分け開始
   flag T+=1 T 1 index=p[index]-1 index p[index]-1 p[index] p index 1 T_score+=c[index] T_score c[index] c index if index==i:
      flag=False
  #場合分け開始
   index==i index i flag=False flag False if T>=k:
    score_list=[c[p[i]-1]]+[0]*(k-1)
    index=p[i]-1
    for s in range(1,k):
      index=p[index]-1
      score_list[s]=score_list[s-1]+c[index]
    score=max(score_list)
  else:
    if T_score<=0:
      score_list=[c[p[i]-1]]+[0]*(T-1)
      index=p[i]-1
      for s in range(1,T):       
        index=p[index]-1
        score_list[s]=score_list[s-1]+c[index]        
      score=max(score_list)
    else:
      m1=k//T
      m2=k%T
      score_list=[c[p[i]-1]]+[0]*(T+m2-1)
      index=p[i]-1
      for s in range(1,T+m2):
        index=p[index]-1
        score_list[s]=score_list[s-1]+c[index]
      score=max(score_list)+T_score*(m1-1)
   T>=k T k score_list=[c[p[i]-1]]+[0]*(k-1) score_list [c[p[i]-1]]+[0]*(k-1) [c[p[i]-1]] c[p[i]-1] c p[i]-1 p[i] p i 1 [0]*(k-1) [0] 0 k-1 k 1 index=p[i]-1 index p[i]-1 p[i] p i 1 for s in range(1,k):
      index=p[index]-1
      score_list[s]=score_list[s-1]+c[index]
     s range(1,k) range 1 k index=p[index]-1 index p[index]-1 p[index] p index 1 score_list[s]=score_list[s-1]+c[index] score_list[s] score_list s score_list[s-1]+c[index] score_list[s-1] score_list s-1 s 1 c[index] c index score=max(score_list) score max(score_list) max score_list if T_score<=0:
      score_list=[c[p[i]-1]]+[0]*(T-1)
      index=p[i]-1
      for s in range(1,T):       
        index=p[index]-1
        score_list[s]=score_list[s-1]+c[index]        
      score=max(score_list)
    else:
      m1=k//T
      m2=k%T
      score_list=[c[p[i]-1]]+[0]*(T+m2-1)
      index=p[i]-1
      for s in range(1,T+m2):
        index=p[index]-1
        score_list[s]=score_list[s-1]+c[index]
      score=max(score_list)+T_score*(m1-1)
   T_score<=0 T_score 0 score_list=[c[p[i]-1]]+[0]*(T-1) score_list [c[p[i]-1]]+[0]*(T-1) [c[p[i]-1]] c[p[i]-1] c p[i]-1 p[i] p i 1 [0]*(T-1) [0] 0 T-1 T 1 index=p[i]-1 index p[i]-1 p[i] p i 1 for s in range(1,T):       
        index=p[index]-1
        score_list[s]=score_list[s-1]+c[index]        
       s range(1,T) range 1 T index=p[index]-1 index p[index]-1 p[index] p index 1 score_list[s]=score_list[s-1]+c[index] score_list[s] score_list s score_list[s-1]+c[index] score_list[s-1] score_list s-1 s 1 c[index] c index score=max(score_list) score max(score_list) max score_list m1=k//T m1 k//T k T m2=k%T m2 k%T k T score_list=[c[p[i]-1]]+[0]*(T+m2-1) score_list [c[p[i]-1]]+[0]*(T+m2-1) [c[p[i]-1]] c[p[i]-1] c p[i]-1 p[i] p i 1 [0]*(T+m2-1) [0] 0 T+m2-1 T+m2 T m2 1 index=p[i]-1 index p[i]-1 p[i] p i 1 for s in range(1,T+m2):
        index=p[index]-1
        score_list[s]=score_list[s-1]+c[index]
       s range(1,T+m2) range 1 T+m2 T m2 index=p[index]-1 index p[index]-1 p[index] p index 1 score_list[s]=score_list[s-1]+c[index] score_list[s] score_list s score_list[s-1]+c[index] score_list[s-1] score_list s-1 s 1 c[index] c index score=max(score_list)+T_score*(m1-1) score max(score_list)+T_score*(m1-1) max(score_list) max score_list T_score*(m1-1) T_score m1-1 m1 1 ans=max(ans,score) ans max(ans,score) max ans score print(ans) print ans ans=-float('inf') -float('inf') ans index=p[index]-1 p[index]-1 index T+=1 1 T score_list=[c[p[i]-1]]+[0]*(T-1) [c[p[i]-1]]+[0]*(T-1) score_list index=p[i]-1 p[i]-1 index flag=True True flag score=max(score_list) max(score_list) score ans=max(ans,score) max(ans,score) ans T=1 1 T index=p[i]-1 p[i]-1 index index=p[index]-1 p[index]-1 index index=p[index]-1 p[index]-1 index c=list(map(int,input().split())) list(map(int,input().split())) c score=max(score_list) max(score_list) score flag=False False flag p=list(map(int,input().split())) list(map(int,input().split())) p index=p[i]-1 p[i]-1 index score_list=[c[p[i]-1]]+[0]*(T+m2-1) [c[p[i]-1]]+[0]*(T+m2-1) score_list score=max(score_list)+T_score*(m1-1) max(score_list)+T_score*(m1-1) score n,k=map(int,input().split()) map(int,input().split()) n k=map(int,input().split()) map(int,input().split()) k T_score=c[index] c[index] T_score m1=k//T k//T m1 index=p[index]-1 p[index]-1 index T_score+=c[index] c[index] T_score m2=k%T k%T m2 score_list=[c[p[i]-1]]+[0]*(k-1) [c[p[i]-1]]+[0]*(k-1) score_list index=p[i]-1 p[i]-1 index
n,k=map(int,input().split())
p=list(map(int,input().split()))
c=list(map(int,input().split()))

ans=-float('inf')
for i in range(n):
  T=1
  index=p[i]-1
  T_score=c[index]
  flag=True
  while flag:
    T+=1
    index=p[index]-1
    T_score+=c[index]
    if index==i:
      flag=False
  #場合分け開始
  if T>=k:
    score_list=[c[p[i]-1]]+[0]*(k-1)
    index=p[i]-1
    for s in range(1,k):
      index=p[index]-1
      score_list[s]=score_list[s-1]+c[index]
    score=max(score_list)
  else:
    if T_score<=0:
      score_list=[c[p[i]-1]]+[0]*(T-1)
      index=p[i]-1
      for s in range(1,T):       
        index=p[index]-1
        score_list[s]=score_list[s-1]+c[index]        
      score=max(score_list)
    else:
      m1=k//T
      m2=k%T
      score_list=[c[p[i]-1]]+[0]*(T+m2-1)
      index=p[i]-1
      for s in range(1,T+m2):
        index=p[index]-1
        score_list[s]=score_list[s-1]+c[index]
      score=max(score_list)+T_score*(m1-1)
  ans=max(ans,score)
print(ans)