joj1995 Energy

 1995: Energy


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
3s10240K1366357Standard

Mr. Jojer is a very famous chemist. He is doing a research about behavior of a group of atoms. Atoms may have different energy and energy can be positive or negative or zero, e.g. 18 or -9. Absolute value of energy can not be more than 100. Any number of continuous atoms can form an atom-group. Energy of an atom-group is defined by the sum of energy of all the atoms in the group. All the atoms form an atom-community which is a line formed by all the atoms one by one. Energy of an atom-community is defined by the greatest energy of an atom-group that can be formed by atoms in the atom-community. The problem is, given an atom-community, to calculate its energy.

Input

The input contains several test cases. Each test case consists of two lines describing an atom-community. The first line of each test case contains an integer N(N<=1000000), the number of atoms in the atom-community. The second line of each test case contains N integers, separated by spaces, each representing energy of an atom, given in the order according to the atom-community. The last test case marks by N=-1, which you should not proceed.

Output

For each test case(atom-community description), print a single line containing the energy.

Sample Input

5
8 0 6 4 -1
-1

Sample Output

18

 


This problem is used for contest: 3  163 


Submit / Problem List / Status / Discuss

 

DP的经典问题,最大子串和。
 
 1 #include <stdio.h>
 2 
 3 int main()
 4 {
 5     int a[1000005];
 6     int n;
 7     int i;
 8     int maxsum, tmpsum;
 9 
10     while (scanf("%d", &n) == 0, n != -1)
11     {
12         tmpsum = 0;
13         maxsum = -10000;
14         for (i=0; i<n; ++i)
15         {
16             scanf("%d", &a[i]);
17         }
18 
19         for (i=0; i<n; ++i)
20         {
21             tmpsum += a[i];
22             if (tmpsum > maxsum)
23             {
24                 maxsum = tmpsum;
25             }
26             else if (tmpsum < 0)
27             {
28                 tmpsum = 0;
29             }
30         }
31         printf("%d\n", maxsum);
32     }
33 
34 
35     return 0;
36 }

 

 
 

转载于:https://www.cnblogs.com/RootJie/archive/2012/04/21/2462564.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值