input = readlines()

n, k = input.shift.chomp.split("\s").map{|x| x.to_i}
p_list = input.shift.chomp.split("\s").map{|x| x.to_i}
c_list = input.shift.chomp.split("\s").map{|x| x.to_i}

tmp_routes = []
(1..p_list.length).each do |p_start|
  current = p_start
  tmp = []
  while(true)
    break if tmp.include?(current)
    tmp.push(current)
    current = p_list[current - 1]
  end
  route = tmp.sort.join('-')
  tmp_routes.push(route) unless tmp_routes.include?(route)
end

routes = []
tmp_routes.each do |route|
  routes.push(route.split('-').map{|x| x.to_i})
end

def calcMax(s_route, limit)
  res = []
  (1..s_route.length).each do |start_m|
    (0..limit).each do |len|
      score = 0
      (0..len).each do |i|
        current_i = (start_m + i) % s_route.length
        score += s_route[current_i]
      end
      res.push(score)
    end
  end
  return res.max
end

list = []
routes.each do |route|
  s_route = []
  route_total = 0
  route.each do |m|
    s_route.push(c_list[m - 1])
    route_total += c_list[m - 1]
  end

  route_max = nil
  if route_total > 0
    loop_count = k / route.length
    loop_rem = k % route.length
    route_max = route_total * loop_count + calcMax(s_route, loop_rem - 1)
  else
    limit = s_route.length < k ? s_route.length : k
    route_max = calcMax(s_route, limit)
  end
  list.push(route_max)
end

puts list.max
