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

def loop(i)
  ret = [v = Ps[i]]
  ret << (v = Ps[v]) until v == i
  ret
end

def score(ls)
  l = ls.size
  s = ls.each_with_object([0]){|n, o| o << (o.last + Cs[n])}.drop(1)
  (K <= l ? s.take(K) : s.take(K%l)).max + [0, s.last*(K/l)].max
end

puts (1 .. N).map{score(loop(_1))}.max