Cod sursa(job #2791212)

Utilizator LXGALXGA a LXGA Data 30 octombrie 2021 11:13:57
Problema Parantezare optima de matrici Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.68 kb
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
ifstream cin("podm.in");
ofstream cout("podm.out");
long long v[505], dp[505][505], n;
int main()
{
    ios_base::sync_with_stdio(false);
    cin >> n;
    for (int i = 0; i <= n; i++)
    {
        cin >> v[i];
    }
    for (int l = 2; l <= n; l++)
    {
        for (int i = 1; i <= n - l + 1; i++)
        {
            int j = i + l - 1;
            dp[i][j] = (1LL << 61);
            for (int k = i; k < j; k++)
            {
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + v[i-1] * v[k] * v[j]);
            }
        }
    }
    cout << dp[1][n];
    return 0;
}