380 传统题

[CF2289D] 邻项反号非负化 (Problem from Yuzhnouralsk)

时间限制
2000ms
内存限制
256MiB
登顶 / 尝试
1/ 1
难度
5
    上传者 标签CodeforcesDiv. 2前缀和逆序对树状数组

[CF2289D] 邻项反号非负化 (Problem from Yuzhnouralsk)

Problem Statement

You are given an array of integers a1,a2,…,ana_1, a_2, \dots, a_n. In one operation, choose an index ii (2≤i≤n2 \le i \le n) and simultaneously:

  • add aia_i to ai−1a_{i-1};
  • if i<ni < n, add aia_i to ai+1a_{i+1};
  • replace aia_i with −ai-a_i.

Find the minimum number of operations to make all elements nonnegative, or output -1 if impossible.

Input

First line: tt (1≤t≤1041 \le t \le 10^4). Each test case: nn (2≤n≤2⋅1052 \le n \le 2 \cdot 10^5) and a1,…,ana_1, \dots, a_n (−109≤ai≤109-10^9 \le a_i \le 10^9). Sum of n≤2⋅105n \le 2 \cdot 10^5.

Output

Output the minimum number of operations, or -1.

样例

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