UVALive 4869 Profits DP

G - Profits
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

Download as PDF

Your friends have just opened a new business, and you want to see how well they are doing. The business has been running for a number of days, and your friends have recorded their net profit on each day. You want to find the largest total profit that your friends have made during any consecutive time span of at least one day. For example, if your friends' profits looked like this:


Day 1: -3

Day 2: 4

Day 3: 9

Day 4: -2

Day 5: -5

Day 6: 8


Their maximum profit over any span would be 14, from days 2 to 6.

Input

There will be several test cases in the input. Each test case will begin with an integer N(1$ \le$N$ \le$250, 000) on its own line, indicating the number of days. On each of the next N lines will be a single integer P(- 100$ \le$P$ \le$100), indicating the profit for that day. The days are specified in order. The input will end with a line with a single 0.

Output

For each test case, output a single integer, representing the maximum profit over any non-empty span of time. Print each integer on its own line with no spaces. Do not print any blank lines between answers.

Sample Input

6 
-3 
4 
9 
-2 
-5 
8 
2 
-100
-19 
0

Sample Output

14 
-19

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 100001
const int inf=0x7fffffff;   //无限大

int main()
{
    sspeed;
    int n;
    while(cin>>n)
    {
        if(n==0)
            break;
        ll sum=0;
        ll ans=0;
        ll a;
        ll k=0;
        ll kiss=-maxn;
        int flag=0;
        for(int i=0;i<n;i++)
        {
            cin>>a;
            if(a>0)
            {
                flag=1;
            }
            else
            {
                kiss=max(kiss,a);
            }
            sum+=a;
            sum=max(sum,k);
            ans=max(ans,sum);
        }
        if(flag==0)
            cout<<kiss<<endl;
        else
            cout<<ans<<endl;
    }
    return 0;
}

 

posted @ 2015-01-21 10:35  qscqesze  阅读(243)  评论(0编辑  收藏  举报