n, k = gets.split.map &:to_i
ps = gets.split.map &:to_i
cs = gets.split.map &:to_i

max_score = -4_000_000_000
dp = [[0] * n]
k.times do |i|
  dp << [0] * n
  n.times do |j|
    p_next = ps[j] - 1
    score = dp[i][j] + cs[p_next]
    dp[i+1][p_next] = score
    max_score = [max_score, score].max
  end
end

p max_score
