码迷,mamicode.com
首页 > 其他好文 > 详细

URAL 1296. Hyperjump(最大子序列和)

时间:2015-03-07 11:35:23      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:ural   数学   

题目链接: http://acm.timus.ru/problem.aspx?space=1&num=1296


Developed in the beginning of XXI century, hyperjump remains the primary method of transportation for distances up to thousands parsecs. But physicists have recently discovered an amazing phenomenon. They believe the duration of the hyperjump alpha phase can be easily controlled. Alpha phase is the period when hyper-spacecraft accumulates its gravity potential. The larger is the gravity potential accumulated, the less energy is required to complete the hyperjump. Your task is to write a program, which would help pilots decide when to enter and when to leave the alpha-phase, in order for the hyperspacecraft to accumulate the largest possible gravity potential.
The most crude gravity field model (which you will have to use) yields the sequence of integers pi, which represent field intensities at different moments in time. According to this model, if the alpha-phase begins at moment i and ends at moment j, then the value of gravity potential accumulated will be equal to the sum of sequence elements at places from i-th to j-th inclusive.

Input

The first line of the input contains an integer N being the number of elements in the intensity values sequence (0 ≤ N ≤ 60000). Next N lines specify sequence elements, each line containing a single integer pi (?30000 ≤ pi ≤ 30000).

Output

The only line of output contains the largest possible value of the gravity potential that can be accumulated by a hyperspacecraft during the alpha phase. You should assume that the initial gravity potential of a hyperspacecraft is equal to zero.

Samples

input output
10
31
-41
59
26
-53
58
97
-93
-23
84
187
3
-1
-5
-6
0

PS:

如果最小和小于零 ,输出零!

代码如下:

#include <cstdio>
int main()
{
    int n;
    int a[60047];
    while(~scanf("%d",&n))
    {
        for(int i = 0; i < n; i++)
        {
            scanf("%d",&a[i]);
        }
        int maxx = 0, sum = 0;
        for(int i = 0; i < n; i++)
        {
            sum+=a[i];
            if(sum <= 0)
            {
                sum = 0;
            }
            if(sum > maxx)
            {
                maxx = sum;
            }
        }
        printf("%d\n",maxx);
    }
    return 0;
}


URAL 1296. Hyperjump(最大子序列和)

标签:ural   数学   

原文地址:http://blog.csdn.net/u012860063/article/details/44114621

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!