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: For a ring sequence, the maximal contiguous sub-segment and.
Idea: Set arr[0] and arr[n-1] This place is a notch.
There are two possible ways:
(1) The answer does not go through the gap. Then the maximum sub-segment and calculation can be followed.
(2) The answer passes through the gap. Then the most small number of negative numbers and will not pass through the gap. will take all negative, and then follow the method (1) to find the most small section and. Then subtract the sum of the small and the small from the sum of the original ring sequence.
1#include <bits/stdc++.h>2 using namespacestd;3 Const intn=100005;4 intArr[n], T, N;5 intcal ()6 {7 intCnt=0, sum=0;8 for(intI=0; i<n; i++)9 {Tensum+=Arr[i]; One if(sum>cnt) cnt=sum; A if(sum<0) sum=0; - } - intcnt1=0, sum1=0, total=0; the for(intI=0; i<n; i++) - { -total+=Arr[i]; -sum1+=-Arr[i]; + if(SUM1>CNT1) cnt1=sum1; - if(sum1<0) sum1=0; + } A at returnCnt> (total+cnt1)? cnt:total+cnt1; - } - - - intMain () - { in //freopen ("Input.txt", "R", stdin); -Cin>>T; to while(t--) + { -scanf"%d",&n); the for(intI=0; i<n; i++) scanf ("%d",&arr[i]); *printf"%d\n", Cal ()); $ }Panax Notoginseng return 0; -}
AC Code
Acdream 1682 sweets to eat (Ring Max sub-segment and)