Http://soj.me/1345
Similar to matrix concatenation.
/* DP, N beads, and find that the last remaining bead can emit the maximum energy d [I] [J] indicates that the first bead starts, the maximum energy transfer equation for beads with a length of J: d [I] [J] = max (d [I] [J], d [I] [k] + d [x] [J-K] + node [I]. head * node [X]. head * node [Y]. rear), where 1 <= I <= N, 1 <= j <= N, 1 <= k <= J, x = I + k, if X> N, X = x-N; y = I + J-1, if y> N, y = y-n; max (d [1] [N], d [2] [N], d [3] [N], d [4] [N],..., d [N] [N]); * // source code of submission 829987, Zhongshan University Online judge System # include <iostream> # include <cstdio> # include <cstring> # include <algorithm> using namespace STD; struct node {int head, rear ;}; node node [105]; int d [105] [105]; int main () {int N; while (scanf ("% d", & N )! = EOF) {int temp; For (INT I = 1; I <= N; I ++) {scanf ("% d", & temp); node [I]. head = temp; if (I = 1) node [N]. rear = temp; else node [I-1]. rear = temp;} memset (D, 0, sizeof (d); int X, Y; // d [I] [J] J for (Int J = 2; j <= N; j ++) {for (INT I = 1; I <= N; I ++) {for (int K = 1; k <j; k ++) {x = I + k; If (x> N) X = x-N; y = x + (J-k)-1; if (Y> N) y = Y-N; d [I] [J] = max (d [I] [J], d [I] [k] + d [x] [J-K] + node [I]. head * node [X]. head * node [Y]. rear) ;}} int max =-1; for (INT I = 1; I <= N; I ++) {If (max <D [I] [N]) max = d [I] [N] ;}cout <max <Endl ;}}