n,k = gets.split.map(&:to_i)
p = gets.split.map(&:to_i)
c = gets.split.map(&:to_i)

max = c.max
now_index = c.find_index(max)
# p max,now_index
ary = []
ary << max

(k-1).times do
  next_index = p[now_index]
  ary << ary[-1] + c[next_index-1]
  now_index = p.find_index(p[next_index-1])
  # p next_index-1,ary,now_index
end

puts ary.max
