import sys
N,K=map(int,input().split())
plist=[0]+list(map(int,input().split()))
clist=[-float("inf")]+list(map(int,input().split()))

if max(clist)<=0:
  print(max(clist))
  sys.exit(0)
  
if K<=N:
  max_answer=0
  for i in range(1,N+1):
    pos=i
    max_score=0
    score=0
    for c in range(K):
      pos=plist[pos]
      score+=clist[pos]
      max_score=max(score,max_score)
      
    #print(i,max_score)
    max_answer=max(max_answer,max_score)
    
  print(max_answer)
  sys.exit(0)
  
max_answer=0
for i in range(1,N+1):  
  pos=i
  cycle_list=[]
  score_list=[]
  for _ in range(N):
    pos=plist[pos]
    cycle_list.append(pos)
    score_list.append(clist[pos])    
    if pos==i:
      break
      
  num_cycle=K//len(cycle_list)
  cycle_score=sum(score_list)
  #print(cycle_list,score_list,num_cycle,cycle_score)
  
  score=0
  max_score=0  
  if cycle_score>=0:
    score+=(num_cycle-1)*cycle_score
  K2=K-len(cycle_list)*(num_cycle-1)

  for _ in range(K2):
    pos=plist[pos]
    score+=clist[pos]
    max_score=max(score,max_score)
  
  #print(i,max_score)
  max_answer=max(max_answer,max_score)
  
print(max_answer) import sys sys N,K=map(int,input().split()) N map(int,input().split()) map int input().split() ().split () input split K plist=[0]+list(map(int,input().split())) plist [0]+list(map(int,input().split())) [0] 0 list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split clist=[-float("inf")]+list(map(int,input().split())) clist [-float("inf")]+list(map(int,input().split())) [-float("inf")] -float("inf") float("inf") float "inf" list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split if max(clist)<=0:
  print(max(clist))
  sys.exit(0)
   max(clist)<=0 max(clist) max clist 0 print(max(clist)) print max(clist) max clist sys.exit(0) sys.exit sys exit 0 if K<=N:
  max_answer=0
  for i in range(1,N+1):
    pos=i
    max_score=0
    score=0
    for c in range(K):
      pos=plist[pos]
      score+=clist[pos]
      max_score=max(score,max_score)
      
    #print(i,max_score)
    max_answer=max(max_answer,max_score)
    
  print(max_answer)
  sys.exit(0)
   K<=N K N max_answer=0 max_answer 0 for i in range(1,N+1):
    pos=i
    max_score=0
    score=0
    for c in range(K):
      pos=plist[pos]
      score+=clist[pos]
      max_score=max(score,max_score)
      
    #print(i,max_score)
    max_answer=max(max_answer,max_score)
    
   i range(1,N+1) range 1 N+1 N 1 pos=i pos i max_score=0 max_score 0 score=0 score 0 for c in range(K):
      pos=plist[pos]
      score+=clist[pos]
      max_score=max(score,max_score)
      
    #print(i,max_score)
     c range(K) range K pos=plist[pos] pos plist[pos] plist pos score+=clist[pos] score clist[pos] clist pos max_score=max(score,max_score) max_score max(score,max_score) max score max_score max_answer=max(max_answer,max_score) max_answer max(max_answer,max_score) max max_answer max_score print(max_answer) print max_answer sys.exit(0) sys.exit sys exit 0 max_answer=0 max_answer 0 for i in range(1,N+1):  
  pos=i
  cycle_list=[]
  score_list=[]
  for _ in range(N):
    pos=plist[pos]
    cycle_list.append(pos)
    score_list.append(clist[pos])    
    if pos==i:
      break
      
  num_cycle=K//len(cycle_list)
  cycle_score=sum(score_list)
  #print(cycle_list,score_list,num_cycle,cycle_score)
  
  score=0
  max_score=0  
  if cycle_score>=0:
    score+=(num_cycle-1)*cycle_score
  K2=K-len(cycle_list)*(num_cycle-1)

  for _ in range(K2):
    pos=plist[pos]
    score+=clist[pos]
    max_score=max(score,max_score)
  
  #print(i,max_score)
  max_answer=max(max_answer,max_score)
   i range(1,N+1) range 1 N+1 N 1 pos=i pos i cycle_list=[] cycle_list [] score_list=[] score_list [] for _ in range(N):
    pos=plist[pos]
    cycle_list.append(pos)
    score_list.append(clist[pos])    
    if pos==i:
      break
      
   _ range(N) range N pos=plist[pos] pos plist[pos] plist pos cycle_list.append(pos) cycle_list.append cycle_list append pos score_list.append(clist[pos]) score_list.append score_list append clist[pos] clist pos if pos==i:
      break
      
   pos==i pos i break num_cycle=K//len(cycle_list) num_cycle K//len(cycle_list) K len(cycle_list) len cycle_list cycle_score=sum(score_list) cycle_score sum(score_list) sum score_list score=0 score 0 max_score=0 max_score 0 if cycle_score>=0:
    score+=(num_cycle-1)*cycle_score
   cycle_score>=0 cycle_score 0 score+=(num_cycle-1)*cycle_score score (num_cycle-1)*cycle_score num_cycle-1 num_cycle 1 cycle_score K2=K-len(cycle_list)*(num_cycle-1) K2 K-len(cycle_list)*(num_cycle-1) K len(cycle_list)*(num_cycle-1) len(cycle_list) len cycle_list num_cycle-1 num_cycle 1 for _ in range(K2):
    pos=plist[pos]
    score+=clist[pos]
    max_score=max(score,max_score)
  
  #print(i,max_score)
   _ range(K2) range K2 pos=plist[pos] pos plist[pos] plist pos score+=clist[pos] score clist[pos] clist pos max_score=max(score,max_score) max_score max(score,max_score) max score max_score max_answer=max(max_answer,max_score) max_answer max(max_answer,max_score) max max_answer max_score print(max_answer) print max_answer plist=[0]+list(map(int,input().split())) [0]+list(map(int,input().split())) plist max_answer=max(max_answer,max_score) max(max_answer,max_score) max_answer pos=plist[pos] plist[pos] pos pos=i i pos pos=i i pos max_answer=0 0 max_answer pos=plist[pos] plist[pos] pos cycle_list=[] [] cycle_list cycle_score=sum(score_list) sum(score_list) cycle_score max_answer=max(max_answer,max_score) max(max_answer,max_score) max_answer score+=clist[pos] clist[pos] score max_score=0 0 max_score score+=(num_cycle-1)*cycle_score (num_cycle-1)*cycle_score score max_answer=0 0 max_answer score_list=[] [] score_list pos=plist[pos] plist[pos] pos num_cycle=K//len(cycle_list) K//len(cycle_list) num_cycle N,K=map(int,input().split()) map(int,input().split()) N max_score=max(score,max_score) max(score,max_score) max_score score=0 0 score score+=clist[pos] clist[pos] score max_score=0 0 max_score K=map(int,input().split()) map(int,input().split()) K score=0 0 score max_score=max(score,max_score) max(score,max_score) max_score clist=[-float("inf")]+list(map(int,input().split())) [-float("inf")]+list(map(int,input().split())) clist K2=K-len(cycle_list)*(num_cycle-1) K-len(cycle_list)*(num_cycle-1) K2
import sys
N,K=map(int,input().split())
plist=[0]+list(map(int,input().split()))
clist=[-float("inf")]+list(map(int,input().split()))

if max(clist)<=0:
  print(max(clist))
  sys.exit(0)
  
if K<=N:
  max_answer=0
  for i in range(1,N+1):
    pos=i
    max_score=0
    score=0
    for c in range(K):
      pos=plist[pos]
      score+=clist[pos]
      max_score=max(score,max_score)
      
    #print(i,max_score)
    max_answer=max(max_answer,max_score)
    
  print(max_answer)
  sys.exit(0)
  
max_answer=0
for i in range(1,N+1):  
  pos=i
  cycle_list=[]
  score_list=[]
  for _ in range(N):
    pos=plist[pos]
    cycle_list.append(pos)
    score_list.append(clist[pos])    
    if pos==i:
      break
      
  num_cycle=K//len(cycle_list)
  cycle_score=sum(score_list)
  #print(cycle_list,score_list,num_cycle,cycle_score)
  
  score=0
  max_score=0  
  if cycle_score>=0:
    score+=(num_cycle-1)*cycle_score
  K2=K-len(cycle_list)*(num_cycle-1)

  for _ in range(K2):
    pos=plist[pos]
    score+=clist[pos]
    max_score=max(score,max_score)
  
  #print(i,max_score)
  max_answer=max(max_answer,max_score)
  
print(max_answer)