#include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
scanf("%d", &n);
vector<int> v(n);///
int leftindex=0,rightindex=n-1,sum =-1,temp=0,tempindex=0;
for (int i = 0; i < n; i++) {
scanf("%d", &v[i]);
temp = temp + v[i];
if (temp < 0)// 舍弃temp < 0前面的内容
{
temp = 0;
tempindex = i + 1;
}
else if (temp > sum) {
sum = temp;
leftindex = tempindex;
rightindex = i;
}
}
if (sum < 0) sum = 0;
printf("%d %d %d", sum, v[leftindex], v[rightindex]);
return 0;
}
代码源自:https://blog.csdn.net/liuchuo/article/details/52144554
上面的链接中也有解题分析,感谢。