QOJ.ac

QOJ

时间限制: 2 s 内存限制: 512 MB 总分: 100

#5374. Number Circle

统计

$n$ integers are arranged in a circle. An operation is defined as follows: choose an integer $a$ from the circle, replace it with $-a$, and add $a$ to its two adjacent neighbors on the circle.

Find the minimum number of operations required to make all integers non-negative.

Input

There are multiple test cases.

The first line contains a positive integer $T$ ($1 \leq T \leq 10$), representing the number of test cases.

For each test case, there are two lines:

  • The first line contains a positive integer $n$ ($3 \leq n \leq 10^5$).
  • The second line contains $n$ integers $a_1, a_2, \cdots, a_n$ ($-10^4 \leq a_i \leq 10^4$), which are arranged in a circle in order.

Output

For each test case, output a single integer on a new line. If it is possible to make all integers non-negative, output the minimum number of operations; otherwise, output $-1$.

Examples

Input 1

3
3
2 2 -3
3
2 2 -5
3
0 0 0

Output 1

5
-1
0

Note

For the initial state $2, 2, -3$, the sequence of operations is: $2, 2, -3 \Longrightarrow -1, -1, 3 \Longrightarrow 1, -2, 2 \Longrightarrow -1, 2, 0 \Longrightarrow 1, 1, -1 \Longrightarrow 0, 0, 1$. The goal is reached in $5$ operations, and it is clear that no fewer operations are possible.

For the initial state $2, 2, -5$, it is impossible to reach the goal.

For the initial state $0, 0, 0$, the goal is already reached without any operations.

Subtasks

  • Task 1 (10 points): $n = 3$, $-10 \leq a_i \leq 10$.
  • Task 2 (20 points): $n \leq 50$, $-10 \leq a_i \leq 10$.
  • Task 3 (30 points): $n \leq 2\,000$.
  • Task 4 (10 points): $n \leq 3 \times 10^4$, $\sum a_i = 1$.
  • Task 5 (10 points): $n \leq 3 \times 10^4$, $-10 \leq \sum a_i \leq 10$.
  • Task 6 (10 points): $n \leq 3 \times 10^4$.
  • Task 7 (10 points): No additional constraints.

For $100\%$ of the data, $1 \leq T \leq 10$, $3 \leq n \leq 10^5$, $-10^4 \leq a_i \leq 10^4$.

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.