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

round = n.times.map do |start|
  pos = start
  max = -10_000_000_000
  scr = 0
  rec = [[pos, scr, max]]
  n.times do
    pos_next = ps[pos]-1
    scr += cs[pos_next]
    max = [max, scr].max
    pos = pos_next
    rec << [pos, scr, max]
  end
  rec
end

#p round

ac = cs[0]
wa = 100_000_000_000
while (ac - wa).abs > 1
  wj = (ac + wa) / 2
  if n.times.any? { |i|
      rnd = round[i]
      rep = k / n
      max = -100_000_000_000
      scr = 0
      dmax = rnd[-1][2]
      dscore = rnd[-1][1]
      rep.times do
        smax = scr + dmax
        scr += dscore
        max = [max, scr, smax].max
      end
      max = [max, scr + rnd[k % n][2]].max
      next wj <= max
    }
    ac = wj
  else
    wa = wj
  end
end

p ac
