7051.Increasing Array II

Time Limit: 1s Memory Limit: 512MB

You are given an array of $n$ integers. You want to modify the array so that it is increasing, i.e., every element is at least as large as the previous element.

On each move, you can increase or decrease the value of any element by one. What is the minimum number of moves required?

Input Format(From the terminal/stdin)

The first input line contains an integer $n$ : the size of the array.

Then, the second line contains $n$ integers $x_1,x_2,\ldots,x_n$ : the contents of the array.

  • $1 \le n \le 2 \cdot 10^5$
  • $1 \le x_i \le 10^9$

Output Format(To the terminal/stdout)

Print the minimum number of moves.

Sample Input

Copy
5
3 8 5 6 5
 \n
 · · · · \n

Sample Output

Copy
4
 \n
Source: CSES, Additional Problems II, 2132

Submit

请先 登录

© 2025 FAQs