粗看一眼,,和石子归并相差无几啊。。。
那么果断AC了。。。
#include <iostream> #include <cstdio> #include <cstdlib> using namespace std; #define MAX 101 int n,p[MAX],opt[MAX][MAX]; int cut(int x) { if ( x > n ) x -= n; return x; } void init() { cin>>n; for (int i = 1; i <= n; i++) { cin>>p[i]; } } int main() { init(); for (int i = 1; i <= n; i++) { opt[i][1] = p[i]; } for (int j = 1; j <= n; j++) for (int i = 1; i <= n; i++) { int end = cut(i+j); int ans = 0; for (int k = 1; k < j; k++) { int mid = cut(i+k); int mans = opt[i][k] + opt[mid][j-k] + p[i]*p[mid]*p[end]; ans = max(ans,mans); } opt[i][j] = ans; } int ans = 0; for (int i = 1; i <= n; i++) { ans = max(ans,opt[i][n]); } cout<<ans; return 0; }