The first thought was to enumerate the root nodes and then memorize the search. The results of tle, and finally a glance at the puzzle moment understand. Alas, the mind is too limited.
Since the data is arranged from small to large, a binary sort tree can be formed naturally.
The dp[i][j] is the minimum value of the BST that can be composed of the elements of the interval [i,j], then the result of the large interval is related to the root node and the results between the cells, it is obvious that the interval DP,
Transfer equation lap dp[i][j] = Min{dp[i][k-1] + dp[k+1][j] + sum (i,j)-a[k]} sum (I,J) is the interval and, because when the two trees are connected to the left and right
On the root node, the height of itself is increased by 1, so each element is evaluated more than once, and the last root node is subtracted from the root node because the number of layers is 0.
#include <cstdio>#include<iostream>#include<algorithm>#include<vector>#include<cstring>#include<cmath>#include<sstream>#include<queue>#include<stack>#defineINF 530600414#defineN 201314using namespaceStd;typedefLong LongLl;typedef pair<int,int>PII;Const intMAXN =255;intN;intA[MAXN];intDP[MAXN][MAXN];intSUM[MAXN];intMain () {//freopen ("in", "R", stdin); while(~SCANF ("%d",&N)) {sum[0] =0; for(inti =1; I <= N; ++i) {scanf ("%d", &A[i]); Sum[i]= sum[i-1] +A[i]; } //sort (a+1,a+n+1); if(n = =1) dp[1][n] =0; Else{memset (DP,0,sizeof(0)); //for (int i = 1; I <= n; ++i) dp[i][i] = A[i]; for(intL =2; L <= N; ++l) for(intSt =1; st+l-1<= N; ++St) { inted = st+l-1; Dp[st][ed]=INF; intCNT = sum[ed]-sum[st-1]; for(intK = st; K <= Ed; ++k) dp[st][ed]= Min (dp[st][ed],dp[st][k-1] + dp[k+1][ed] + CNT-A[k]); }} printf ("%d\n", dp[1][n]); }}
UVA10304---(Interval dp)