n, k = gets.split(" ").map(&:to_i)
p_arr = gets.split(" ").map(&:to_i)
c_arr = gets.split(" ").map(&:to_i)

max_score = -1_000_000_000
k1 = k % n + 1
r = k / n + k
sum = c_arr.sum
add = sum > 0 ? sum * r : 0

n.times do |i|
  score = 0
  score_arr = []
  
  ind = i
  k1.times do
    ind = p_arr[ind] - 1
    score += c_arr[ind]
    p score
    max_score = score if max_score < score
  end
end

puts max_score + add
