http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1483
代码:
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<map> #include<cmath> #define LL long long using namespace std; const int N=100005; LL a[N],l1[N],l2[N],r1[N],r2[N]; int main() { //freopen("data.in","r",stdin); int n; while(cin>>n) { for(int i=1;i<n;++i) cin>>a[i]; l1[0]=r1[0]=l1[n]=r1[n]=0; l2[0]=r2[0]=l2[n]=r2[n]=0; for(int i=1;i<n;++i) { if(a[i]%2==1) { l1[i]=a[i]+max(l1[i-1],l2[i-1]); if(a[i]>=2) l2[i]=a[i]+l2[i-1]-1; else l2[i]=0; } else { if(a[i]>0) l1[i]=a[i]+max(l1[i-1],l2[i-1])-1; else l1[i]=0; if(a[i]>=2) l2[i]=a[i]+l2[i-1]; else l2[i]=0; } } for(int i=n-1;i>=1;--i) { if(a[i]%2==1) { r1[i]=a[i]+max(r1[i+1],r2[i+1]); if(a[i]>=2) r2[i]=a[i]+r2[i+1]-1; else r2[i]=0; } else { if(a[i]>0) r1[i]=a[i]+max(r1[i+1],r2[i+1])-1; else r1[i]=0; if(a[i]>=2) r2[i]=a[i]+r2[i+1]; else r2[i]=0; } } LL ans=0; for(int i=0;i<n;++i) ans=max(ans,max(l1[i],l2[i])+max(r1[i+1],r2[i+1])); cout<<ans<<endl; } return 0; }