n, k = gets.split.map(&:to_i)
d = gets.split.map(&:to_i)
c = gets.split.map(&:to_i)
ans = nil

n.times do |i|
  q = []
  now = i
  point = 0
  points = []
  n.times do
    nex = d[now] - 1
    break if q.include?(nex)

    q << nex
    point += c[nex]
    points << point
    now = nex
  end
  x, y = k.divmod(q.length)
  sub_sum = points.last
  res = 0
  if x > 0 && sub_sum > 0
    res = sub_sum * x
    if y > 0
      m = points[0..y-1].max
      res += m if m > 0
    else
      m = points.max
      if m > sub_sum
        res = res + m - sub_sum
      end
    end
  else
    if y > 0
      res = points[0..y-1].max
    else
      res = points.max
    end
  end

  if ans
    ans = [ans, res].max
  else
    ans = res
  end
end
puts ans
