Cod sursa(job #3195132)

Utilizator Alex_DumitrascuAlex Dumitrascu Alex_Dumitrascu Data 20 ianuarie 2024 10:09:40
Problema Parantezare optima de matrici Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include <iostream>
#include <fstream>
#include <climits>
#define ll long long
using namespace std;
ifstream fin ("podm.in");
ofstream fout ("podm.out");
ll d[505];
ll dp[505][505];
ll dinamica(ll n)
{
    for (int step=1; step<n; step++) {
        for (int i=1; i<=n-step; i++) {
            int j=i+step;
            ll best=LONG_MAX;
            for (int k=i; k<=j-1; k++) {
                ll value=dp[i][k]+dp[k+1][j]+d[i-1]*d[k]*d[j];
                best=min(best, value);
            }
            cout<<i<<" "<<j<<" "<<best<<endl;
            dp[i][j]=best;
        }
    }
    return dp[1][n];
}
int main()
{
    ll n;
    fin>>n;
    for (ll i=0; i<=n; i++) {
        fin>>d[i];
    }
    ll sol=dinamica(n);
    fout<<sol;
    return 0;
}