Address: http://zsyz.openjudge.cn/dp/23/
/* Author: bob lee2012.9.20 ======================================== ============= this is a Chinese question, I don't need to explain it. This is an interval DP. We use F [I] [J] to represent the maximum energy value from I to J. Then its subproblem is to divide I to J into two parts. then, the maximum value of the input fields multiplied by the input values can be used to export the state transition equation f [I] [J] = max (F [I] [k] + F [k + 1] [J] + data [i] * Data [k + 1] * Data [J + 1]) (1 <= I <= k <j <= 2n-1) where data [I] indicates the header of the I, And I + 1 is the end, that is, this necklace is a ring, so the head is not necessarily 1, so we are in 1 ~ N plus one so we only need to enumerate f [I] [n + I-1] (1 <= I <= N) obtain the maximum value */# include <iostream> # include <cstdio> # include <cstdlib> using namespace STD; # define maxn 105 # define INF 1 <30int f [2 * maxn] [2 * maxn]; int data [2 * maxn]; int N; void Update (Int & X, int y) {If (Y> X) x = y;} int main () {// while (scanf ("% d ", & N )! = EOF) scanf ("% d", & N); {int I, j; for (I = 1; I <= N; I ++) {scanf ("% d", & Data [I]); Data [I + N] = data [I];} for (I = 1; I <= 2 * n; I ++) {for (j = 1; j <= 2 * n; j ++) {if (I = J) f [I] [J] = 0; else f [I] [J] = 0 ;}} int Len; For (LEN = 1; Len <n; Len ++) {for (I = 1; I <= 2 * n-len; I ++) {J = I + Len; int K; For (k = I; k <J; k ++) {Update (F [I] [J], f [I] [k] + F [k + 1] [J] + data [I] * Data [k + 1] * Data [J + 1]);}} int ans =-INF; for (I = 1; I <= N; I ++) {Update (ANS, F [I] [n + I-1]); // cout <F [I] [n + I-1] <Endl;} printf ("% d \ n", ANS);} return 0 ;}