#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
int main()
{
int n;
cin >> n;
vector<int> arr(n);
int sum=-1;//最大连续子序列和
int tempsum=0;//临时的连续子序列和
int left=0;//最大子序列左下标
int right=0;//最大子序列右下标
int index=0;//临时的序列左下标
bool flag = true;//检验是否全是负数
for(int i=0; i<n; i++){
scanf("%d", &arr[i]);
if(arr[i] >= 0)
flag = false;
tempsum += arr[i];
if(tempsum > sum){
sum = tempsum;
left = index;
right = i;
}else if(tempsum < 0){//如果临时序列和已经小于sum的情况下还是负数,应该舍弃之前的序列
index = i+1;
tempsum = 0;
}
}
if(flag){
printf("0 %d %d", arr[0], arr[n-1]);
}else{
printf("%d %d %d", sum, arr[left], arr[right]);
}
return 0;
}
相关文章
- 10-201292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold_[二维前缀和]
- 10-20【CF280D】 k-Maximum Subsequence Sum ,线段树模拟费用流
- 10-20转载:最大子段和问题(Maximum Interval Sum)
- 10-20Leetcode-1031 Maximum Sum of Two Non-Overlapping Subarrays(两个非重叠子数组的最大和)
- 10-20689. Maximum Sum of 3 Non-Overlapping Subarrays
- 10-20LeetCode 689. Maximum Sum of 3 Non-Overlapping Subarrays
- 10-20Day 1: subarray(maximum sum of contiguous subarray)
- 10-20689. Maximum Sum of 3 Non-Overlapping Subarrays
- 10-20LC1043. Partition Array for Maximum Sum
- 10-20Subarray Sum & Maximum Size Subarray Sum Equals K