CF1392C-Omkar and Waterslide

目录

CF1392C-Omkar and Waterslide

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 n supports arranged in a line, the i i -th of which has height ai 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 1 operation, Omkar can do the following: take any contiguous subsegment of supports which is nondecreasing by heights and add 1 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 b is a subsegment of an array c c if b b can be obtained from c 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 b1,b2,,bn b_1, b_2, \dots, b_n is called nondecreasing if bibi+1 b_i\le b_{i+1} for every i i from 1 1 to n1 n-1 .

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

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

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

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

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.

1
2
3
4
5
6
7
3
4
5 3 2 5
5
1 2 3 5 3
3
1 1 1
1
2
3
3
2
0
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include "ybwhead/ios.h"
int n, TTT;
int main()
{
    yin >> TTT;
    while (TTT--)
    {
        yin >> n;
        int la = 0;
        long long ans = 0;
        for (int i = 1; i <= n; i++)
        {
            int x;
            yin >> x;
            if (i >= 2)
                ans += max(0, la - x);
            la = x;
        }
        yout << ans << endl;
    }
}
来发评论吧~
Powered By Valine
v1.4.14