#include<stdio.h>
int main()
{
int n,i,t
,sum=0,max=0,max_t=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&t);
sum+=t;
max_t+=t;
if(max_t<0) max_t=0;
if(max_t>max) max=max_t;
}
printf("%d\n",2*max-sum);
}
要透过复杂的包装,看到问题的本质,就可以避免很多弯路。磨刀不误砍柴工,问题想清楚了再下手。
#include<stdio.h>
int main()
{
int n,i,t
,sum=0,max=0,max_t=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&t);
sum+=t;
max_t+=t;
if(max_t<0) max_t=0;
if(max_t>max) max=max_t;
}
printf("%d\n",2*max-sum);
}
要透过复杂的包装,看到问题的本质,就可以避免很多弯路。磨刀不误砍柴工,问题想清楚了再下手。