n, k = gets.split.map(&:to_i)
pieces = gets.split.map(&:to_i).map(&:pred)
nums = gets.split.map(&:to_i)
if nums.select { |num| num >= 0 }.size == 0
  puts nums.max
  exit
end
orders = []
checked = Hash.new(false)
answers = []
n.times do |from|
  next if checked[from]
  scores = [nums[from]]
  current = pieces[from]
  checked[from] = true
  while current != from
    scores << nums[current]
    checked[current] = true
    current = pieces[current]
  end
  sum = scores.sum
  div, mod = k.divmod(scores.size)
  tmp = sum > 0 ? sum * div : 0
  addition = 0
  if tmp == 0 && div > 0
    mod = scores.size - 1
  end
  scores_size = scores.size
  # puts "#{tmp} #{div} #{mod} #{sum}"
  # (1..mod).each do |i|
    i = mod
    scores_size.times do |f|
      applicables = []
      i.times do |a|
        applicables << (f + a) % scores_size
      end
      tm = 0
      applicables.each do |i|
        tm += scores[i]
        addition = tm if addition < tm
      end
      # addition = tm if addition < tm
    end
  # end
  answers << tmp + addition
end
puts answers.max
