Here is a string of 5000-length numbers. You need to change some numbers. Yes, the new series is not decreasing... This type of question can be left-side tree... It doesn't matter if the data range is large. However, since the data range is 5000, use an n ^ 2 character first, is the number after transformation or the number in the original sequence, why? Assume that each number is a tree of a certain height. If we find that the height of a tree in a certain range is not monotonous, we should change the height of a tree, you can either increase it or cut it down. Which value does it increase or cut down? It must be the height of a nearby tree !! It is okay to draw a sketch, so we can construct such a DP. dp [I] [j] indicates the number of the first I in the constructed sequence, the number of I is the minimum cost less than or equal to a [j. Use a scrolling array to implement this function. The space is limited to [cpp] # include <cstdio >#include <cstdlib >#include <cstring >#include <algorithm> using namespace std; const int N = 5100; const _ int64 inf = (_ int64) 1 <62; _ int64 a [N], B [N], dp [2] [N]; _ int64 min (_ int64 a ,__ int64 B) {return a <B? A: B;} int main () {int n, I, j; while (scanf ("% d", & n )! = EOF) {for (I = 1; I <= n; I ++) {scanf ("% I64d", & a [I]); B [I] = a [I];} sort (B + 1, B + n + 1); memset (dp [0], 0, sizeof (dp [0]); int cur = 0; for (I = 1; I <= n; I ++) {cur ^ = 1; fill (dp [cur], dp [cur] + n + 1, inf); for (j = 1; j <= n; j ++) {dp [cur] [j] = min (dp [cur] [J-1], dp [cur ^ 1] [j] + abs (a [I]-B [j]); }}_ int64 ans = dp [cur] [1]; for (I = 2; I <= n; I ++) {if (dp [cur] [I] <ans) ans = dp [cur] [I];} printf ("% I64d \ n", ans );}}