最大连续子列和

#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdio>
using namespace std;
#define N 20010
int a[N];
int main()
{
    int n;
    while(cin>>n){
        for(int i=0;i<n;i++)
            cin>>a[i];
        int s1=0,s2=0;
        for(int i=0;i<n;i++){
            s1+=a[i];
            if(s1>s2) s2=s1;
            else if(s1<0) s1=0;
        }
        cout<<s2<<endl;
    }
    return 0;
}
View Code

 

posted @ 2017-09-03 11:33  wydxry  阅读(174)  评论(0编辑  收藏  举报
Live2D