#P17294. [ICPC 2026 Xi'an I] North and South
[ICPC 2026 Xi'an I] North and South
Problem Description
Yuki has a sequence of length .
Yuki defines an operation as follows:
- Choose an interval of . For every integer such that :
- If is odd, the value of decreases by , i.e., .
- If is even, the value of increases by , i.e., .
Now, Yuki wants to perform some number of operations such that all numbers in the sequence are equal. You need to help Yuki find the minimum number of operations required to make all numbers in the sequence equal, or report if it is impossible.
Input Format
This problem contains multiple test cases.
The first line contains a positive integer , representing the number of test cases.
For each test case:
- The first line contains a positive integer .
- The second line contains integers .
It is guaranteed that the sum of over all test cases does not exceed .
Output Format
For each test case, output one line:
- If it is impossible, output .
- If it is possible, output an integer representing the minimum number of operations to make all numbers in the sequence equal.
3
2
1 3
4
1 5 1 5
5
1 3 1 3 1
1
2
-1
Hint
For the first test case:
- Perform the operation on the interval . The sequence becomes , where all numbers are equal.
- It can be proven that no solution with fewer operations exists, so the answer is .
For the second test case:
- Perform the operation on the interval . The sequence becomes .
- Perform the operation on the interval . The sequence becomes , where all numbers are equal.
- It can be proven that no solution with fewer operations exists, so the answer is .
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 .