显示代码纯文本
/*
Name: 双子序列最大和
Copyright:
From:cogs
Author: Go灬Fire
Date: 24/10/16 07:54
Description:2015笔试填空第一题
很显然,以i为两个序列的分界,l[i-1]是i左边的连续序列的最大和,r[i+1]是 i右边连续序列的最大和
效率O(N)可以过
*/
#include<cmath>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=105000;
int n,a[maxn],l[maxn],r[maxn];
void Init();
int main(){
freopen("Pengshuangcang.in","r",stdin);
freopen("Pengshuangcang.out","w",stdout);
Init();
getchar();getchar();
return 0;
}
void Init(){
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
int Max=0,sum=0;
for(int i=1;i<=n;i++){
Max+=a[i];l[i]=sum;
if(Max<0){l[i]=0;Max=0;}
if(Max>sum){sum=Max;l[i]=sum;}
}
sum=Max=0;
for(int i=n;i>=1;i--){
Max+=a[i];r[i]=sum;
if(Max<0){r[i]=0;Max=0;}
if(Max>sum){sum=Max;r[i]=sum;}
}
int ans=0;
for(int i=2;i<n;i++)ans=max(ans,l[i-1]+r[i+1]);
printf("%d\n",ans);
}