Training question
The title says that at most a sub-interval can be reversed. What is the maximum sum?
Let's set the meaning of the dp array to the interval of the previous i and the meaning of c[i][j] to reverse the interval from i to j. Interval sum
c[i][j]=c[i+1][j-1]+a[i]*b[j]+a[j]*b[i]
#include<stdio.h>
#include<algorithm>
using namespace std;
const int N=1e3;
long long dp[N*6+20],c[N*5+100][N*5+100];
long long a[N*6+20],b[N*6+20];
int main()
{
int i,j,k,m,n;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%lld",&a[i]);
for(i=1;i<=n;i++)
{
scanf("%lld",&b[i]);
c[i][i]=a[i]*b[i];
dp[i]=dp[i-1]+a[i]*b[i];
}
long long ans=dp[n];
for(int len=2;len<=n;len++)
{
for(i=1;i<=n-len+1;i++)
{
j=i+len-1;
c[i][j]=c[i+1][j-1]+a[i]*b[j]+a[j]*b[i];
long long sum=dp[i-1]+dp[n]-dp[j];//要减去从i到j这段区间的乘积的和
sum+=c[i][j];//再加上反转后的和
ans=max(ans,sum);//求最大值
}
}
printf("%lld
",ans);
return 0;
}