Universal Cup Judging System

Universal Cup

Time Limit: 1 s Memory Limit: 512 MB Total points: 100 Difficulty: [show] Hackable ✓
Statistics

Yuki has a sequence $a$ of length $n$.

Yuki defines an operation as follows:

  • Choose an interval $[l, r]$ of even length. For every integer $i$ such that $l \le i \le r$:
    • If $i - l$ is odd, the value of $a_i$ decreases by $1$, i.e., $a_i \leftarrow a_i - 1$.
    • If $i - l$ is even, the value of $a_i$ increases by $1$, i.e., $a_i \leftarrow a_i + 1$.

Now, Yuki wants to perform some number of operations such that all numbers in the sequence $a$ are equal. You need to help Yuki find the minimum number of operations required to make all numbers in the sequence $a$ equal, or report if it is impossible.

Input

This problem contains multiple test cases.

The first line contains a positive integer $t$ ($1 \le t \le 10^5$), representing the number of test cases.

For each test case:

  • The first line contains a positive integer $n$ ($1 \le n \le 10^6$).
  • The second line contains $n$ integers $a_1, \dots, a_n$ ($0 \le a_i \le 10^{12}$).

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$.

Output

For each test case, output one line:

  • If it is impossible, output $-1$.
  • If it is possible, output an integer representing the minimum number of operations to make all numbers in the sequence $a$ equal.

Examples

Input 1

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

Output 1

1
2
-1

Note

For the first test case:

  • Perform the operation on the interval $[1, 2]$. The sequence becomes $2, 2$, where all numbers are equal.
  • It can be proven that no solution with fewer operations exists, so the answer is $1$.

For the second test case:

  • Perform the operation on the interval $[1, 4]$. The sequence becomes $2, 4, 2, 4$.
  • Perform the operation on the interval $[1, 4]$. The sequence becomes $3, 3, 3, 3$, where all numbers are equal.
  • It can be proven that no solution with fewer operations exists, so the answer is $2$.

For the third test case:

  • It is easy to prove that it is impossible to make all numbers equal regardless of the number of operations, so the answer is $-1$.

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.