Omkar and Waterslide
Contest: Codeforces Global Round 10
Difficulty rating: 1200
Tags: greedy, implementation

Omkar is building a waterslide in his water park, and he needs your help to ensure that he does it as efficiently as possible.

Omkar currently has $$$n$$$ supports arranged in a line, the $$$i$$$-th of which has height $$$a_i$$$. Omkar wants to build his waterslide from the right to the left, so his supports must be nondecreasing in height in order to support the waterslide. In $$$1$$$ operation, Omkar can do the following: take any contiguous subsegment of supports which is nondecreasing by heights and add $$$1$$$ to each of their heights.

Help Omkar find the minimum number of operations he needs to perform to make his supports able to support his waterslide!

An array $$$b$$$ is a subsegment of an array $$$c$$$ if $$$b$$$ can be obtained from $$$c$$$ by deletion of several (possibly zero or all) elements from the beginning and several (possibly zero or all) elements from the end.

An array $$$b_1, b_2, \dots, b_n$$$ is called nondecreasing if $$$b_i\le b_{i+1}$$$ for every $$$i$$$ from $$$1$$$ to $$$n-1$$$.

Input format:
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 100$$$). Description of the test cases follows.

The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the number of supports Omkar has.

The second line of each test case contains $$$n$$$ integers $$$a_{1},a_{2},...,a_{n}$$$ $$$(0 \leq a_{i} \leq 10^9)$$$ — the heights of the supports.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

Output format:
For each test case, output a single integer — the minimum number of operations Omkar needs to perform to make his supports able to support his waterslide.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
The subarray with which Omkar performs the operation is bolded.

In the first test case:

- First operation:$$$[5, 3, \textbf{2}, 5] \to [5, 3, \textbf{3}, 5]$$$
- Second operation:$$$[5, \textbf{3}, \textbf{3}, 5] \to [5, \textbf{4}, \textbf{4}, 5]$$$
- Third operation:$$$[5, \textbf{4}, \textbf{4}, 5] \to [5, \textbf{5}, \textbf{5}, 5]$$$

In the third test case, the array is already nondecreasing, so Omkar does $$$0$$$ operations.

Example input:
3
4
5 3 2 5
5
1 2 3 5 3
3
1 1 1

Example output:
3
2
0

Editorial:
Call the initial array $$$a$$$. We claim that the answer is $$$\sum max(a_i-a_{i+1}, 0)$$$ over the entire array of supports (call this value $$$ans$$$). Now let's show why.
First, notice that in a nondecreasing array, $$$ans = 0$$$. So, the problem is now to apply operations to the array such that $$$ans = 0$$$.
Now, let's see how applying one operation affects $$$ans$$$. Perform an operation on an arbitrary nondecreasing subarray that begins at index $$$i$$$ and ends at index $$$j$$$. Note that the differences of elements within the subarray stay the same, so the only two pairs of elements which affect the sum are $$$a_{i-1}, a_i$$$ and $$$a_j, a_{j+1}$$$.
Let's initially look at the pair $$$a_{i-1}, a_i$$$. If $$$a_{i-1} \leq a_i$$$ (or if $$$i = 1$$$), applying an operation would not change $$$ans$$$. But, if $$$a_{i-1} /gt a_i$$$, applying an operation would decrease $$$ans$$$ by $$$1$$$.
Now let's look at the pair $$$a_j, a_{j+1}$$$. If $$$a_j \leq a_{j+1}$$$ (or if $$$j = n$$$), applying an operation would not change $$$ans$$$. But, if $$$a_j \gt a_{j+1}$$$, applying an operation would increase $$$ans$$$ by $$$1$$$.
We have now shown that we can decrease $$$ans$$$ by at most $$$1$$$ with each operation, showing that it is impossible to make his supports able to hold the waterslide in fewer than $$$\sum max(a_i-a_{i+1}, 0)$$$ over the initial array. Now, let's construct a solution that applies exactly $$$\sum max(a_i-a_{i+1}, 0)$$$ operations to make the array valid.
Consider applying operations to each suffix of length $$$j$$$ until the suffix of length $$$j+1$$$ is nondecreasing. Since operations are applied iff $$$a_{n-j+1} \lt a_{n-j}$$$, and each operation decreases $$$a_{n-j+1} \lt a_{n-j}$$$ by $$$1$$$, the total number of operations would just be the sum of $$$max(0, a_{n-j}-a_{n-j+1})$$$, which is equal to $$$\sum max(a_i-a_{i+1}, 0)$$$ over the entire array.