Description
Give you N integers a1, a2 ... aN (|ai| <=1000, 1 <= i <= N).
You should output S.
Input
The input will consist of several test cases. For each test case, one integer N (2 <= N <= 100000) is given in the first line. Second line contains N integers. The input is terminated by a single line with N = 0.Output
For each test of the input, print a line containing S.Sample Input
5-5 9 -5 11 200
Sample Output
40
#include#include #include using namespace std; int left_v[100005]; int right_v[100005]; int value[100005]; int main() { int Test; while(cin>>Test) { if(!Test) break; left_v[0]=0; right_v[0]=0; left_v[Test+1]=0; right_v[Test+1]=0; int i,max_v=-100000000; for(i=1;i<=Test;i++) { cin>>value[i]; } left_v[1]=value[1]; right_v[Test]=value[Test]; for(i=2;i<=Test;i++) { left_v[i]=max(left_v[i-1]+value[i],value[i]); } for(i=Test-1;i>=1;i--) { right_v[i]=max(right_v[i+1]+value[i],value[i]); } for(i=2;i<=Test;i++) { left_v[i]=max(left_v[i-1],left_v[i]); } for(i=Test-1;i>=1;i--) { right_v[i]=max(right_v[i+1],right_v[i]); } for(i=1;i max_v) max_v=left_v[i]+right_v[i+1]; } cout< 自己把這道題A掉,相當開心。2015/7/5。