Guy-Manuel and Thomas have an array aa of nn integers [a1,a2,…,ana1,a2,…,an]. In one step they can add 11 to any element of the array. Formally, in one step they can choose any integer index ii (1≤i≤n1≤i≤n) and do ai:=ai+1ai:=ai+1.

If either the sum or the product of all elements in the array is equal to zero, Guy-Manuel and Thomas do not mind to do this operation one more time.

What is the minimum number of steps they need to do to make both the sum and the product of all elements in the array different from zero? Formally, find the minimum number of steps to make a1+a2+a1+a2+ …… +an≠0+an≠0 and a1⋅a2⋅a1⋅a2⋅ …… ⋅an≠0⋅an≠0.

Input
Each test contains multiple test cases.

The first line contains the number of test cases tt (1≤t≤1031≤t≤103). The description of the test cases follows.

The first line of each test case contains an integer nn (1≤n≤1001≤n≤100) — the size of the array.

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (−100≤ai≤100−100≤ai≤100) — elements of the array .

Output
For each test case, output the minimum number of steps required to make both sum and product of all elements in the array different from zero.

Example
Input
4
3
2 -1 -1
4
-1 0 0 1
2
-1 2
3
0 -2 1
Output
1
2
0
2
Note
In the first test case, the sum is 00. If we add 11 to the first element, the array will be [3,−1,−1][3,−1,−1], the sum will be equal to 11 and the product will be equal to 33.

In the second test case, both product and sum are 00. If we add 11 to the second and the third element, the array will be [−1,1,1,1][−1,1,1,1], the sum will be equal to 22 and the product will be equal to −1−1. It can be shown that fewer steps can’t be enough.

In the third test case, both sum and product are non-zero, we don’t need to do anything.

In the fourth test case, after adding 11 twice to the first element the array will be [2,−2,1][2,−2,1], the sum will be 11 and the product will be −4−4.
一个思维题,只要注意一点,在乘法为0的时候,就代表着肯定有0存在,这样的话就只能+1了,但是在这一过程中需要注意加法不能变为0,所以需要特判一下。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;const int maxx=110;
int a[maxx];
int n;int main()
{int t;scanf("%d",&t);while(t--){scanf("%d",&n);int sum0=0;int sum=0,csum=1;for(int i=1;i<=n;i++){scanf("%d",&a[i]);sum+=a[i];csum*=a[i];sum0+=(a[i]==0);}if(sum&&csum) cout<<0<<endl;else if(sum&&!csum){if(sum+sum0!=0) cout<<sum0<<endl;else cout<<sum0+1<<endl;}else if(!sum&&csum) cout<<1<<endl;else {if(sum+sum0!=0) cout<<sum0<<endl;else cout<<sum0+1<<endl;}}return 0;
}

努力加油a啊,(o)/~

Non-zero CodeForces - 1300A相关推荐

  1. CodeForces 375D Tree and Queries

    传送门:https://codeforces.com/problemset/problem/375/D 题意: 给你一颗有根树,树上每个节点都有其对应的颜色,有m次询问,每次问你以点v为父节点的子树内 ...

  2. 「日常训练」Bad Luck Island(Codeforces Round 301 Div.2 D)

    题意与分析(CodeForces 540D) 是一道概率dp题. 不过我没把它当dp做... 我就是凭着概率的直觉写的,还好这题不算难. 这题的重点在于考虑概率:他们喜相逢的概率是多少?考虑超几何分布 ...

  3. 【codeforces 812C】Sagheer and Nubian Market

    [题目链接]:http://codeforces.com/contest/812/problem/C [题意] 给你n个物品; 你可以选购k个物品;则 每个物品有一个基础价值; 然后还有一个附加价值; ...

  4. CodeForces 获得数据

    针对程序的输出可以看见 CodeForces :当输入.输出超过一定字符,会隐藏内容 所以:分若干个程序进行输入数据的获取 1. 1 for (i=1;i<=q;i++) 2 { 3 scanf ...

  5. codeforces水题100道 第二十七题 Codeforces Round #172 (Div. 2) A. Word Capitalization (strings)...

    题目链接:http://www.codeforces.com/problemset/problem/281/A 题意:将一个英文字母的首字母变成大写,然后输出. C++代码: #include < ...

  6. CodeForces 595A

    题目链接: http://codeforces.com/problemset/problem/595/A 题意: 一栋楼,有n层,每层有m户,每户有2个窗户,问这栋楼还有多少户没有睡觉(只要一个窗户灯 ...

  7. codeforces A. Jeff and Digits 解题报告

    题目链接:http://codeforces.com/problemset/problem/352/A 题目意思:给定一个只有0或5组成的序列,你要重新编排这个序列(当然你可以不取尽这些数字),使得这 ...

  8. Codeforces Round #506 (Div. 3)

    Codeforces Round #506 (Div. 3) 实习期间事不多,对div3 面向题解和数据编程了一波 A. Many Equal Substrings 题目链接 A题就是找后缀和前缀重合 ...

  9. Codeforces Round #417:E. FountainsSagheer and Apple Tree(树上博弈)

    Codeforces Round #417:E. FountainsSagheer and Apple Tree(树上博弈) 标签: codeforces 2017-06-02 11:41 29人阅读 ...

最新文章

  1. Mysql捕捉(网站)应用执行的语句
  2. 在OnLButtonDown获取其他控件被点击的消息
  3. 【图论】【并查集】矩形(ssl 1222)
  4. .net core 5 IIS Api网站部署需要注意
  5. Pycharm 安装
  6. 百度云文章的链接地址抓取工具
  7. 几种常见的基于Lucene的开源搜索解决方案对比
  8. 在CAD中容易混淆的概念
  9. java项目开发——使用MAP和实体类作为参数的优缺点
  10. 3D Engine Demo
  11. 【FPGA】FPGA程序的固化和下载(sd卡)
  12. 某去哪网,JS逆向:★★★★
  13. 从零开始的WTL入门教程(1) WTL的简介,环境搭建
  14. 联想计算机打开盖子自动开机,联想笔记本开盖自动开机怎么关闭
  15. codeforces 722C Destroying Array
  16. 【最新】CentOS7.8 部署redis6.0.1集群
  17. python开发cms 教程_django CMS 入门教程
  18. 用动画和实战打开 React Hooks(一):useState 和 useEffect
  19. SEO优化,4个独辟蹊径的策略
  20. <polymorphism>

热门文章

  1. IOS之UIToolBar约束报错
  2. php 安装测试程序,PHPUnit安装及使用示例
  3. crt中 新建的连接存储在哪_LCD发展至今仍然没有完胜CRT显示器,“老玩家”为你现身说法...
  4. linux共享 smb3.0,区块链3.0_精通Hyperledger之Samba使用(4)
  5. 蜗轮蜗杆计算软件_齿轮传动计算软件
  6. sql datetime转字符串_datetime的用法,时间戳转换
  7. vue中异步组件的使用与配置说明
  8. Visual Studio 智能提示消失解决办法
  9. iconv android 编译,将iconv编译成lua接口
  10. java測試動態方法_java反射学习