record Node, value : Int32, depth : Int32 n = gets.not_nil!.to_i a = gets.not_nil!.split.map(&.to_i) b = gets.not_nil!.split.map(&.to_i) ans = n n.times do |start| # Create min-heap using array (we'll manually maintain it) heap = a.map { |val| Node.new(val, 0) } max_depth = 0 valid = true n.times do |j| # Find the node with minimum value min_index = 0 heap.each_with_index do |node, idx| if node.value < heap[min_index].value min_index = idx end end # Remove the min node min_node = heap.delete_at(min_index) # Calculate new value and depth new_value = min_node.value + b[(start + j) % n] // 2 new_depth = min_node.depth + 1 max_depth = Math.max(max_depth, new_depth) # Early termination if we can't beat current best answer if max_depth >= ans valid = false break end # Add the new node back to heap heap << Node.new(new_value, new_depth) end ans = max_depth if valid && max_depth < ans end puts ans