fork download
  1. #include<bits/stdc++.h>
  2. #define ll long long
  3. using namespace std;
  4.  
  5.  
  6. int maxSubArraySum(int a[], int size)
  7. {
  8. int max_so_far = INT_MIN, max_ending_here = 0;
  9.  
  10. for (int i = 0; i < size; i++)
  11. {
  12. max_ending_here = max_ending_here + a[i];
  13. if (max_so_far < max_ending_here)
  14. max_so_far = max_ending_here;
  15.  
  16. if (max_ending_here < 0)
  17. max_ending_here = 0;
  18. }
  19. return max_so_far;
  20. }
  21.  
  22. int main() {
  23. //ios_base::sync_with_stdio(false);cin.tie(NULL);
  24. ll t;
  25. cin>>t;
  26. while(t--)
  27. {
  28. ll n;
  29. cin>>n;
  30. int a[n];
  31. for(int i=0;i<n;i++) cin>>a[i];
  32. int max_sum = maxSubArraySum(a, n);
  33. cout << max_sum<<endl;
  34.  
  35. }
  36.  
  37. return 0;
  38. }
  39.  
Success #stdin #stdout 0s 15232KB
stdin
1
5
1 2 3 4 5
stdout
15