n, k = gets.split.map(&:to_i)
p = gets.split.map(&:to_i)
c = gets.split.map(&:to_i)
ans = c.max
a = Array.new(n + 1, 0)
h = Hash.new { |x, y| x[y] = [] }
1.upto(n) do |i|
  j = i
  if a[j].zero?
    h[j] << j
    while a[p[j - 1]].zero?
      j = p[j - 1]
      a[j] = 1
      h[i] << j
    end
  end
end
h.each do |_, x|
  if x.size.pred >= k
    (x[0..-2] + x[0..-2]).each_cons(k) do |y|
      sum = 0
      sum1 = -Float::INFINITY
      y.size.pred.times do |u|
        sum += c[y[u] - 1]
        sum1 = sum if sum1 < sum
      end
      ans = sum1 if ans < sum1
    end
  else
    (x[0..-2] + x[0..-2]).each_cons(x.size.pred) do |y|
      sum = 0
      y.each do |z|
        sum += c[z - 1]
      end
      sum *= (k / x.size.pred)
      sum2 = 0
      sum3 = -Float::INFINITY
      (k % x.size.pred).times do |u|
        sum2 += c[y[u] - 1]
        sum3 = sum2 if sum3 < sum2
      end
      ans = sum + sum3 if ans < sum + sum3
    end
  end
end
puts ans