跟某NOIP的《矩阵取数游戏》很像。

f(i,j)表示从左边取i个,从右边取j个的答案。

f[x][y]=max(dp(x-1,y)+a[x]*(x+y),dp(x,y-1)+a[n-y+1]*(x+y))。

ans=max{f(i,n-i)}。

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define N 2001
int n,a[N],f[N][N],ans;
int dp(int x,int y)
{
    if(f[x][y]!=-1) return f[x][y];
    if((!x)&&(!y)) return f[x][y]=0;
    if(!x) return f[x][y]=dp(x,y-1)+a[n-y+1]*(x+y);
    if(!y) return f[x][y]=dp(x-1,y)+a[x]*(x+y);
    return f[x][y]=max(dp(x-1,y)+a[x]*(x+y),dp(x,y-1)+a[n-y+1]*(x+y));
}
int main()
{
    memset(f,-1,sizeof(f));
    scanf("%d",&n);
    for(int i=1;i<=n;++i)
      scanf("%d",&a[i]);
    for(int i=0;i<=n;++i)
      ans=max(ans,dp(i,n-i));
    printf("%d\n",ans);
    return 0;
}

相关文章:

  • 2022-12-23
  • 2021-07-04
  • 2021-10-22
  • 2022-12-23
  • 2022-12-23
  • 2021-10-28
  • 2021-08-06
猜你喜欢
  • 2021-12-20
  • 2021-11-30
  • 2021-09-20
  • 2022-12-23
  • 2021-08-05
  • 2022-12-23
  • 2022-01-22
相关资源
相似解决方案