Cod sursa(job #3164703)

Utilizator cristiWTCristi Tanase cristiWT Data 4 noiembrie 2023 09:38:32
Problema Parantezare optima de matrici Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int nmax = 5e2 + 5, mod = 1e9 + 7, inf = 2e18;
int n, a[nmax], dp[nmax][nmax];

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
#ifndef local
    freopen("podm.in", "r", stdin);
    freopen("podm.out", "w", stdout);
#endif

    cin >> n;
    for (int i = 1; i <= n + 1; i++)
        cin >> a[i];
    for (int len = 3; len <= n + 1; len++)
        for (int l = 1; l + len - 1 <= n + 1; l++) {
            int r = l + len - 1;
            dp[l][r] = inf;
            for (int mid = l + 1; mid < r; mid++)
                dp[l][r] = min(dp[l][r], a[l] * a[r] * a[mid] + dp[l][mid] + dp[mid][r]);
        }
    cout << dp[1][n + 1];
}