END { load __FILE__ unless $stdin.eof? }

require 'numo/narray'

n, k = gets.split.map!(&:to_i)
ps = gets.split.map!(&:to_i)
cs = gets.split.map!(&:to_i)

cells = {}
(1..n).zip(ps, cs) { |i, pi, ci| cells[i] = [pi, ci] }
ps = cs = nil

groups = []
until cells.empty?
  group = []
  idx = cells.keys.first

  while cell = cells.delete(idx)
    pi, ci = cell
    group << ci
    idx = pi
  end

  groups << Numo::Int64.cast(group)
end
cells = nil

def max_score(seq, cnt)
  s, t = cnt.divmod(seq.size)
  u = seq.sum  # score per loop
  u = 0 if u < 0

  maxes = [seq.max]
  seq_sum = seq.copy
  (2..t).each do
    seq_sum = Numo::Int64.cast(seq_sum.to_a.rotate(1))
    seq_sum += seq
    maxes << seq_sum.max
  end

  maxes.max + u * s
end

p groups.map { |g| max_score(g, k) }.max
