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 <bits/stdc++.h>
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<int> 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 <<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值