fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. // Speed
  5. #define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
  6.  
  7. // Typedefs
  8. #define int long long
  9. #define pb push_back
  10. #define ff first
  11. #define ss second
  12. #define all(x) (x).begin(), (x).end()
  13. #define rall(x) (x).rbegin(), (x).rend()
  14. #define sz(x) ((int)(x).size())
  15. #define endl '\n'
  16. #define yes cout << "yes\n"
  17. #define no cout << "no\n"
  18.  
  19. // Loops
  20. #define rep(i,a,b) for(int i=a;i<b;++i)
  21. #define per(i,a,b) for(int i=b-1;i>=a;--i)
  22. #define each(x, a) for (auto& x : a)
  23.  
  24. // Consts
  25. const int INF = 1e18;
  26. const int MOD = 1e9+7;
  27. const int N = 2e5 + 5;
  28.  
  29. // Logic
  30. void solve() {
  31. int n;
  32. cin >> n;
  33. vector<int> b(n);
  34. int sum = 0;
  35. int m = 0; // Count of non-zero elements
  36. rep(i, 0, n) {
  37. cin >> b[i];
  38. sum += b[i];
  39. if (b[i] > 0) m++;
  40. }
  41.  
  42.  
  43. cout << min(m, sum - n + 1) << endl;
  44. }
  45.  
  46. // Main
  47. int32_t main() {
  48. fast_io;
  49.  
  50. int t;
  51. cin >> t;
  52. while (t--) {
  53. solve();
  54. }
  55.  
  56. return 0;
  57. }
Success #stdin #stdout 0.01s 5320KB
stdin
3
5
0 5 1 0 1
3
3 2 1
5
1 1 1 1 1
stdout
3
3
1