fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void maxsub(vector<int>& m)
  5. {
  6. int i, msf=INT_MIN, mn=0, ie=0, j=0,is=0;
  7. for(i=0;i<m.size();i++)
  8. {
  9. mn += m[i];
  10. j++;
  11. if(msf<mn)
  12. {
  13. msf = mn;
  14. ie = i;
  15. is = ie-j+1;
  16. }
  17. if(mn<0)
  18. {
  19. mn = 0;
  20. j=0;
  21. }
  22. }
  23. cout<<msf<<" "<<is<<" "<<ie<<"\n";
  24. return;
  25. }
  26. int main() {
  27. // your code goes here
  28. int t;
  29. cin>>t;
  30. while(t--)
  31. {
  32. int n, x, i;
  33. cin>>n;
  34. vector<int> m;
  35. for(i=0;i<n;i++)
  36. {
  37. cin>>x;
  38. m.push_back(x);
  39. }
  40. maxsub(m);
  41. }
  42. return 0;
  43. }
Success #stdin #stdout 0.01s 5516KB
stdin
4
9
-24 0 28 28 55 -31 -27 -45 -24 
10
40 5 39 45 31 -44 73 -16 -31 27 
7
57 18 -14 17 31 16 -16 
14
-24 0 28 28 55 -31 -27 -45 -24 0 28 28 55 57 
stdout
111 1 4
189 0 6
125 0 5
168 9 13