#C7132. Make Array Non-Decreasing
Make Array Non-Decreasing
Make Array Non-Decreasing
You are given an integer array. In one operation, you can choose an element and increment it so that the overall array becomes non-decreasing. In other words, for each index i (1 ≤ i < n), the condition arr[i] ≤ arr[i+1] must hold after performing the operations.
Find the minimum number of operations required to transform the given array into a non-decreasing array.
The operation is defined as follows:
If arr[i]
is less than arr[i-1]
, you increment arr[i]
to match arr[i-1]
. Formally, for every index i from 2 to n, if arr[i] < arr[i-1]
, you perform an operation to add arr[i-1] - arr[i]
to the total count, and set arr[i] = arr[i-1]
.
The mathematical formulation for the number of operations can be expressed in \( \LaTeX \) as follows:
[ \text{operations} = \sum_{i=2}^{n} \max(0, arr[i-1] - arr[i]) ]
It is guaranteed that the answer fits in a 32-bit signed integer.
inputFormat
The first line of input contains a single integer T
denoting the number of test cases.
For each test case:
- The first line contains an integer
N
, the number of elements in the array. - The second line contains
N
space-separated integers representing the array.
outputFormat
For each test case, output a single line containing the minimum number of operations required to transform the array into a non-decreasing array.
## sample2
5
1 3 2 5 4
4
10 20 30 40
2
0
</p>