Tips
- 输出的不是首尾位置,而是该位置上的数值
- 注意最大值为0与全是负数被迫输出为0的区别
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
int n;
cin >> n;
int num[n];
int sum = 0;
int maxx = -1;
int start, end;
int start_temp = 0;
for (int i = 0; i < n; i++)
{
cin >> num[i];
sum += num[i];
if (sum >= 0)
{
if (sum > maxx)
{
maxx = sum;
start = start_temp;
end = i;
}
}
else
{
sum = 0;
start_temp = i + 1;
}
}
if (maxx == -1)
cout << 0 << " " << num[0] << " " << num[n - 1];
else
cout << maxx << " " << num[start] << " " << num[end];
}