Dynamic Programming21 sections · 916 units
Open in Course

CSES Increasing Array II - Problem Statement

CSES 2132

You have nn numbers. You can change each to any value. The cost of changing aia_i to xx is xai|x - a_i|. Make the array non-decreasing with minimum total cost.

This is the classic Slope Trick problem. Naive DP: dp[i][v]dp[i][v] = min cost for first ii elements with ai=va_i = v. With V=109V = 10^9, you can't store all values. You'll track the function's shape through its slope-change points. The solution is similar to Sonya, but without the strictly-increasing transform.