Cod sursa(job #3290930)

Utilizator IleaIlea Bogdan Ilea Data 2 aprilie 2025 10:20:43
Problema Parantezare optima de matrici Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.88 kb
#include <iostream>
using namespace std;

#define NMAX 505

int n, prv, l[NMAX], c[NMAX];
long long dp[NMAX][NMAX];
void solve(){
    cin>>n>>prv;
    for (int i=1; i<=n; ++i){
        l[i]=prv;
        cin>>prv;
        c[i]=prv;
    }
    for (int st=n-1; st>0; --st){
        for (int dr=st+1; dr<=n; ++dr){
            dp[st][dr]=0b111111111111111111111111111111111111111111111111111111111111111;
            for (int k=st; k<dr; ++k){
                long long inm=1LL*l[st]*c[dr]*c[k];
                dp[st][dr]=min(dp[st][dr], dp[st][k]+dp[k+1][dr]+inm);
            }
        }
    }
    cout<<dp[1][n];
}





int main(){
    #ifdef LOCAL
    #else
        ios::sync_with_stdio(false);
        cin.tie(NULL);
        cout.tie(NULL);
    #endif
    freopen("podm.in", "r", stdin);
    freopen("podm.out", "w", stdout);
    solve();
    return 0;
}