You know, the maximum and the minimum is relative, the sum minus the smallest can get the largest. The problem of programming beauty is not understood, and then reference to the http://zhangpeizhen.blog.163.com/blog/static/231873112201431784024921/
Two cases
1, ordinary array, can O (n) to find the maximum substring and.
2, if it is a ring, then to see across the n-1 to 0 of this ring of the situation, the requirement of the largest sum, only need to use the total minus the non-ring of the most small string and can.
Then take the maximum value of two cases.
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<string>using namespacestd;voiddp () {inti,t1,t2; T1= arra[0]; T2= arra[0]; Max=T1; Min=T2; for(i =1; I < n; i + +) { if(T1 <=0) T1=Arra[i]; ElseT1+=Arra[i]; if(T2 >=0) T2=Arra[i]; ElseT2+=Arra[i]; Max=Max (T1,max); Min=min (t2,min); } Max= Max (max,total-Min); printf ("%d\n", Max (Max,0));} intMain () { while(~SCANF ("%d",&N)) { Total=0; for(inti =0; I < n; i + +) {scanf ("%d",&Arra[i]); Total+=Arra[i]; } DP (); } return 0;}
But I think less of a case, such as an array of all-1,
Normal array maximum substring and is-1,
In accordance with the largest ring of the above must be 0, but this is nothing to take the state,
So I think the last thing I need to decide is whether the most small string and the whole array and equal, if the equality is the case, then take the maximum substring of the normal array and the line.
Ring array maximum substring and maximum and minimum are relative