Cod sursa(job #3218240)

Utilizator profinfo114Prof Info profinfo114 Data 26 martie 2024 16:52:54
Problema Parantezare optima de matrici Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 kb
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 1e18;
const int NMAX = 505;
int n,d[NMAX],dp[NMAX][NMAX];

ifstream fin("podm.in");
ofstream fout("podm.out");

auto minSelf(auto &a, auto b){
    a = min(a, b);
}

signed main()
{
    fin >> n;
    n++;
    for(int i = 1; i <= n; i++){
        fin >> d[i];
        dp[i][i+1] = 0;
    }
    for(int len = 3; len <= n; len++){
        for(int l = 1; l <= n-len+1; l++){
            int r = l+len-1;
            dp[l][r] = INF;
            for(int k = l+1; k < r; k++){
                minSelf(dp[l][r], dp[l][k]+dp[k][r]+d[l]*d[k]*d[r]);
            }
        }
    }
    fout << dp[1][n];
    return 0;
}