题目链接:https://ac.nowcoder.com/acm/problem/16681
题目大意:
![图片说明](https://uploadfiles.nowcoder.com/images/20200605/7533514_1591350427159_FF1193FCCF3E9017B3BEABD957AA4124 "图片标题")
![图片说明](https://www.nowcoder.com/equation?tex=%5Cbegin%7Balign*%7D%5Clabel%7B2%7D%0A%26f%5Bi%5D%5Bj%5D%3A%E8%A1%A8%E7%A4%BA%E4%B8%AD%E5%BA%8F%E9%81%8D%E5%8E%86l~r%E5%8C%BA%E9%97%B4%E7%9A%84%E6%9C%80%E5%A4%A7%E5%80%BC%E3%80%82%E6%9E%9A%E4%B8%BE%E6%A0%B9k%E3%80%82%5C%5C%0A%26f%5Bi%5D%5Bj%5D%3Df%5Bi%5D%5Bk-1%5D*f%5Bk%2B1%5D%5Bj%5D%2Bc%5Bk%5D%5C%5C%0A%26%E5%B9%B6%E4%B8%94%E8%AE%B0%E5%BD%95g%5Bi%5D%5Bj%5D%E4%B8%BA%E6%A0%B9%E7%9A%84%E7%BC%96%E5%8F%B7%E3%80%82%0A%0A%5Cend%7Balign*%7D "图片标题")
#include <bits/stdc++.h> #define LL long long using namespace std; LL c[55], f[105][105], g[105][105]; LL DFS(int l, int r){ if(l>r) return 1; if(l==r){ g[l][r]=l; return c[l]; } if(f[l][r]!=-1) return f[l][r]; for(int k=l; k<=r; k++){ LL ans=max(f[l][r], DFS(l, k-1)*DFS(k+1, r)+c[k]); if(ans>f[l][r]){ f[l][r]=ans; g[l][r]=k; } } return f[l][r]; } void put(int l, int r){ if(l>r) return ; printf("%lld ", g[l][r]); put(l, g[l][r]-1); put(g[l][r]+1, r); } int main(){ memset(f, -1, sizeof(f)); int n; scanf("%d", &n); for(int i=1; i<=n; i++){ scanf("%lld", &c[i]); } DFS(1, n); printf("%lld\n", f[1][n]); put(1, n); return 0; }