n, k = gets.split.map(&:to_i)
p_list = gets.split.map(&:to_i)
c_list = gets.split.map(&:to_i)
score = []
p_list.each do |p|
  p_index = p
  sum = 0
  k.times do
    sum += c_list[p_index - 1]
    p_index = p_list[p_index - 1]
    score << sum
  end
end
p score.max