Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int nMax = 505;
- typedef long long ll;
- ll best[nMax][nMax], dim[nMax];
- int main()
- {
- int n;
- cin >> n;
- for(int i=0; i<=n; i++)
- cin >> dim[i];
- for(int i=1; i<n; i++)
- best[i][i+1] = dim[i-1] * dim[i] * dim[i+1];
- for(int d=2; d<=n-1; d++)
- {
- for(int i=1; i<=n-d; i++)
- {
- best[i][i + d] = LONG_LONG_MAX;
- for(int k=i; k< i+ d; k++)
- {
- best[i][i + d] = min(best[i][i + d], best[i][k] + best[k +1][i + d] + dim[i-1] * dim[k] * dim[i + d]);
- }
- }
- }
- cout << best[1][n];
- }
Advertisement
Add Comment
Please, Sign In to add comment