Given a sequence ofK integers {N?1??,N?2??, ...,N?K?? }. A continuous subsequence is defined to be {N?I??,N?I+1??, ...,nj?? } where 1≤< Span class= "Mord mathit" >i≤j≤ k. The Maximum subsequence is the continuous subsequence which have the largest sum of its elemen Ts. For example, given sequence {-2, one, -4, -5, 2}, its maximum subsequence are {One, -4, all} with the largest sum bei Ng.
Now is supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.
Input Specification:
Each input file contains the one test case. Each case occupies the lines. The first line contains a positive integer K (≤10000). The second line contains K numbers, separated by a space.
Output Specification:
For each test case, output on one line the largest sum, together with the first and the last numbers of the maximum Subseq Uence. The numbers must is separated by one space, but there must is no extra space at the end of a line. In case the maximum subsequence are not unique and output the one with the smallest indices I and J (as shown by the sample case). If all The K numbers was negative, then it maximum sum is defined to being 0, and you were supposed to output the first and the last numbers of the whole sequence.
Sample Input:
10-10 1 2 3 4 -5 -23 3 7 -21
Sample Output:
10 1 4
1#include <stdio.h>2 intMaxsubsum (intA[],intn);3 intFirsti,endi;4 intMain ()5 {6 intn,a[10005],maxsum;7 while(SCANF ("%d", &n)! =EOF)8 {9 for(inti =0; I < n;i++)Tenscanf"%d",&a[i]); OneMaxsum =maxsubsum (a,n); A if(Maxsum! =-1) -printf"%d%d%d\n", Maxsum,a[firsti],a[endi]); - Else theprintf"0%d%d\n", A[firsti],a[endi]); - } - return 0; - } + intMaxsubsum (intA[],intN) - { + intThissum =0, maxsum=-1; A intfirsti=-1; atFirsti =0; Endi = n1; - for(inti =0; I < n;i++){ -Thissum + =A[i]; - if(Maxsum <thissum) { -Maxsum =thissum; -Endi =i; inFirsti = Firsti +1; - } to Else if(Thissum <0) + { -Thissum =0; theFirsti =i; * } $ }Panax Notoginseng returnmaxsum; -}
Maximum child column and CT 01-complexity 2 Maximum subsequence Sum