DOJ logoDOJ
Problems
Recently added problemsSaved problems
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord-backed certification.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

Flattening the Mountain

Back to problem

Editorial

Let CiC_iCi​ be the number of selected bombs whose intervals contain position iii. After all selected bombs are used, position iii has height 000 if and only if Ci≥AiC_i \geq A_iCi​≥Ai​. 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 0,1,…,N0,1,\ldots,N0,1,…,N.

  • For every iii (1≤i≤N1 \leq i \leq N1≤i≤N), add an edge i−1→ii-1 \rightarrow ii−1→i with capacity M−AiM-A_i and cost .

Send exactly MMM units of flow from vertex 000 to vertex NNN at minimum cost. If some Ai>MA_i>MAi​>M, the instance is immediately impossible.

Consider the cut between vertices 0,1,…,i−10,1,\ldots,i-10,1,…,i−1 and vertices i,i+1,…,Ni,i+1,\ldots,Ni,i+1,…,N. 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 i−1→ii-1 \rightarrow ii−1→i can carry at most M−AiM-A_iM−Ai​ units. Therefore, at least AiA_i 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 000 to Lj−1L_j-1Lj​−1, takes the bomb edge Lj−1→RjL_j-1 \rightarrow R_jLj, and then follows zero-cost edges to . Every remaining path uses only zero-cost edges.

The zero-cost edge at position iii is used by exactly M−CiM-C_iM−Ci​ paths. Since Ci≥AiC_i \geq A_iCi​, 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-111 edge carries one unit of flow. Both transformations preserve the total cost, so the minimum flow cost is the answer.

The graph has N+1N+1N+1 vertices and O(N+M)O(N+M)O(N+M) edges. A successive shortest augmenting path algorithm with Dijkstra and potentials performs at most MMM augmentations, giving time complexity O(M(N+M)log⁡N)O(M(N+M)\log N) and space complexity .

Solution written by GPT5.6

M−Ai​
000
  • For every bomb jjj, add an edge Lj−1→RjL_j-1 \rightarrow R_jLj​−1→Rj​ with capacity 111 and cost WjW_jWj​.
  • MM
    M
    MMM
    A
    i​
    Lj−1→RjL_j-1 \rightarrow R_jLj​−1→Rj​
    Lj≤i≤RjL_j \leq i \leq R_jLj​≤i≤Rj​
    iii
    AiA_iAi​
    ​
    −
    1→
    Rj​
    NNN
    ≥
    Ai​
    M−Ci≤M−AiM-C_i \leq M-A_iM−Ci​≤M−Ai​
    MMM
    O(M(N+
    M)logN)
    O(N+M)O(N+M)O(N+M)