Pku acm 2479-Maximum Sum

Source: Internet
Author: User

A Dynamic Planning question

Http://acm.pku.edu.cn/JudgeOnline/problem? Id = 2479

 

Maximum Sum
Time limit:1000 ms   Memory limit:65536 K
Total submissions:17327   Accepted:5239

Description

Given a set of N integers: A = {A1, A2,..., an}, we define a function D (A) as below:

The formula is invisible, that is, the maximum value of the sum of the two sub-arrays in array.

 

Your task is to calculate d ().

Input

The input consists of T (<= 30) test cases. The number of test cases (t) is given in the first line of the input.
Each test case contains two lines. the first line is an integer N (2 <= n <= 50000 ). the second line contains N integers: A1, A2 ,..., an. (| ai | <= 10000 ). there is an empty line after each case.

Output

Print exactly one line for each test case. The line shoshould contain the integer d ().

Sample Input

1101 -1 2 2 3 -3 4 -4 5 -5

Sample output

13

Hint

In the sample, we choose {2, 2, 3,-3, 4} and {5}, then we can get the answer.

Huge input, scanf is recommended.

Solution Report: Find the largest sum of array Neutron arrays, which is mentioned in the beauty of programming. However, what is required here is the maximum sum of the two sub-arrays. It is traversed twice. During the first traversal, the greatest sum of the sub-arrays in the array before each number is obtained, during the second traversal, obtain the largest sum of the subarrays in the array after each number, and add the largest sum to the subarrays before it to obtain the final maximum value. The first traversal goes from front to back, and the second traversal goes from back to back. For the first traversal, the maximum sum of the subarray that contains a [I] must be recorded before start [I]: A [I. All [I]: the largest sum of sub-arrays before a [I. The second traversal directly calculates nstart, Nall, maximum value = Nall + ALL [I-1] complexity analysis, O (N) # Include <iostream> <br/> using namespace STD; </P> <p> # define N-500000001 </P> <p> int returnmax (int, int B) <br/>{< br/> return A> B? A: B; <br/>}</P> <p> int main () <br/>{< br/> int casenum; <br/> CIN> casenum; <br/> while (casenum --) <br/> {<br/> int max = N; <br/> int N; <br/> CIN> N; <br/> int * A = new int [N]; <br/> int * Start = new int [N]; <br/> int * All = new int [N]; <br/> int nstart; <br/> int Nall; <br/> for (INT I = 0; I <n; I ++) <br/>{< br/> scanf ("% d", & A [I]); <br/> if (I> 0) <br/> {<br/> Start [I] = returnmax (A [I], start [I-1] + A [I]); <br/> All [I] = returnmax (ALL [I-1], start [I]); <br/>}< br/> else <br/>{< br/> Start [I] = A [I]; <br/> All [I] = A [I]; <br/>}< br/> for (INT I = n-1; I> 0; I --) <br/>{< br/> if (I = N-1) <br/>{< br/> nstart = A [I]; <br/> Nall = A [I]; <br/> max = returnmax (max, Nall + ALL [I-1]); <br/>}< br/> else <br/> {<br/> nstart = returnmax (A [I], nstart + A [I]); <br/> Nall = returnmax (Nall, nstart); <br/> max = returnmax (max, Nall + ALL [I-1]); <br/>}< br/> printf ("% d/N", max); <br/> Delete []; <br/> Delete [] Start; <br/> Delete [] All; <br/>}< br/>}

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.