Maximum Sum
Problem Description
Given a set of n integers: A={a1, a2,..., an}, we define a function d(A) as below:
d(A) = max(sum(a[i] +a[i+1] + … + a[j])) ( 1 <= i <= j <= n).
Now your task is to calculate d(A).
Input
The input consists of T(T<=30) test cases. The number of testcases (T) is given in the first line of the input.
Each test case contains two lines. The first line is an integer n(1<=n<=50000). The second line contains n integers: a1, a2, ..., an. (|ai| <= 10000).
Output
Print exactly one line for each test case. The line should containthe integer d(A).
Sample Input
1
10
1 -1 2 2 3 -3 4 -4 5 -5
Sample Output
9
#include<stdio.h>
int main(){
int testCase,input_num;
int a[50010],max,sum;
//FILE *fp;
//fp = freopen("in.txt","r",stdin);
scanf("%d",&testCase);
while(testCase--){
scanf("%d",&input_num);
scanf("%d",&a[0]);
max = a[0];
for(int i = 1; i < input_num; ++i){
scanf("%d",&a[i]);
if(max < a[i]){
max = a[i];
}
}
if(max <= 0){
printf("%d\n",max);
}else{
sum = 0;
for(int i = 0; i < input_num;++i){
sum += a[i];
if(sum < 0){
sum = 0;
}
if(sum > max){
max = sum;
}
}
printf("%d\n",max);
}
}
return 0;
}