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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,根据提供的引用内容,我无法理解你具体想要问什么问题。请提供更清晰明确的问题,我将竭诚为你解答。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Codeforces Round 860 (Div. 2)题解](https://blog.csdn.net/qq_60653991/article/details/129802687)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [【CodeforcesCodeforces Round 865 (Div. 2) (补赛)](https://blog.csdn.net/t_mod/article/details/130104033)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Codeforces Round 872 (Div. 2)(前三道](https://blog.csdn.net/qq_68286180/article/details/130570952)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值