QOJ.ac

QOJ

時間限制: 1.0 s 記憶體限制: 1024 MB 總分: 100 可 Hack ✓

#14508. Pearl Chain

统计

Ling has a pearl necklace $a$ of length $n$ on her desk. Each pearl has a luster value, and the luster value of the $i$-th pearl from the beginning to the end is $a_i$.

Ling holds a pearl necklace $b$, which is initially empty. She performs several operations on this necklace. In each operation, she can choose one of the following two options:

  • Add a pearl with a luster value of $i$ to the end of $b$.
  • Increase the luster value of any pearl in $b$ by $1$.

Ling wants to know: what is the minimum number of operations required to make the pearl necklace $b$ identical to $a$? If it is impossible to make $b$ identical to $a$, output -1.

Input

The input contains multiple test cases. The first line contains a single positive integer $T$ ($1 \le T \le 10^4$), representing the number of test cases.

For each test case: The first line contains a single positive integer $n$ ($1 \le n \le 5 \times 10^5$), representing the length of the pearl necklace $a$. The second line contains $n$ positive integers, where the $i$-th positive integer is $a_i$ ($1 \le a_i \le 10^{12}$), representing the luster value of the $i$-th pearl in $a$ from beginning to end.

It is guaranteed that the sum of $n$ over all $T$ test cases does not exceed $5 \times 10^5$.

Output

For each test case: Output a single integer representing the minimum number of operations. If it is impossible to make $b$ identical to $a$, output -1.

Examples

Input 1

3
5
1 2 4 5 6
8
1 2 4 5 5 8 10 9
3
3 2 1

Output 1

6
13
-1

Note

For the first test case: A sequence of 6 operations is as follows: 1. Add a pearl with a luster value of 1 to the end of $b$. 2. Add a pearl with a luster value of 2 to the end of $b$. 3. Add a pearl with a luster value of 3 to the end of $b$. 4. Increase the luster value of the 3rd pearl in $b$ (from beginning to end) by 1. 5. Add a pearl with a luster value of 5 to the end of $b$. 6. Add a pearl with a luster value of 6 to the end of $b$.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.