#K88292. Minimum Moves to Non-Decreasing Sequence
Minimum Moves to Non-Decreasing Sequence
Minimum Moves to Non-Decreasing Sequence
You are given a sequence of n integers. Your task is to determine the minimum number of moves required to transform this sequence into a non-decreasing sequence.
In one move, you can increment an element by exactly one. More formally, given a sequence \(a_1, a_2, \dots, a_n\), you need to ensure that \(a_1 \le a_2 \le \dots \le a_n\). The total number of moves required is the sum of all necessary increments:
\(\text{moves} = \sum_{i=2}^{n} \max(0, a_{i-1} - a_i)\)
Help the judge determine the minimum number of moves required to achieve this non-decreasing order.
inputFormat
The input is provided via standard input and has the following format:
n x1 x2 x3 ... xn
Where:
n
is the number of elements in the sequence.x1, x2, ..., xn
are the integers in the sequence (separated by spaces).
outputFormat
Output a single integer which is the minimum number of moves required to make the sequence non-decreasing. The output should be printed to standard output.
## sample5
4 2 1 3 5
6