G - Profits

Description

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 (1N250, 000) on its own line, indicating the number of days. On each of the next N lines will be a single integer P (- 100P100), 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<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 250005
#define INF 0x7fffffff

using namespace std;

int main()
{
    int n;
    int f[N],g[N];
    while(scanf("%d",&n),n!=0)
    {
        for(int i=1;i<=n;i++)
            scanf("%d",&g[i]);
        int ans=g[1];
        for(int i=2;i<=n;i++)
        {
            if(g[i-1]+g[i]>g[i]) g[i]=g[i-1]+g[i];
            if(ans<g[i]) ans=g[i];
        }
        printf("%d\n",ans);
    }
    return 0;
}


你可能感兴趣的:(G - Profits)