POJ 1651 DP 矩阵链乘
// DP
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<vector>
#define MAXN 105
#define MAXM 305
#define MOD 10000
#define inf 100000000
#define eps 1e-9
#define pi acos(-1.0)
typedef long long LL;
using namespace std;
int n, p[MAXN], dp[MAXN][MAXN];
int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
while (scanf("%d", &n) != EOF) {
for (int i = 1; i <= n; i++)
scanf("%d", &p[i]);
memset(dp, 0, sizeof(dp));
for (int i = 2; i <= n - 1; i++) {
for (int j = 1; j <= n - i + 1; j++) {
dp[j][j + i] = dp[j + 1][j + i] + p[j] * p[j + 1] * p[j + i];
for (int k = j + 1; k < j + i; k++)
dp[j][j + i] = min(dp[j][j + i], dp[j][k] + dp[k][j + i] + p[j] * p[k] * p[j + i]);
}
}
printf("%d\n", dp[1][n]);
}
return 0;
}