CF1987B.Sort

传统题 时间 2000 ms 内存 256 MiB 5 尝试 1 已通过 1 标签

Sort

You are given an array of integers aa of length nn.

You can apply the following operation any number of times (maybe, zero):

  • First, choose an integer kk such that 1kn1 \le k \le n and pay k+1k + 1 coins.
  • Then, choose exactly kk indices such that 1i1<i2<<ikn1 \le i_1 \lt i_2 \lt \ldots \lt i_k \le n.
  • Then, for each xx from 11 to kk, increase aixa_{i_x} by 11.

Find the minimum number of coins needed to make aa non-decreasing. That is, a1a2ana_1 \le a_2 \le \ldots \le a_n.

Input

Each test contains multiple test cases. The first line of input contains a single integer tt (1t1041 \le t \le 10^4) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer nn (1n1051 \le n \le 10^5) — the length of the array aa.

The second line of each test case contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (1ai1091 \le a_i \le 10^9) — the elements of the array aa.

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

Output

For each test case, output a single integer — the minimum number of coins needed to make aa non-decreasing.

Note

In the first test case, aa is already sorted, so you don't have to spend any coins.

In the second test case, the optimal sequence of operations is:

  • Choose k=2k = 2 and the indices 22 and 55: $[ 2, \color{red}{1}, 4, 7, \color{red}{6} ] \rightarrow [2, 2, 4, 7, 7]$. This costs 33 coins.

It can be proven that it is not possible to make aa non-decreasing by spending less than 33 coins.

Samples

5
3
1 7 9
5
2 1 4 7 6
4
1 3 2 4
1
179
9
344 12 37 60 311 613 365 328 675
0
3
2
0
1821

在线编程 IDE

建议全屏模式获得最佳体验