51nod DP 最大子段和

#include<iostream>
#include<algorithm>
#include<cstdio>
#define MAXN 50000
using namespace std;
int n;
long long a[MAXN],sum[MAXN];
/*
d[j]表示以j为终点的sum中最大的.
if(d[j]>0) d[j+1] = d[j]+a[j+1];
else
    d[j+1] = a[j+1]
*/
int main()
{
    cin>>n;
    long long max = 0;
    //memset(a,0,sizeof(a));
    for(int j=0;j<n;j++)
    {
        cin>>a[j];
        if(j==0)
        {
            sum[0] = a[0];
            continue;
        }
        if(sum[j-1]>0)
            sum[j] = sum[j-1] + a[j];
        else
            sum[j] = a[j];
        if(sum[j]>max)
            max = sum[j];
    }
    cout<<max<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/joeylee97/p/6128970.html