P28795 [洛谷] Sequence Transformation
提高+
题目描述

Problem Background

Enhanced version of CF13C

Problem Description

Given a sequence, each operation can increase or decrease a number by 1. The goal is to transform the sequence into a non-decreasing sequence while ensuring that the modified sequence only contains numbers that were present in the original sequence.

Input Format

The first line contains an integer \( n \) (\( n \leq 5 \times 10^5 \)), representing the number of digits.
The second line contains \( n \) integers, each with an absolute value not exceeding \( 10^9 \).

Output Format

Output a single number, representing the minimum number of operations required.

Sample Input and Output

Sample Input #1

5
3 2 -1 2 11

Sample Output #1

4

Sample Input #2

5
2 1 1 1 1

Sample Output #2

1

Notes

For \( 100\% \) of the data, \( 1 \le n \le 5 \times 10^5 \).

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
5 3 2 -1 2 11
样例输出
4