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)

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

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

res = []
cyc.each do |cs|
  l = cs.size
  dp = [0]
  ct = Array.new(l, 0)

  l.times do |w|
    l.times do |i|
      ct[i] += cs[(i + w) % l]
    end
    dp << ct.max
  end

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