CF:Codeforces Round 879 (Div. 2)A. Unit Array

Given an array aa of length nn, which elements are equal to −1−1 and 11. Let's call the array aa good if the following conditions are held at the same time:

  • a1+a2+…+an≥0a1+a2+…+an≥0;

  • a1⋅a2⋅…⋅an=1a1⋅a2⋅…⋅an=1.

In one operation, you can select an arbitrary element of the array aiai and change its value to the opposite. In other words, if ai=−1ai=−1, you can assign the value to ai:=1ai:=1, and if ai=1ai=1, then assign the value to ai:=−1ai:=−1.

Determine the minimum number of operations you need to perform to make the array aa good. It can be shown that this is always possible.

Input

Each test consists of multiple test cases. The first line contains a single integer tt (1≤t≤5001≤t≤500) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤1001≤n≤100) — the length of the array aa.

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (ai=±1ai=±1) — the elements of the array aa.

Output

For each test case, output a single integer — the minimum number of operations that need to be done to make the aa array good.

Example

input

Copy

 
  

7

4

-1 -1 1 -1

5

-1 -1 -1 1 1

4

-1 1 -1 1

3

-1 -1 -1

5

1 1 1 1 1

1

-1

2

-1 -1

output

Copy

1
1
0
3
0
1
2

Note

In the first test case, we can assign the value a1:=1a1:=1. Then a1+a2+a3+a4=1+(−1)+1+(−1)=0≥0a1+a2+a3+a4=1+(−1)+1+(−1)=0≥0 and a1⋅a2⋅a3⋅a4=1⋅(−1)⋅1⋅(−1)=1a1⋅a2⋅a3⋅a4=1⋅(−1)⋅1⋅(−1)=1. Thus, we performed 11 operation.

In the second test case, we can assign a1:=1a1:=1. Then a1+a2+a3+a4+a5=1+(−1)+(−1)+1+1=1≥0a1+a2+a3+a4+a5=1+(−1)+(−1)+1+1=1≥0 and a1⋅a2⋅a3⋅a4⋅a5=1⋅(−1)⋅(−1)⋅1⋅1=1a1⋅a2⋅a3⋅a4⋅a5=1⋅(−1)⋅(−1)⋅1⋅1=1. Thus, we performed 11 operation.

In the third test case, a1+a2+a3+a4=(−1)+1+(−1)+1=0≥0a1+a2+a3+a4=(−1)+1+(−1)+1=0≥0 and a1⋅a2⋅a3⋅a4=(−1)⋅1⋅(−1)⋅1=1a1⋅a2⋅a3⋅a4=(−1)⋅1⋅(−1)⋅1=1. Thus, all conditions are already satisfied and no operations are needed.

In the fourth test case, we can assign the values a1:=1,a2:=1,a3:=1a1:=1,a2:=1,a3:=1. Then a1+a2+a3=1+1+1=3≥0a1+a2+a3=1+1+1=3≥0 and a1⋅a2⋅a3=1⋅1⋅1=1a1⋅a2⋅a3=1⋅1⋅1=1. Thus, we performed 33 operations.

#include 
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
 
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
 
        vector a(n);
 
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
        }
        int res = 0,cnt = 1;
 
        int s = accumulate(a.begin(), a.end(), 0);
 
        for (int j = 0; s < 0 && j < n  ; j++)
        {
            if (a[j] == -1)
            {
                a[j] = 1;
                res++;
                s += 2;
            }
        }
 
        for (int i = 0; i < n; i++)
        {
            cnt = cnt * a[i];
        }
        if (cnt == -1)
        {
            res += 1;
        }
        cout << res <

你可能感兴趣的:(开发语言,c++,算法)