fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. ios::sync_with_stdio(false);
  6. cin.tie(NULL);
  7.  
  8. int t;
  9. cin >> t;
  10. while (t--) {
  11. int n;
  12. cin >> n;
  13. vector<int> a(n);
  14. int sum = 0, count = 0;
  15. for (int i = 0; i < n; i++) {
  16. cin >> a[i];
  17. sum += a[i];
  18. if (a[i] == -1) count++;
  19. }
  20. int ops = 0;
  21. while (sum < 0) {
  22. sum += 2;
  23. count--;
  24. ops++;
  25. }
  26. if (count % 2 != 0) ops++;
  27. cout << ops << '\n';
  28. }
  29. return 0;
  30. }
  31.  
Success #stdin #stdout 0.01s 5288KB
stdin
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
stdout
1
1
0
3
0
1
2