Omkar and Waterslide

Omkar and Waterslide

time limit per test 2 seconds
memory limit per test 256 megabytes

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

Input

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

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

The second line of each test case contains n integers a 1 , a 2 , . . . , a n ( 0 ≤ a i ≤ 1 0 9 ) a_1,a_2,...,a_n (0≤a_i≤10^9) a1,a2,...,an(0ai109) — the heights of the supports.

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

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.

Example

input

3
4
5 3 2 5
5
1 2 3 5 3
3
1 1 1

output

3
2
0

Note

The subarray with which Omkar performs the operation is bolded.

In the first test case:

First operation:

[ 5 , 3 , 2 , 5 ] → [ 5 , 3 , 3 , 5 ] [5,3,2,5]→[5,3,3,5] [5,3,2,5][5,3,3,5]
Second operation:

[ 5 , 3 , 3 , 5 ] → [ 5 , 4 , 4 , 5 ] [5,3,3,5]→[5,4,4,5] [5,3,3,5][5,4,4,5]
Third operation:

[ 5 , 4 , 4 , 5 ] → [ 5 , 5 , 5 , 5 ] [5,4,4,5]→[5,5,5,5] [5,4,4,5][5,5,5,5]
In the third test case, the array is already nondecreasing, so Omkar does 0 0 0 operations.

/***  Amber  ***/
#pragma GCC optimize(3,"Ofast","inline")
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define ls (rt<<1)
#define rs (rt<<1|1)
typedef long long ll;
template <typename T>
inline void read(T &x) {
    x = 0;
    static int p;
    p = 1;
    static char c;
    c = getchar();
    while (!isdigit(c)) {
        if (c == '-')p = -1;
        c = getchar();
    }
    while (isdigit(c)) {
        x = (x << 1) + (x << 3) + (c - 48);
        c = getchar();
    }
    x *= p;
}
template <typename T>
inline void print(T x) {
    if (x<0) {
        x = -x;
        putchar('-');
    }
    static int cnt;
    static int a[50];
    cnt = 0;
    do {
        a[++cnt] = x % 10;
        x /= 10;
    } while (x);
    for (int i = cnt; i >= 1; i--)putchar(a[i] + '0');
    puts("");
}
const double pi=acos(-1);
const double eps=1e-6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
const ll Inf = 0x3f3f3f3f3f3f3f3f;
const int maxn = 2e5+10;
ll n;
ll a[maxn];
inline void work() {
    read(n);
    for (int i = 1; i <= n; i++) {
        read(a[i]);
    }
    ll ans = 0;
    for (int i = 2; i <= n; i++) {
        if (a[i] < a[i - 1]) ans += a[i - 1] - a[i];
    }
    print(ans);
}
int main() {
    //freopen("1.txt","r",stdin);
    int T = 1;
    read(T);
    while (T--) {
        work();
    }
    return 0;
}

你可能感兴趣的:(Codeforces,acm竞赛)