传送门:C. Sequence
描述:
C. Sequence time limit per test 1 second memory limit per test 64 megabytes input standard input output standard outputLittle Petya likes to play very much. And most of all he likes to play the following game:
He is given a sequence ofNinteger numbers. At each step it is allowed to increase the value of any number by1or to decrease it by1. The goal of the game is to make the sequence non-decreasing with the smallest number of steps. Petya is not good at math, so he asks for your help.
The sequenceais called non-decreasing ifa1 ≤ a2 ≤ ... ≤ aNholds, whereNis the length of the sequence.
InputThe first line of the input contains single integerN(1 ≤ N ≤ 5000) — the length of the initial sequence. The followingNlines contain one integer each — elements of the sequence. These numbers do not exceed109by absolute value.
OutputOutput one integer — minimum number of steps required to achieve the goal.
Examples input 53 2 -1 2 11 output 4 input 52 1 1 1 1 output 1题意:
给你n个数,每次操作都可以使某个数加1或者减1,求至少多少次操作使得这n个数是非递减的。非递减定义: a1 ≤ a2 ≤ ... ≤ aN
思路:
这题是个大原题
原题是POJ3666题解见Here
不过这题卡空间,需要滚动数组优化一下,秒过_(:зゝ∠)_
还有一个严格递增的版本:CF 714 E 题解见Here
CF改原题真是不亦乐乎,还是要多做题啊