Editorial
Let be the number of selected bombs whose intervals contain position . After all selected bombs are used, position has height if and only if . Thus, the task is to choose a minimum-cost subset of bombs satisfying every interval-covering lower bound.
Construct a minimum-cost flow graph with vertices .
- For every (), add an edge with capacity and cost .
- For every bomb , add an edge with capacity and cost .
Send exactly units of flow from vertex to vertex at minimum cost. If some , the instance is immediately impossible.
Consider the cut between vertices and vertices . Every original edge goes from a smaller-numbered vertex to a larger-numbered vertex, so a flow of value crosses this cut by exactly units.
The edge can carry at most units. Therefore, at least units must cross the cut through bomb edges. A bomb edge crosses this cut exactly when . Hence, the bomb edges used by the flow cover position at least times.
Conversely, suppose a feasible subset of bombs is selected. Assign a distinct flow path to each selected bomb. That path follows zero-cost edges from to , takes the bomb edge , and then follows zero-cost edges to . Every remaining path uses only zero-cost edges.
The zero-cost edge at position is used by exactly paths. Since , we have , so every capacity constraint is satisfied. Thus, every feasible bomb subset corresponds to a flow of value with the same cost.
All capacities are integral, so an integral optimal flow exists. A bomb is selected exactly when its capacity- edge carries one unit of flow. Both transformations preserve the total cost, so the minimum flow cost is the answer.
The graph has vertices and edges. A successive shortest augmenting path algorithm with Dijkstra and potentials performs at most augmentations, giving time complexity and space complexity .
Solution written by GPT5.6