For maximum child column and problems, we should be very familiar. With DP algorithm can very quickly and easily find the problem SUM, but for the beginning and end of a column, or a problem worthy of serious consideration.
A not required at the beginning, ending sequence, the situation only requires values
To test points on the PTA as an example:
In fact, the difficulty is that the number of columns negative, 0 mixed with three conditions are all negative 0, all for.
If all 0, the output is 000;
When the whole is negative, then the output is 0 a [0] a [n -1]
If negative mix 0, then the output is 000.
So it is natural to think of, so that the two variables results left = right = 0, then set a flag when detecting the full negative, left = a [0], right = a [n-1].
Then the code is easy to write out:
#include<stdio.h>
int f4(int a[],int n){
int i,max=0,now=0,l=0,r=0,flag=0;
a[10001]=0;a[10002]=0;
for(i= 0;i<n;i++){
if(a[i]>=0)
flag=1;
if(now<0){
l=i;
r=i;
now=a[i]; //刚吸收的数可能非常大,所以必须更新l与r
}
else {
r=i;
now+=a[i];
}
if(now>max){
max=now;
a[10002]=a[r];
a[10001]=a[l];
}
}
if(!flag){
a[10001]=a[0];
a[10002]=a[n-1];
}
return max;
}
int main(){
int i,n;
scanf("%d",&n);
int a[10002];
for(i=0;i<n;i++)
scanf("%d",&a[i]);
int sum=f4(a,n);
printf("%d %d %d",sum,a[10001],a[10002]);
return 0;
}