n, k = gets.chomp.split.map(&:to_i)
p = gets.chomp.split.map(&:to_i)
c = gets.chomp.split.map(&:to_i)
dp = Array.new(n)

n.times do |u|
  used = Hash.new(false)
  cur = u
  ct = []
  t = k
  while !used[cur] && t > 0
    used[cur] = true
    cur = p[cur] - 1
    ct << c[cur]
    t -= 1
  end

  cs = Array.new(ct.size, 0)
  ct.size.times do |i|
    if i == 0
      cs[i] = ct[i]
    else
      cs[i] = cs[i - 1] + ct[i]
    end
  end
  if t == 0 || cs[-1] < 0
    dp[u] = cs.max
  else
    dp[u] = cs[-1] * (t / cs.size + 1)
    t %= cs.size
    if t > 0
      dp[u] += cs[0..t - 1].max
    end
  end
end
puts dp.max