Cod sursa(job #2409800)

Utilizator armandpredaPreda Armand armandpreda Data 19 aprilie 2019 13:41:55
Problema Parantezare optima de matrici Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.75 kb
#include <fstream>

using namespace std;

ifstream cin ("podm.in");
ofstream cout ("podm.out");

const int LIM = 505;
int n, d[LIM];
long long dp[LIM][LIM];

void print()
{
    for (int i = 1; i <= n; ++i)
    {
        for (int j = 1; j <= n; ++j)
            cout << dp[i][j] << ' ';
        cout << '\n';
    }
}

int main()
{
    cin >> n;
    for (int i = 0; i <= n; ++i)
        cin >> d[i];
    for (int i = 2; i <= n; ++i)
        for (int x = 1, y = i; y <= n; ++x, ++y)
        {
            dp[x][y] = (1ll << 63) - 1;
            for (int j = x; j < y; ++j)
                dp[x][y] = min(dp[x][y], dp[x][j] + dp[j + 1][y] + 1ll * d[x - 1] * d[j] * d[y]);
        }
    //print();
    cout << dp[1][n];
    return 0;
}