(N, K), ps_, cs_ = readlines.map{_1.split.map(&:to_i)}
Ps, Cs = [nil] + ps_, [nil] + cs_

ma = ->(a, b){a > b ? a : b}
mi = ->(a, b){a < b ? a : b}
@m = Array.new(N + 1, false)

def loop(i)
  ret = []
  while !@m[i]
    @m[i] = true
    ret << (i = Ps[i])
  end
  ret
end

ans = (1 .. N).inject(-Float::INFINITY) do |r, i|
  next r if @m[i]
  lp = loop(i)
  l = lp.size
  ss = (1 .. 3*l).each_with_object([0]){|j, o| o << o.last + Cs[lp[j%l]]}
  k = mi[K, l + K%l]
  ma[r, (0 ... l).map{(K - k)/l*ma[0, ss[l]] + ss[_1 + 1, k].max - ss[_1]}.max]
end
puts ans