#C5170. Minimum Operations to Make Array Non-Decreasing

    ID: 48790 Type: Default 1000ms 256MiB

Minimum Operations to Make Array Non-Decreasing

Minimum Operations to Make Array Non-Decreasing

You are given T test cases. For each test case, you are provided with an array of n integers. Your task is to determine the minimum number of operations required to transform the array into a non-decreasing array.

In one operation, you can increase any single element of the array by 1. Mathematically, for every index \(i\) (where \(2 \leq i \leq n\)), if the array does not satisfy \(a_i \geq a_{i-1}\), you need to perform \(a_{i-1} - a_i\) operations on \(a_i\) so that \(a_i = a_{i-1}\). That is, if \(a_i

and add (a_{i-1} - a_i) to your total count of operations.

Output the required number of operations for each test case on a separate line.

inputFormat

The input is read from stdin and is formatted as follows:

  1. The first line contains an integer T, the number of test cases.
  2. For each test case, the first line contains an integer n, the size of the array.
  3. The second line contains n space-separated integers representing the array elements.

outputFormat

For each test case, print a single integer representing the minimum number of operations required to make the array non-decreasing. Each answer should be printed on a new line to stdout.

## sample
2
5
1 2 3 2 5
3
3 2 1
1

3

</p>