Ankit_132

C

Sep 25th, 2023
661
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1.  
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. #define ll     long long
  7. #define _test   int _TEST; cin>>_TEST; while(_TEST--)
  8.  
  9. int main()
  10. {
  11.     _test
  12.     {
  13.         int n;
  14.         cin>>n;
  15.        
  16.         vector<ll int> a(n);
  17.         for(auto &e: a)     cin>>e;
  18.        
  19.         if(n == 1)
  20.         {
  21.             if(a[0] <= 0)       cout<<0<<"\n";
  22.             else                cout<<a[0]<<"\n";
  23.            
  24.             continue;
  25.         }
  26.        
  27.         ll int ans = 0;
  28.        
  29.         if(a[0]>=0 || a[1]<=0)
  30.         {
  31.             for(int i=0; i<n; i++)
  32.                 ans += max(0ll, a[i]);
  33.                
  34.             cout<<ans<<"\n";
  35.            
  36.             continue;
  37.         }
  38.        
  39.         for(int i=2; i<n; i++)
  40.             ans += max(0ll, a[i]);
  41.            
  42.         ans += max(0ll, a[0]+a[1]);
  43.            
  44.         cout<<ans<<"\n";
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment