n, k = gets.chomp.split.map(&:to_i)
p = gets.chomp.split.map(&:to_i)
c = gets.chomp.split.map(&:to_i)
used = Hash.new(false)
res = - 10 ** 22

n.times do |cur|
  if used[cur]
    next
  end

  cyc = []
  while !used[cur]
    used[cur] = true
    cyc << c[cur]
    cur = p[cur] - 1
  end

  l = cyc.size
  dp = [0]
  ct = Array.new(l, 0)

  l.times do |w|
    ctm = - 10 ** 22
    l.times do |i|
      ct[i] += cyc[(i + w) % l]
      ctm = ct[i] if ct[i] > ctm
    end
    dp << ctm
  end

  if k <= l
    resm = dp[1..k].max
  elsif dp[-1] <= 0
    resm = dp[1..l].max
  else
    resm = dp[-1] * ((k - 1) / l) + dp[1..((k - 1) % l + 1)].max
  end
  res = resm if resm > res
end
puts res