# 最大子序列和（枚举，分治，DP）

int MaxSubseqSum1(int A[],int N)
{
int ThisSum,MaxSum=0;
int i,j,k;
for(i=0;iMaxSum) //更新结果
MaxSum=ThisSum;
}
}
return MaxSum;
}

int MaxSubseqSum2(int A[],int N)
{
int ThisSum,MaxSum=0;
int i,j;
for(i=0;iMaxSum) MaxSum=ThisSum;
}
}
return MaxSum;
}

int Max(int a,int b,int c)
{
if(a>b&&a>c) return a;
else if(b>a&&b>c) return b;
else return c;
}

int MaxSubSum(int A[],int left,int right)
{
int MaxLeftSum,MaxRightSum;
int MaxLeftBorderSum,MaxRightBorderSum;
int LeftBorderSum,RightBorderSum;
int mid,i;

if(left==right){
if(A[left]>0) return A[left];
else return 0;
}

mid=(right+left)/2;
MaxLeftSum=MaxSubSum(A,left,mid);
MaxRightSum=MaxSubSum(A,mid+1,right);

MaxLeftBorderSum=0;
LeftBorderSum=0;
for(i=mid;i>=left;--i){
LeftBorderSum+=A[i];
if(LeftBorderSum>MaxLeftBorderSum) MaxLeftBorderSum=LeftBorderSum;
}

MaxRightBorderSum=0;
RightBorderSum=0;
for(i=mid+1;i<=right;++i){
RightBorderSum+=A[i];
if(RightBorderSum>MaxRightBorderSum) MaxRightBorderSum=RightBorderSum;
}
return Max(MaxLeftSum,MaxRightSum,MaxLeftBorderSum+MaxRightBorderSum);
}

最后得到的状态转移方程为：

MaxSum[i] = Max{ MaxSum[i-1] + A[i], A[i]}

int MaxSubseqSum(int A[],int N)
{
int maxsum=0,sum=0;
for(int i=0;i<N;++i){
sum+=A[i];
if(sum>maxsum) maxsum=sum;
else if(sum<0) sum=0;
}
return maxsum;
}