Cod sursa(job #2847150)

Utilizator OvidRata Ovidiu Ovid Data 10 februarie 2022 12:46:22
Problema Parantezare optima de matrici Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include<bits/stdc++.h>
using namespace std;
#define INIT  ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount
#define int ll



ifstream fin("podm.in"); ofstream fout("podm.out");
#define cin fin
#define cout fout

int n, d[505];

int dp[505][505];

int32_t main(){
INIT
cin>>n;
for(int i=1; i<=n+1; i++){
    cin>>d[i];
}


for(int sz=1; sz<=n; sz++){
    for(int i=1; i<=(n-sz+1); i++){
        for(int j=i; j<(i+sz-1); j++){
            if(dp[i][i+sz-1]==0){
                dp[i][i+sz-1]=dp[i][j]+dp[j+1][i+sz-1]+d[i]*d[j+1]*d[ i+sz ];
            }
            else{
                dp[i][i+sz-1]=min( dp[i][i+sz-1],  dp[i][j]+dp[j+1][i+sz-1]+d[i]*d[j+1]*d[ i+sz ] );
            }
        }
    }
}

/*
for(int i=1; i<=n; i++){
    for(int j=1; j<=n; j++){
        cout<<dp[i][j]<<" ";
    }
    cout<<"\n";
}
*/


cout<<dp[1][n];

return 0;
}