思路:
区间dp。
实现:
1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 using namespace std; 5 6 const int INF = 0x3f3f3f3f; 7 int n, a[105], dp[105][105]; 8 int dfs(int l, int r) 9 { 10 if (r == l + 1) return 0; 11 if (r == l + 2) return a[l] * a[l + 1] * a[l + 2]; 12 if (dp[l][r] != -1) return dp[l][r]; 13 int minn = INF; 14 for (int i = l + 1; i <= r - 1; i++) 15 { 16 minn = min(minn, dfs(l, i) + dfs(i, r) + a[i] * a[l] * a[r]); 17 } 18 return dp[l][r] = minn; 19 } 20 int main() 21 { 22 cin >> n; 23 for (int i = 0; i < n; i++) cin >> a[i]; 24 memset(dp, -1, sizeof dp); 25 cout << dfs(0, n - 1) << endl; 26 return 0; 27 }