Dynamic Programming21 sections · 916 units
Open in Course

CSES - Walkthrough

Increasing array variant

Make array non-decreasing by only increasing elements. find the lowest sum of increases. Simpler than Sonya: we can only increase, not decrease. The DP is asymmetric. dp[i][x]dp[i][x] = min cost for first ii elements, last value x\leq x.

But we only pay for increases: cost = max(0,xai)\max(0, x - a_i). Slope trick: track where the optimal xx lies. It's always at a breakpoint (one of the aia_i values). Use heap to maintain.