Cod sursa(job #3285225)

Utilizator Dia3141Costea Diana Stefania Dia3141 Data 12 martie 2025 17:01:50
Problema Parantezare optima de matrici Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.57 kb
#include <fstream>
#define inf 1e18
using namespace std;
ifstream cin("podm.in");
ofstream cout("podm.out");
int n,v[505];
long long dp[505][505];
signed main()
{
    cin>>n;
    for(int i=1;i<=n+1;i++)
        cin>>v[i];
    for(int i=1;i<n;i++)
        dp[i][i+1]=1LL*v[i]*v[i+1]*v[i+2];
    for(int l=3;l<=n;l++)
        for(int i=1;i<=n-l+1;i++){
            int j=i+l-1;
            dp[i][j]=inf;
            for(int k=i;k<j;k++)
                dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]+1LL*v[i]*v[k+1]*v[j+1]);
        }
    cout<<dp[1][n];
    return 0;
}