登录后复制 #include<iostream> #include<cstring> #include<algorithm> using namespace std; const int N=35; int n; int w[N],f[N][N],g[N][N]; void dfs(int l,int r){ if(l>r)return; int k=g[l][r]; cout<<k<<' '; dfs(l,k-1); dfs(k+1,r); } int main(){ cin>>n; for(int i=1;i<=n;i++)cin>>w[i]; for(int len=1;len<=n;len++){ for(int i=1;i+len-1<=n;i++){ int j=i+len-1; if(len==1){ f[i][j]=w[i]; g[i][j]=i; } else{ for(int k=i;k<=j;k++){ int left=k==i?1:f[i][k-1]; int right=k==j?1:f[k+1][j]; int score=left*right+w[k]; if(score>f[i][j]){ f[i][j]=score; g[i][j]=k; } } } } } cout<<f[1][n]<<endl; dfs(1,n); } 1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40. 原创作者: u_16969274 转载于: https://blog.51cto.com/u_16969274/11813512