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

ans = -10000000000

n.times do |i|
    score = 0
    p_index = i
    k.times do |j|
        c_index = p_array[p_index] - 1
        score = score + c_array[c_index]
        ans = score if ans < score
        p_index = c_index
    end
end

puts ans
