n, k = gets.split.map(&:to_i)
p = gets.split.map(&:to_i)
c = gets.split.map(&:to_i)
t = []
l = []
idx = p[0] - 1
used = []
loop do
  break if idx.nil?
  if used.include?(idx)
    l << t.dup
    t = []
    idx = ([*0..n-1] - used).first
  else
    t << c[idx]
    used << idx
    idx = p[idx] - 1
  end
end

ans = []
l.each do |t|
  ln = t.size
  max = Array.new(ln)
  ln.times do |i|
    m = -10000000000
    ln.times do |j|
      tr = t.rotate(j)
      m = tr.take(i+1).sum if m < tr.take(i+1).sum
    end
    max[i] = m
  end

  if t.sum >= 0
    if k >= ln
      ans << t.sum * (k/ln) + (k % ln == 0 ? 0 : max[k % ln - 1])
    else
      ans << max[0..k-1].max
    end
  else
    if k >= ln
      ans << max.max
    else
      ans << max[0..k-1].max
    end
  end
end
puts ans.max