CF2154B.Make it Zigzag

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

Make it Zigzag

An arbitrary array of integers bb of length mm is considered awesome if for all ii (1i<m1 \le i \lt m):

  • if ii is odd then bi<bi+1b_i \lt b_{i + 1} holds.
  • if ii is even then bi>bi+1b_i \gt b_{i + 1} holds.

In other words, the following inequality is true: b1<b2>b3<b4b_1 \lt b_2 \gt b_3 \lt b_4 \ldots

You are given an array of positive integers aa of length nn. You may do either of the following operations any number of times, in any order:

  • operation 11: select an integer ii (1in1 \le i \le n) and do: ai:=max(a1,,ai)a_i := \max(a_1,\ldots,a_i), that is, replace aia_i with the prefix max up to ii.
  • operation 22: select an integer ii (1in1 \le i \le n) and then decrease aia_i by 11.

Determine the minimum number of times you need to do operation 22 to make aa awesome. Note that you do not need to minimise the number of times you perform operation 11.

Input

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

The first line of each test case contains an integer nn (2n21052 \le n \le 2 \cdot 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 sum of nn across all test cases does not exceed 21052 \cdot 10^5.

Output

For each testcase, output the minimum cost to make aa awesome.

Note

In the first test case, the array is already awesome so no operations need to be done.

In the second test case, aa can be made awesome as follows:

  • use operation 22 and decrease a1a_1 by 11. $[\color{red}3, 3, 2, 1] \rightarrow [\color{red}2, 3, 2, 1]$.
  • use operation 11 and change a4a_4 to max(2,3,2,1)=3\max(2, 3, 2, 1) = 3. $[2, 3, 2, \color{red}1] \rightarrow [2, 3, 2, \color{red}3]$.

[2,3,2,3][2, 3, 2, 3] is awesome as 2<3>2<32 \lt 3 \gt 2 \lt 3. It can be proven that this is the minimum number of times operation 22 needs to be performed.

Samples

7
5
1 4 2 5 3
4
3 3 2 1
5
6 6 6 6 6
7
1 2 3 4 5 6 7
3
3 2 1
2
1 2
9
65 85 19 53 21 79 92 29 96
0
1
3
6
1
0
13

在线编程 IDE

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