n, k = gets.split(' ').map(&:to_i)
p_array = [0] + gets.split(' ').map(&:to_i)
c_array = [0] + gets.split(' ').map(&:to_i)

ans = (10**9 + 1)*(-1)
points_memo = Array.new(k+1, 0)

(1..n).each do |i|
  next_point = i
  (1..k).each do |j|
    next_point = p_array[next_point]
    points_memo[j] = c_array[next_point] + points_memo[j - 1]
  end
  ans = points_memo.drop(1).max if ans < points_memo.drop(1).max
end

puts ans