Max SumTime
limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 153057 Accepted Submission (s): 35699
Problem Descriptiongiven 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.
Inputthe 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 is b etween-1000 and 1000).
Outputfor Each test case, you should output of the lines. The first line was "Case #:", # means the number of the the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end posi tion of the sub-sequence. If there is more than one result, output the first one. Output a blank line between cases.
Sample Input
25 6-1 5 4-77 0 6-1 1-6 7-5
Sample Output
Case 1:14 1 4Case 2:7 1 6
Authorignatius.l
Recommendwe carefully selected several similar problems for you:1176 1069 1058 1159 1257
AC CODE:
#include <stdio.h>main () { int n, I, a[100002], B, J, S, M, K; Long Max, sum; while (scanf ("%d", &n)! = EOF) { for (j = 1; J <= N; j + +) { scanf ("%d", &m); for (i = 0; i < m; i++) scanf ("%d", &a[i]); sum = 0; b = 1; max = -1001; for (i = 0; i < m; i++) { sum + = A[i]; if (Sum > Max) { max = sum; K = b; s = i + 1; } if (Sum < 0) { sum = 0; b = i + 2; } } if (j! = N) printf ("Case%d:\n%ld%d%d\n\n", J, Max, K, s); else printf ("Case%d:\n%ld%d%d\n", J, Max, K, s);}}
Hdu 1003 Max Sum