Max Sum (maximum substring and)

Source: Internet
Author: User

Max Sum

Time Limit: 1000MS Memory Limit:32768KB 64bit IO Format:%i64d &%i64u

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 is b etween-1000 and 1000).

Output

For the 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 4 case 2:7 1 6 Analysis state transition equation Dp[i] = max (Dp[i-1] + a[i], a[i] )
1#include <stdio.h>2#include <string.h>3 4 inta[100002];5 6 Main ()7 {8     intt,n,kkk=1, I,j;9 Tenscanf"%d",&T); One      while(t--) A     { -         intmax,max_s,max_e,sum,sum_s; -         //memset theprintf"Case %d:\n", kkk++); -  -scanf"%d",&N); -          for(i=1; i<=n;i++) +         { -scanf"%d",&a[i]); +         } A  atmax = SUM = a[1]; -max_s = Max_e = sum_s =1; -  -         //printf ("sum =%d max =%d\n", Sum,max); -          for(i=2; i<=n;i++) -         { in             if(sum + a[i] <A[i]) -             { tosum =A[i]; +sum_s =i; -             } the             Elsesum = sum +A[i]; *  $             if(Sum >max)Panax Notoginseng             { -Max =sum; themax_s =sum_s; +Max_e =i; A             } the             //printf ("sum =%d max =%d\n", Sum,max); +         } -printf"%d%d%d\n", max,max_s,max_e); $         if(T) $printf"\ n"); -     } -}

Max Sum (maximum substring and)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.