Topic description
Background: May 12, under the warm welcome of the people of Xiamen, the Olympic torch symbolizing peace, friendship and holiness finally came to Xiamen and began to pass ...
Description: The mayor of Xiamen announced: "The torch relay to do very meaningful." What do you mean, "meaningful"? The mayor of Xiamen ingenuity gave it a definition: each torchbearer has a meaning value, they can be regarded as a first and last ring, and the value of the torch relay is the maximum and continuous substring of this ring.
For example: The string is-2 2 0 1-48 1, apparently its largest and contiguous substring is 2 0 1, and its and is 3.
Now given the value of the N torch, please figure out the value of the entire torch relay (meaning may be negative) input format
Line 1th an integer n (1<=n<=1000000) represents n a torch hand
Line 2nd has n integers, and each integer is within the range of Longint.
New note: Due to unknown reasons, the normal program is tle at 8 9 103 test points, so the following 3 test points are temporarily closed. Change data range to 5000 output format
The value of the torch relay. Sample input
2 1 3 Sample output
4
Three-dimensional state image
The eldest son of string and, the ring for the chain
#include <iostream> #define MAXN 5000 using namespace std; long a[2* (maxn+1)],ans; int main () {long n; cin>> N for (long i=1;i<=n;++i) {cin>>a[i]; if (a[i]<0) ++ans} if (ans==n) {ans=int_min; for (long I=1;i<=n;++i) if (A[i]>ans) ans=a[i]; else{ans=0 for (long I=1;i<=n;++i) a[i+n]=a[i], for (long Start=1;start<=n;++start) {long temp=0; Art;i-start+1<=n;++i) {if (temp+a[i]<0) temp=0; else temp+=a[i]; if (Temp>ans) ans=temp;}} } cout<<ans<<endl; return 0; }