require "set"
n,k = gets.chomp.split(" ").map(&:to_i)
p_arr = [0] + gets.chomp.split(" ").map(&:to_i)
c_arr = [0] + gets.chomp.split(" ").map(&:to_i)

arr = []
set = Set.new
(1..n).each do |i|
  next if set.include?(i)
  p = i
  tmp_arr = []
  tmp_set = Set.new
  while true do
    break if tmp_set.include?(p)
    tmp_set << p
    set << p
    p_next = p_arr[p]
    tmp_arr << c_arr[p_next]
    p = p_next
  end
  arr << tmp_arr
end

max = (10 ** 9) * (-1)
arr.each do |tmp_cycle|
  cycle = tmp_cycle + tmp_cycle
  (0..tmp_cycle.length-1).each do |i|
    l = [tmp_cycle.length, k].min
    tmp_max = (10 ** 9) * (-1)
    tmp_sum = 0
    l.times do |j|
      tmp_sum += cycle[i+j]
      tmp_max = tmp_sum if tmp_sum > tmp_max
    end
    if l == k
      max = tmp_max if tmp_max > max
    else
      l2 = (k / tmp_cycle.length)
      r = k % tmp_cycle.length
      tmp_sum2 = tmp_sum * l2
      tmp_max = tmp_sum2 if tmp_sum2 > tmp_max
      r.times do |j2|
        tmp_sum2 += cycle[i+j2]
        tmp_max = tmp_sum2 if tmp_sum2 > tmp_max
      end
      max = tmp_max if tmp_max > max
    end
  end
end

puts max
