/*based on the nature of the middle sequence traversal and the quasi-recursive processing of the subtree well, it's a stone merge. However, the boundary is not always written and the cycle sequence is written in the memory .... The optimal value of each interval is separated by that point, that is, who is the root of the subtree and then recursively outputs to note that the left subtree is handed to the right subtree .*/#include<iostream>#include<cstdio>#include<cstring>#defineMAXN 35using namespacestd;intN,f[maxn][maxn],v[maxn],c[maxn][maxn],pre[maxn],len;voidDfs (intLintR) { if(L>r)return; intk=C[l][r]; pre[++len]=K; Dfs (L,k-1);D FS (k +1, R);}intDP (intLintR) { if(r<l)return 1; if(F[l][r])returnF[l][r]; for(inti=l;i<=r;i++) { intK=DP (l,i-1) *DP (i+1, R) +V[i]; if(k>F[l][r]) {F[l][r]=k;c[l][r]=i; } } returnf[l][r];}intMain () {//freopen ("jfecs.in", "R", stdin); //freopen ("Jfecs.out", "w", stdout);scanf"%d",&N); for(intI=1; i<=n;i++) scanf ("%d",&V[i]); for(intI=1; i<=n;i++) F[i][i]=v[i],c[i][i]=i; printf ("%d\n", DP (1, N)); Dfs (1, N); for(intI=1; i<=n;i++) printf ("%d", Pre[i]); return 0;}
Codevs 1090 plus two fork tree