Skip to content

Commit

Permalink
Create 1007.Maximum Subsequence Sum (25 分).cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
hao14293 authored Apr 18, 2019
1 parent 1f9a2b7 commit 8883d67
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions PAT/PAT-A/CPP/1007.Maximum Subsequence Sum (25 分).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <iostream>
#include <vector>
using namespace std;

int main(){
int n;
cin >> n;
vector<int> v(n);
int start = 0, end = n - 1, sum = -1, temp = 0, tempindex = 0;
for(int i = 0; i < n; i++){
cin >> v[i];
temp = temp + v[i];
if(temp < 0){
temp = 0;
tempindex = i + 1;
}else if(temp > sum){
sum = temp;
start = tempindex;
end = i;
}
}
if(sum < 0) sum = 0;
printf("%d %d %d", sum, v[start], v[end]);
return 0;
}

0 comments on commit 8883d67

Please sign in to comment.