1007 Maximum Subsequence Sum （25 point(s)）

``````#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct node{
long long start;
long long end;
long long sum;
};

bool camp(const node &one,const node &two){
if(one.sum>two.sum){
return 1;
}else if(one.sum == two.sum){
if(one.start<two.start){
return 1;
}else if(one.start == two.start){
return one.end<two.end;
}
}
return false;
}

int main(){
long long M=0;
cin>>M;

vector<long long> nums;
vector<node>outs;
long long neg_count=0;

for(long long i=0;i<M;i++){
long long temp;
cin>>temp;
nums.push_back(temp);
if(temp<0){
neg_count++;
}
}
if(neg_count==M){
cout<<0<<" "<<nums[0]<<" "<<nums[nums.size()-1];
return 0;
}

vector<long long> dp(nums.size()+1,0);

long long start=0;
//    long long end=0;
long long max=-1;
long long length = dp.size()-1;

for(long long i=0;i<length;i++){
if(nums[i]>0){
dp[i+1]=dp[i]+nums[i];
}else{
if(dp[i]+nums[i]>=0){
dp[i+1]=dp[i]+nums[i];
}else {
dp[i+1]=0;
start =i+1;
}
}
if(max<dp[i+1]){
node temp;
temp.start = start;
temp.end = i;
temp.sum = dp[i+1];
max = dp[i+1];
outs.push_back(temp);
}
}

sort(outs.begin(),outs.end(),camp);
cout<<outs[0].sum<<" "<<nums[outs[0].start]<<" "<<nums[outs[0].end];
return 0;

}``````