main.cpp
1 #include<stdio.h> 2 #define max 10001 3 int main() 4 { 5 long s,sum,i,a[max],n,x,y,x1; 6 while(scanf("%ld",&n)>0&&n) 7 { 8 for(i=1;i<=n;i++) 9 scanf("%ld",&a[i]); 10 i=1; 11 s=sum=0; 12 x=1; 13 y=1; 14 while(a[i]<=0&&i<=n) 15 { 16 if(a[i]==0) 17 x=y=i; 18 i++; 19 } 20 if(i>n) 21 { 22 if(a[x]==0&&a[y]==0) 23 printf("0 0 0\n"); 24 else 25 printf("0 %ld %ld\n",a[x],a[n]); 26 } 27 else 28 { 29 x1=x=y=i; 30 while(i<=n) 31 { 32 while(a[i]>0&&i<=n) 33 sum+=a[i++]; 34 if(sum>s) 35 { 36 x=x1; 37 y=i-1; 38 s=sum; 39 } 40 while(a[i]<=0&&i<=n) 41 sum+=a[i++]; 42 if(sum<=0) 43 { 44 x1=i; 45 sum=0; 46 } 47 } 48 printf("%ld %ld %ld\n",s,a[x],a[y]); 49 } 50 } 51 return 0; 52 }