poj 2479 Maximum sum

#include<iostream>
#include
<stdio.h>
using namespace std;
int num[50010],l[50010],r[50010];
#define Min -9999999
int main()
{
int T,n,sum,tmp;
cin
>>T;
while(T--)
{
scanf(
"%d",&n);
for(int i=1;i<=n;++i)
scanf(
"%d",&num[i]);

sum
=0;tmp=Min; //l[i]表示[1,i]中最大的子段和
for(int i=1;i<=n;++i)
{
sum
+=num[i];
if(tmp<sum)
tmp
=sum;
l[i]
=tmp;
if(sum<0)
sum
=0;
}

sum
=0;tmp=Min; //r[i]表示[i,n]中最大的子段和
for(int i=n;i>=1;--i)
{
sum
+=num[i];
if(tmp<sum)
tmp
=sum;
r[i]
=tmp;
if(sum<0)
sum
=0;
}

int Max=Min;
for(int i=1;i<n;++i)
{
if(Max<l[i]+r[i+1])
Max
=l[i]+r[i+1];
}
printf(
"%d\n",Max);
}
return 0;
}

  

posted on 2011-07-18 11:41  sysu_mjc  阅读(111)  评论(0编辑  收藏  举报

导航