fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void maxsubarray(vector<int>&nums){
  5. int sum=0;
  6. int maxi=nums[0];
  7. for(auto it : nums){
  8. sum+=it;
  9. maxi = max(sum, maxi);
  10. if(sum<0) sum=0;
  11. }
  12. cout<<maxi<<"\n";
  13. }
  14.  
  15. int main() {
  16. int T;
  17. cin>>T;
  18. vector<int>nums;
  19. while(T--){
  20. int n;cin>>n;
  21. for(int i=0;i<n;i++){
  22. int u;cin>>u;
  23. nums.push_back(u);
  24. }
  25. maxsubarray(nums);
  26. }
  27. // your code goes here
  28. return 0;
  29. }
Success #stdin #stdout 0s 5408KB
stdin
1
9
-2 1 -3 4 -1 2 1 -5 4
stdout
6