【题目链接】
【算法】
树形DP即可
【代码】
#include<bits/stdc++.h>
using namespace std;
#define MAXN 50
int i,j,k,n,l,r,mid;
int d[MAXN],f[MAXN][MAXN],root[MAXN][MAXN];
template <typename T> inline void read(T &x) {
int f = 1; x = 0;
char c = getchar();
for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
x *= f;
}
template <typename T> inline void write(T x) {
if (x < 0) { putchar('-'); x = -x; }
if (x > 9) write(x/10);
putchar(x%10+'0');
}
template <typename T> inline void writeln(T x) {
write(x);
puts("");
}
inline void dfs(int l,int r) {
if (l <= r) {
putchar(' ');
write(root[l][r]);
dfs(l,root[l][r]-1);
dfs(root[l][r]+1,r);
}
}
int main() {
read(n);
for (i = 0; i <= n; i++) {
for (j = 0; j <= n; j++) {
f[i][j] = 1;
}
}
for (i = 1; i <= n; i++) {
read(d[i]);
f[i][i] = d[i];
root[i][i] = i;
}
for (k = 2; k <= n; k++) {
for (l = 1; l <= n - k + 1; l++) {
r = l + k - 1;
for (mid = l; mid <= r; mid++) {
if (f[l][mid-1] * f[mid+1][r] + d[mid] > f[l][r]) {
f[l][r] = f[l][mid-1] * f[mid+1][r] + d[mid];
root[l][r] = mid;
}
}
}
}
writeln(f[1][n]);
write(root[1][n]);
dfs(1,root[1][n]-1);
dfs(root[1][n]+1,n);
return 0;
}