#P1392C. Omkar and Waterslide
Omkar and Waterslide
Description
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 supports arranged in a line, the -th of which has height . 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 operation, Omkar can do the following: take any contiguous subsegment of supports which is nondecreasing by heights and add 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 is a subsegment of an array if can be obtained from by deletion of several (possibly zero or all) elements from the beginning and several (possibly zero or all) elements from the end.
An array is called nondecreasing if for every from to .
Each test contains multiple test cases. The first line contains the number of test cases (). Description of the test cases follows.
The first line of each test case contains an integer () — the number of supports Omkar has.
The second line of each test case contains integers — the heights of the supports.
It is guaranteed that the sum of over all test cases does not exceed .
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.
Input
Each test contains multiple test cases. The first line contains the number of test cases (). Description of the test cases follows.
The first line of each test case contains an integer () — the number of supports Omkar has.
The second line of each test case contains integers — the heights of the supports.
It is guaranteed that the sum of over all test cases does not exceed .
Output
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.
Note
The subarray with which Omkar performs the operation is bolded.
In the first test case:
First operation:
Second operation:
Third operation:
In the third test case, the array is already nondecreasing, so Omkar does operations.