# 数据结构 01-复杂度2 Maximum Subsequence Sum (25 分)

Given a sequence of

Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

### Input Specification:

Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer

### Output Specification:

For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices

### Sample Input:

10
-10 1 2 3 4 -5 -23 3 7 -21

### Sample Output:

10 1 4

当出现 当前的子列和 大于 已知的最大子列和 时

更新最大子列和同时更新 子列开始和结尾下标 (结尾下标是子列开始下标+子列长度计数)

当出现 当前子列和 小于 0时,  必须是小于0是才更新

重置最大子列和为0 , 子列长度计数为0, 子列开始下标为 当前下标+1

#include <iostream>
using namespace std;
int main(){
int n,temp,start{0},sum{0},max{-1},left{0},right{0},count{0};
bool isAllNegative=true;
cin >> n;
int arr[n];
for(int i=0;i<n;i++){
scanf("%d",&temp);
arr[i]=temp;
sum+=temp;
if(temp>=0){
isAllNegative=false;
}
if(sum>max){
max=sum;
left=start;
right=left+count;
}
count++;
if(sum<0){//重置子列和 重置起点和计数
sum=0;
start=i+1;
count=0;
}
}
if(isAllNegative){
cout<<"0"<<" "<< arr<<" "<<arr[n-1]<<endl;
}else{
cout << max<<" "<< arr[left]<<" "<<arr[right]<<endl;
}
return 0;
}

(0)
(0) 