fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define pb push_back
  4. #define mod 1000000007
  5. using namespace std;
  6.  
  7. int maxSubArraySum(ll a[], ll size)
  8. {
  9. int max_so_far = INT_MIN, max_ending_here = 0;
  10. ll start =0, end = 0, s=0;
  11.  
  12. for (ll i=0; i< size; i++ )
  13. {
  14. max_ending_here += a[i];
  15.  
  16. if (max_so_far < max_ending_here)
  17. {
  18. max_so_far = max_ending_here;
  19. start = s;
  20. end = i;
  21. }
  22.  
  23. if (max_ending_here < 0)
  24. {
  25. max_ending_here = 0;
  26. s = i + 1;
  27. }
  28. }
  29. return max_so_far;
  30. }
  31. int main()
  32. {
  33. ll i,j,t,n;
  34. cin>>t;
  35. while(t--)
  36. {
  37. cin>>n;
  38. ll a[n];
  39. ll sum=0;
  40. int flag=0;
  41. for(i=0;i<n;i++)
  42. {
  43. cin>>a[i];
  44. sum+=a[i];
  45. if(a[i]<=0)
  46. flag=1;
  47. }
  48. int max_sum = maxSubArraySum(a, n);
  49. /*cout<<flag<<endl;
  50.   cout<<max_sum<<endl;
  51.   cout<<sum<<endl;*/
  52. if(sum>max_sum||flag==0)
  53. cout<<"YES"<<endl;
  54. else cout<<"NO"<<endl;
  55. }
  56. return 0;
  57. }
  58.  
Success #stdin #stdout 0s 4516KB
stdin
3
4
1 2 3 4
3
7 4 -1
3
5 -5 5
stdout
YES
NO
NO