Dynamic Programming21 sections · 915 units
Open in Course

Codeforces 713C Sonya and Problem Without a Legend - Problem Statement

CF 713C

You have nn numbers. Change each to any value. The cost of changing aia_i to xx is ∣x−ai∣|x - a_i|. Make the array strictly increasing with minimum total cost. Naive DP (dynamic programming): dp[i][v]dp[i][v] = min cost for first ii elements with ai=va_i = v. Transition: dp[i][v]=min⁡w<v(dp[i−1][w]+∣v−ai∣)dp[i][v] = \min_{w < v} (dp[i-1][w] + |v - a_i|).

This is O(n⋅V2)O(n \cdot V^2). With V=109V = 10^9, impossible. Think about the shape of dp[i]dp[i] as a function of the last value. It's piecewise linear and convex. You'll track slope changes instead of all values.