Cod sursa(job #2371062)
| Utilizator | Data | 6 martie 2019 15:25:59 | |
|---|---|---|---|
| Problema | Parantezare optima de matrici | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.64 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream fin("podm.in");
ofstream fout("podm.out");
int n;
long long x[501];
long long D[501][501];
int main()
{
fin>>n;
for(int i=0; i<=n; ++i) fin>>x[i];
for(int i=1; i<=n; ++i) D[i][i]=0;
for(int i=1; i<n; ++i) D[i][i+1]=x[i-1]*x[i]*x[i+1];
for(int d=2; d<n; ++d)
{
for(int i=1; i+d<=n; ++i)
{
D[i][i+d]=LLONG_MAX;
for(int j=i; j<i+d; ++j)
{
D[i][i+d]=min(D[i][i+d], D[i][j]+D[j+1][i+d]+x[i-1]*x[j]*x[i+d]);
}
}
}
fout<<D[1][n]<<"\n";
return 0;
}
