# Max Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 218423    Accepted Submission(s): 51573

Problem Description
Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.

Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).

Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.

Sample Input
2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5

Sample Output
Case 1:
14 1 4

Case 2:
7 1 6

Author
Ignatius.L

Recommend
We have carefully selected several similar problems for you:  1176 1087 1069 1058 1203

if(dp[i-1]>0)
dp[i]=dp[i-1]+a[i];
else
dp[i]=a[i];

#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#define N 100010
using namespace std;

int a[N],dp[N];

int main()
{
int T,t=1;
scanf("%d",&T);
while(T--)
{
int n,s,e,i,e1,s1;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);

memset(dp,0,sizeof(dp));
dp[0]=-1001;           //因为序列的最小值为-1000，所以为了确保不会取到dp[0]，就将dp[0]设为-1001
int max1=dp[0];
for(i=1;i<=n;i++)
{
if(dp[i-1]>=0)          //延续就序列，更新终点
{
dp[i]=dp[i-1]+a[i];
e1=i;

}
else
{
dp[i]=a[i];           //新序列，更新起点和终点
{
s1=i;
e1=i;
}
}
if(dp[i]>max1)           //更新最大值和结果
{
max1=dp[i];
s=s1;
e=e1;
}
}

printf("Case %d:\n",t);
printf("%d %d %d\n",max1,s,e);
if(T!=0)
printf("\n");
t++;
}
return 0;
}

• 本文已收录于以下专栏：

## HDU-1003-Max Sum

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su...

## HDU 1003 Max Sum【区间最大值】题解

HDU 1003 Max Sum【区间最大值】题解

## hdu 1003 Max Sum 去掉数组后的方法

Max Sum Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Su...

## 【最大连续子序列和dp】hdu 1003 Max Sum

http://acm.hdu.edu.cn/showproblem.php?pid=1003 最大连续子序列和，dp[i] = max(dp[i-1]+a[i],a[i]),注意维护st /* ...

## HDU 1003 — Max Sum

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)