Pagini recente » Cod sursa (job #2262692) | Cod sursa (job #3041248) | Cod sursa (job #643433) | Cod sursa (job #1523244) | Cod sursa (job #1093674)
#include <stdio.h>
using namespace std;
#define Nmax 505
int n;
int mat_dim[Nmax];
long long int mat_cost[Nmax][Nmax];
long long int cost;
const long long int inf = 500000000000000000;
void read()
{
freopen("podm.in", "r", stdin);
scanf("%d", &n);
for (int i = 1; i <= n + 1; ++i)
scanf("%d", &mat_dim[i]);
fclose(stdin);
}
void solve()
{
for (int l = 2; l <= n; ++l)
for (int i = 1, j; i <= n - l + 1; ++i){
j = i + l - 1;
mat_cost[i][j] = inf;
for (int k = i; k < j; ++k){
cost = mat_cost[i][k] + mat_cost[k+1][j] + mat_dim[i]*mat_dim[k+1]*mat_dim[j+1];
if (mat_cost[i][j] > cost)
mat_cost[i][j] = cost;
}
}
}
int main()
{
read();
solve();
freopen("podm.out", "w", stdout);
printf("%lld\n", mat_cost[1][n]);
fclose(stdout);
return 0;
}