N, K = map(int, input().split())
p_list = list(map(int, input().split()))
c_list = list(map(int, input().split()))

max_score = 0
first = True
for i in range(N):
  score = 0
  step = 0
  path = {}
  cumulative_score = []
  current_p = i
  cycle_passed = False
  while step < K:
    # record current step
    cumulative_score.append(score)
    
    # check if cycle is found
    if (not cycle_passed) and (current_p in path.keys()):
      # found a cycle 
      # calculate score sum within the cycle
      # calculate steps with in the cycle
      cycle_start_step = path[current_p]
      cycle_steps = step - cycle_start_step
      cycle_sum = cumulative_score[step] - cumulative_score[cycle_start_step]
      if cycle_sum < 0:
        # negative cycle
        # finish 
        break
      else:
        #positive cycle
        remain_steps = K - step
        if remain_steps > cycle_steps:
          cycle_times = (K - step) // cycle_steps - 1
          step += (cycle_times * cycle_steps)
          score += (cycle_times * cycle_sum)
          cycle_passed = True
        else:
          cycle_passed = True    
        
    # record current step
    path[current_p] = step
    
    # go to next step
    next_p = p_list[current_p-1]
    score += c_list[next_p-1]
    if (first):
      max_score = score
      first = False
    else:
      max_score = max(max_score, score)
    
    current_p = next_p
    step += 1

print(max_score) N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K p_list = list(map(int, input().split())) p_list list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split c_list = list(map(int, input().split())) c_list list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split max_score = 0 max_score 0 first = True first True for i in range(N):
  score = 0
  step = 0
  path = {}
  cumulative_score = []
  current_p = i
  cycle_passed = False
  while step < K:
    # record current step
    cumulative_score.append(score)
    
    # check if cycle is found
    if (not cycle_passed) and (current_p in path.keys()):
      # found a cycle 
      # calculate score sum within the cycle
      # calculate steps with in the cycle
      cycle_start_step = path[current_p]
      cycle_steps = step - cycle_start_step
      cycle_sum = cumulative_score[step] - cumulative_score[cycle_start_step]
      if cycle_sum < 0:
        # negative cycle
        # finish 
        break
      else:
        #positive cycle
        remain_steps = K - step
        if remain_steps > cycle_steps:
          cycle_times = (K - step) // cycle_steps - 1
          step += (cycle_times * cycle_steps)
          score += (cycle_times * cycle_sum)
          cycle_passed = True
        else:
          cycle_passed = True    
        
    # record current step
    path[current_p] = step
    
    # go to next step
    next_p = p_list[current_p-1]
    score += c_list[next_p-1]
    if (first):
      max_score = score
      first = False
    else:
      max_score = max(max_score, score)
    
    current_p = next_p
    step += 1 i range(N) range N score = 0 score 0 step = 0 step 0 path = {} path {} cumulative_score = [] cumulative_score [] current_p = i current_p i cycle_passed = False cycle_passed False while step < K:
    # record current step
    cumulative_score.append(score)
    
    # check if cycle is found
    if (not cycle_passed) and (current_p in path.keys()):
      # found a cycle 
      # calculate score sum within the cycle
      # calculate steps with in the cycle
      cycle_start_step = path[current_p]
      cycle_steps = step - cycle_start_step
      cycle_sum = cumulative_score[step] - cumulative_score[cycle_start_step]
      if cycle_sum < 0:
        # negative cycle
        # finish 
        break
      else:
        #positive cycle
        remain_steps = K - step
        if remain_steps > cycle_steps:
          cycle_times = (K - step) // cycle_steps - 1
          step += (cycle_times * cycle_steps)
          score += (cycle_times * cycle_sum)
          cycle_passed = True
        else:
          cycle_passed = True    
        
    # record current step
    path[current_p] = step
    
    # go to next step
    next_p = p_list[current_p-1]
    score += c_list[next_p-1]
    if (first):
      max_score = score
      first = False
    else:
      max_score = max(max_score, score)
    
    current_p = next_p
    step += 1 step < K step K cumulative_score.append(score) cumulative_score.append cumulative_score append score if (not cycle_passed) and (current_p in path.keys()):
      # found a cycle 
      # calculate score sum within the cycle
      # calculate steps with in the cycle
      cycle_start_step = path[current_p]
      cycle_steps = step - cycle_start_step
      cycle_sum = cumulative_score[step] - cumulative_score[cycle_start_step]
      if cycle_sum < 0:
        # negative cycle
        # finish 
        break
      else:
        #positive cycle
        remain_steps = K - step
        if remain_steps > cycle_steps:
          cycle_times = (K - step) // cycle_steps - 1
          step += (cycle_times * cycle_steps)
          score += (cycle_times * cycle_sum)
          cycle_passed = True
        else:
          cycle_passed = True    
        
    # record current step
     (not cycle_passed) and (current_p in path.keys()) not cycle_passed cycle_passed current_p in path.keys() current_p path.keys() path.keys path keys cycle_start_step = path[current_p] cycle_start_step path[current_p] path current_p cycle_steps = step - cycle_start_step cycle_steps step - cycle_start_step step cycle_start_step cycle_sum = cumulative_score[step] - cumulative_score[cycle_start_step] cycle_sum cumulative_score[step] - cumulative_score[cycle_start_step] cumulative_score[step] cumulative_score step cumulative_score[cycle_start_step] cumulative_score cycle_start_step if cycle_sum < 0:
        # negative cycle
        # finish 
        break
      else:
        #positive cycle
        remain_steps = K - step
        if remain_steps > cycle_steps:
          cycle_times = (K - step) // cycle_steps - 1
          step += (cycle_times * cycle_steps)
          score += (cycle_times * cycle_sum)
          cycle_passed = True
        else:
          cycle_passed = True    
        
    # record current step
     cycle_sum < 0 cycle_sum 0 break remain_steps = K - step remain_steps K - step K step if remain_steps > cycle_steps:
          cycle_times = (K - step) // cycle_steps - 1
          step += (cycle_times * cycle_steps)
          score += (cycle_times * cycle_sum)
          cycle_passed = True
        else:
          cycle_passed = True    
        
    # record current step
     remain_steps > cycle_steps remain_steps cycle_steps cycle_times = (K - step) // cycle_steps - 1 cycle_times (K - step) // cycle_steps - 1 (K - step) // cycle_steps K - step K step cycle_steps 1 step += (cycle_times * cycle_steps) step cycle_times * cycle_steps cycle_times cycle_steps score += (cycle_times * cycle_sum) score cycle_times * cycle_sum cycle_times cycle_sum cycle_passed = True cycle_passed True cycle_passed = True cycle_passed True path[current_p] = step path[current_p] path current_p step next_p = p_list[current_p-1] next_p p_list[current_p-1] p_list current_p-1 current_p 1 score += c_list[next_p-1] score c_list[next_p-1] c_list next_p-1 next_p 1 if (first):
      max_score = score
      first = False
    else:
      max_score = max(max_score, score)
    
     first max_score = score max_score score first = False first False max_score = max(max_score, score) max_score max(max_score, score) max max_score score current_p = next_p current_p next_p step += 1 step 1 print(max_score) print max_score step += (cycle_times * cycle_steps) cycle_times * cycle_steps step cycle_passed = True True cycle_passed cycle_passed = True True cycle_passed N, K = map(int, input().split()) map(int, input().split()) N p_list = list(map(int, input().split())) list(map(int, input().split())) p_list first = False False first score = 0 0 score max_score = max(max_score, score) max(max_score, score) max_score first = True True first step += 1 1 step current_p = i i current_p score += c_list[next_p-1] c_list[next_p-1] score cycle_sum = cumulative_score[step] - cumulative_score[cycle_start_step] cumulative_score[step] - cumulative_score[cycle_start_step] cycle_sum path = {} {} path score += (cycle_times * cycle_sum) cycle_times * cycle_sum score remain_steps = K - step K - step remain_steps max_score = score score max_score K = map(int, input().split()) map(int, input().split()) K c_list = list(map(int, input().split())) list(map(int, input().split())) c_list cumulative_score = [] [] cumulative_score next_p = p_list[current_p-1] p_list[current_p-1] next_p step = 0 0 step cycle_times = (K - step) // cycle_steps - 1 (K - step) // cycle_steps - 1 cycle_times max_score = 0 0 max_score cycle_steps = step - cycle_start_step step - cycle_start_step cycle_steps cycle_start_step = path[current_p] path[current_p] cycle_start_step cycle_passed = False False cycle_passed current_p = next_p next_p current_p
N, K = map(int, input().split())
p_list = list(map(int, input().split()))
c_list = list(map(int, input().split()))

max_score = 0
first = True
for i in range(N):
  score = 0
  step = 0
  path = {}
  cumulative_score = []
  current_p = i
  cycle_passed = False
  while step < K:
    # record current step
    cumulative_score.append(score)
    
    # check if cycle is found
    if (not cycle_passed) and (current_p in path.keys()):
      # found a cycle 
      # calculate score sum within the cycle
      # calculate steps with in the cycle
      cycle_start_step = path[current_p]
      cycle_steps = step - cycle_start_step
      cycle_sum = cumulative_score[step] - cumulative_score[cycle_start_step]
      if cycle_sum < 0:
        # negative cycle
        # finish 
        break
      else:
        #positive cycle
        remain_steps = K - step
        if remain_steps > cycle_steps:
          cycle_times = (K - step) // cycle_steps - 1
          step += (cycle_times * cycle_steps)
          score += (cycle_times * cycle_sum)
          cycle_passed = True
        else:
          cycle_passed = True    
        
    # record current step
    path[current_p] = step
    
    # go to next step
    next_p = p_list[current_p-1]
    score += c_list[next_p-1]
    if (first):
      max_score = score
      first = False
    else:
      max_score = max(max_score, score)
    
    current_p = next_p
    step += 1

print(max_score)