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