代码
作者:
代码人生
,
2024-08-19 22:34:30
,
所有人可见
,
阅读 7
#include <iostream>
#include <cstring>
using namespace std;
typedef long long LL;
const int N = 2005;
int n;
int a[2][N];
LL s[2][N],g[2][N];
LL minn1[N],minn2[N];
int main(){
scanf("%d",&n);
for(int i=0;i<2;i++){
for(int j=1;j<=n;j++){
scanf("%d",&a[i][j]);
}
}
for(int i=0;i<2;i++){
for(int j=1;j<=n;j++){
s[i][j] = s[i][j - 1] + a[i][j];
}
for(int j=n;j>=1;j--){
g[i][j] = g[i][j + 1] + a[i][j];
}
}
LL ans = -1e18;
for(int i=1;i<=n;i++) minn1[i] = min(minn1[i - 1],s[0][i] + s[1][i]);
for(int i=n;i>=1;i--) minn2[i] = min(minn2[i + 1],g[0][i] + g[1][i]);
for(int i=2;i<n;i++){
for(int j=n-1;j>=2;j--){
if(i > j){
printf("%lld",ans);
return 0;
}
LL sum1 = s[0][i - 1] + s[1][i - 1],sum2 = g[0][j + 1] + g[1][j + 1];
ans = max(ans,sum1 - minn1[i - 1] + sum2 - minn2[j + 1] + s[1][j] - s[1][i - 1]);
}
}
return 0;
}
/*
4
-1 2 -3 4
5 6 7 8
*/