Cod sursa(job #2791171)

Utilizator adiaioanaAdia R. adiaioana Data 30 octombrie 2021 10:34:23
Problema Parantezare optima de matrici Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include <fstream>
#define LL long long
#define INF 1000000000000000001
using namespace std;
ifstream cin("podm.in");
ofstream cout("podm.out");

int N;
LL bst[550][550], d[550];
int main()
{
    cin>>N;
    for(int i=0; i<=N; ++i)
        cin>>d[i];
    for(int i=1; i<=N; ++i)
        bst[i][i]=0;
    for(int i=1; i<N; ++i)
        bst[i][i+1]=d[i-1]*d[i]*d[i+1];
    int j;
    for(int dif=2; dif<N; ++dif)
        for(int i=1; i<=N-dif; ++i)
        {
            j=i+dif;
            bst[i][j]=INF;
            for(int k=i; k<j; ++k)
                bst[i][j]=min(bst[i][j],bst[i][k]+bst[k+1][j]+d[j]*d[i-1]*d[k]*1ll);
        }
    cout<<bst[1][N]<<'\n';
    return 0;
}