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

# p_arrの中でループするものに分割
loop_arr = []
s = []
p_arr.each do |pi|
  ind = pi - 1
  next if s.include?(ind)
  parr = []
  carr = []
  n.times do
    break if s.include?(ind)
    s << ind
    parr << parr.length + 1
    carr << c_arr[ind]
    ind = p_arr[ind] - 1
  end
  parr[-1] = 0
  loop_arr << [parr, carr]
end

max_score = -1_000_000_000
# ループごとに最大値を求める
loop_arr.each do |parr, carr|
  m = parr.length
  k1 = k % m + m
  r = k / m - 1
  sum = carr.sum
  add = sum > 0 ? sum * r : 0

  max = -1_000_000_000
  m.times do |i|
    ind = i
    score = 0
    k1.times do
      ind = parr[ind]
      score += carr[ind]
      max = score if max < score
    end
  end
  max += add
  max_score = max if max_score < max
end
puts max_score
