#C1236. Non-Decreasing Fishing Records

    ID: 41778 Type: Default 1000ms 256MiB

Non-Decreasing Fishing Records

Non-Decreasing Fishing Records

In this problem, you are given several fishing records. Each record contains a sequence of integers that represent the number of fish caught in consecutive trips. Unfortunately, due to various inaccuracies, the fish counts may not be non-decreasing. Your task is to calculate the minimum total modifications required to adjust each record so that the sequence becomes non-decreasing.

Formally, given a sequence (a_1, a_2, \ldots, a_n), you need to modify it such that (a_1 \leq a_2 \leq \ldots \leq a_n) holds. The only allowed operation is to increase a value, and the cost of modifying an element from (a_i) to (a_j) is (a_j - a_i). The minimum cost can be computed by the formula:

[ \text{Cost} = \sum_{i=2}^{n} \max(0, a_{i-1} - a_{i}) ]

Moreover, you are given several fishing records. For each record, simply compute and output the minimum cost as described above.

inputFormat

The input is given via standard input (stdin) and begins with an integer (T) representing the number of fishing records. For each record, the first line contains an integer (n) denoting the number of trips, followed by a line with (n) space-separated integers representing the fish counts recorded in each trip.

outputFormat

For each fishing record, output a single integer — the minimum number of modifications required. Each result should be printed on its own line on standard output (stdout).## sample

2
5
3 2 5 1 2
3
1 2 3
8

0

</p>