# 5 2           n k
# 2 4 5 1 3     p
# 3 4 -10 -8 8  c
n, k = gets.split.map(&:to_i)
array_p = gets.split.map(&:to_i)
array_c = gets.split.map(&:to_i)

score = -10**9
n.times do |i|
  score_temp = -10**9
  score_temp_temp = 0
  next_p = 0
  k.times do |j|
    if j == 0
      score_temp_temp = array_c[i]
      next_p = array_p[i] - 1
    else
      score_temp_temp += array_c[next_p]
      next_p = array_p[next_p] - 1
    end
    score_temp = [score_temp, score_temp_temp].max
  end
  score = [score, score_temp].max
end

puts score

