C-Nana in Wonderland Series--Endless sweets

Source: Internet
Author: User

C - Nana in Wonderland Series--Endless sweets Time Limit: 2000/1000ms (java/others) Memory Limit: 128000/64000kb (java/others) Problem Description

Nana finally in your help "jump" over this lake, sure enough be okay, after the war will have a reply, survived, there will be crippled! Now in front of Nana is a lot of candy and some heibuliuqiu things! But Nana's eyes only eat the candy that never ends! Nana is going to jump up happily!

Then there was a girl with wings (Angel?) A bird man? Flew over, and Nana said, these candies are for you ~ (Nana has two eyes shining) ~ You can take ~ (Nana has shed saliva) ~ but ~ (God horse? And, but what? ~ The Fairy sister waved a wave of wings ~ floated across a cloud, candy and those Heibuliuqiu things flew up, fell to the ground into a strange figure.

Fairy Sister is very satisfied, turned to Nana said: "These candies and black hole (God Horse?"). Black hole? Divided into n heaps, each pile is either candy, or black hole, surrounded by a circle (that is, the 1th heap next to the nth heap and the 2nd heap), you can choose a number of successive piles, and then take away, but these black holes, will chanzui the child sucked in, you have to take candy to fall in and out. ”

Nana like candy, but do not like to move the brain ~ so the question to you, how to let Nana take the most candy?

Input

Multiple sets of data, first a positive integer T (t<=20) representing the number of data groups

For each set of data, including two rows, the first row is a positive integer n (3<=n<=100000) representing the number of heaps

The second line is n integers x[i] (1<=|x[i]|<=1000), if it is a positive integer, then this is a bunch of x[i] candy, if it is a negative integer, it is a need to use ABS (X[i]) candy to neutralize the black hole.

Output for each set of data, outputs an integer representing the maximum number of sweets that Nana can take away. Sample Input
351 2 3 4 551-2 3-4 55-1-2-3-4-5
Sample Output
1570
Hint

For example 1, Nana can take all the candies away, so the output 15 (=1+2+3+4+5)

For example 2, Nana can take the 1,2,3,5 heap of sweets, do not forget that it is placed into a circle, so output 7 (=1+ (-2) +3+5)

For example 3, waiting for Nana is 5 black holes, poor nana, a candy can not take off, so output 0

Due to the large input data, please use cin/cout carefully.

Test instructions: The number of n constitutes a ring, and the maximum number of substrings is asked.

Solution: First you have to learn the O (n) DP algorithm that is not the maximum substring of the ring. The ordinary maximum substring and the specific implementation of the following with Dp[i] to the end of the number of a[i] the maximum substring and transfer to Dp[i]=max (Dp[i-1]+a[i],a[i]), the expression in natural language is to consider whether you are following the previous or a new substring, If the interruption is better (that is, larger) than the previous one, then let yourself be the new substring, or you will follow the previous one. So what if it's a ring? We can find that the answer is only two, or do not need to go through the 1,n junction, so just as the normal substring and on the line, if through the 1,n junction? We will find the equivalent of the entire sequence to remove the middle of a continuous substring, to make the rest of the largest, then definitely let the stripped substring and the smallest, how to find the most small string and? Take all the numbers to the opposite number and run the maximum substring once again. Compare two scenarios, the biggest output is the answer.

1#include <stdio.h>2#include <string.h>3  4 intn,x[100005],dp[100005],sum;5  6 intMaxintAintb)7 {8     returnA>b?a:b;9 }Ten   One intSolve () A { -     intI,ma; -      for(i=0; i<n;i++) the     { -         if(i>0) -         { -Dp[i]=max (dp[i-1]+x[i],x[i]); +             if(ma<Dp[i]) -Ma=Dp[i]; +         } A         Else at         { -dp[i]=Max (dp[i],x[i]); -Ma=Dp[i]; -         } -     } -     returnMa; in } -   to intMain () + { -     intT,i,ma,mi; the      while(SCANF ("%d", &t)! =EOF) *     { $          while(t--)Panax Notoginseng         { -scanf"%d",&n); theMemset (DP,0,sizeof(DP)); +sum=0; A              for(i=0; i<n;i++) the             { +scanf"%d",&x[i]); -sum+=X[i]; $             } $Ma=solve (); Find the maximum substring and -Memset (DP,0,sizeof(DP)); -              for(i=0; i<n;i++) the             { -x[i]=-X[i]; Take counterWuyi             } themi=sum+solve (); Sum + the most kid string and -printf"%d\n", Max (Ma,mi)); Wu         } -     } About     return 0; $}

C-Nana in Wonderland Series--Endless sweets

Related Article

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.