n, k = gets.split.map(&:to_i)
p_list = gets.split.map(&:to_i)
c_list = gets.split.map(&:to_i)

result = []
p_list.each do |p|
  sum = 0
  p_index = p
  k.times do
    sum += c_list[p_index - 1]
    result << sum
    p_index = p_list[p_index - 1]
  end
end
p result.max
