题解 | #最大序列和#
最大序列和
http://www.nowcoder.com/questionTerminal/df219d60a7af4171a981ef56bd597f7b
#include "cstdio"
#include "iostream"
#include "climits"
using namespace std;
#define N 1000000
long long a[N];
long long dp[N];
long long maxxulie(int n){
long long maxmum = -INT_MAX;
for (int i = 0; i < n; ++i) {
if (i==0){
dp[i]=a[i];
} else{
dp[i]=max(a[i],a[i]+dp[i-1]);
}
maxmum= max(maxmum,dp[i]);
}
return maxmum;
}
int main(){
int n;
while (scanf("%d",&n)!=EOF){
for (int i = 0; i < n; ++i) {
scanf("%lld",&a[i]);
}
printf("%lld\n", maxxulie(n));
}
}