一道树型dp,方程在程序里有,注意循环的顺序和初始化。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
long long dp[35][35],n,w[35],c[35][35];
void find(long long left,long long right)
{
if (left>right) return;
printf("%d ",c[left][right]);
if (left!=right)
{
find(left,c[left][right]-1);
find(c[left][right]+1,right);
}
return;
}
int main()
{
memset(dp,0,sizeof(dp));
memset(c,0,sizeof(c));
scanf("%lld",&n);
for (long long i=1;i<=n;i++)
scanf("%lld",&w[i]);
for (long long i=1;i<=n;i++)
{
dp[i][i]=w[i];
dp[i][i-1]=1;
c[i][i]=i;
}
for (long long j=1;j<=n;j++)
for (long long i=j-1;i>=1;i--)
for (int k=i;k<=j;k++)
{
if (dp[i][j]<dp[i][k-1]*dp[k+1][j]+w[k])
{
dp[i][j]=dp[i][k-1]*dp[k+1][j]+w[k];
c[i][j]=k;
}
}
printf("%lld\n",dp[1][n]);
find(1,n);
return 0;
}