C-Nana in Wonderland Series--Endless sweets

Source: Internet
Author: User

C-Nana in Wonderland Series--Endless sweetsTime Limit:2000/1000ms (java/others) Memory Limit: 128000/64000kb (java/others) Submit Statusproblem 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.

The problem is to find the maximal ring string and the extension of the method that uses dp[i] to seek the maximal substring. To find the maximum ring and substring, either exist in the 0~n of the largest continuous substring, if it exceeds the boundary n~0, you can know that there is a section of the most small string between the 0~n and, to remove this paragraph of the most small string and is the largest substring and. The problem is to find the maximum substring and the minimum string and, the maximum substring and the method is that dp[i] records the substring ending with I as the maximum and, Dp[i]=max (Dp[i-1]+num[i],num[i]), requires the most small string and words, we only need to the original data to take the opposite number, In order to find the maximum substring and, you can know the number of the 1~n between them, and then remove this paragraph only needs (the sum of the original data + This section of the most kid string and) can be.

1#include <iostream>2#include <stdio.h>3#include <math.h>4#include <stdio.h>5#include <string.h>6#include <stdlib.h>7 #defineMAX (x, y) (() > (y)? ( x):(y))8 #defineMIN (x, Y) ((×) < (y)? ( x):(y))9 using namespacestd;Ten intN; One intdp[1008600]; A intnum1[1008600]; - intnum2[1008600]; - intSEARCH_DP (intnum[]) the { -     intmax=0; -     intdp[1008600];/*Dp[i] records the maximum and the number of substrings ending with I*/ -dp[0]=num[0];  +      for(intI=1; i<n;i++) -     { +Dp[i]=max (dp[i-1]+num[i],num[i]);/*dp[i] Equals (Dp[i-1]+num[i] or num[i])*/ AMax=max (Dp[i],max);/*dp[i] Equals (Dp[i-1]+num[i] or num[i])*/ at     } -     returnMax; - } - intMain () - { -     intT,max; in     intMax1; -     intmax2,sum; toscanf"%d",&T); +      while(t--) -     { thescanf"%d",&N); *sum=0; $          for(intI=0; i<n;i++)Panax Notoginseng         { -scanf"%d",&num1[i]); thesum+=Num1[i]; +Num2[i]=-num1[i];/*Num2[i] num1[i] The opposite number of the original data*/ A   the         } +MAX1=SEARCH_DP (NUM1);/*using dynamic programming DP to find the maximum substring and*/ -MAX2=SEARCH_DP (Num2) +sum;/*find the Num1 and the "=" to the opposite number of the maximum substring and*/ $max=MAX (MAX1,MAX2); $printf"%d\n", Max); -   -     } the}
View Code

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.