Cod sursa(job #1168178)

Utilizator mirceadinoMircea Popoveniuc mirceadino Data 7 aprilie 2014 11:16:46
Problema Parantezare optima de matrici Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.95 kb
#include<cstdio>
#include<algorithm>

using namespace std;

typedef long long int lld;
const int NMAX = 500+5;
const lld LINF = (1LL<<62);

void Read(),Solve(),Print();

int N;
int D[NMAX];
lld DP[NMAX][NMAX];

int main()
{
    Read();
    Solve();
    Print();

    return 0;
}

void Read()
{
    int i;

    freopen("podm.in","r",stdin);
    freopen("podm.out","w",stdout);

    scanf("%d",&N);

    for(i = 0; i <= N; i++)
        scanf("%d",&D[i]);
}

void Solve()
{
    int i,j,k;
    lld value;

    for(i = 1; i <= N-1; i++)
        DP[i][i+1] = 1LL * D[i-1] * D[i] * D[i+1];

    for(k = 2; k <= N; k++)
        for(i = 1; i+k <= N; i++)
        {
            value = LINF;

            for(j = i; j <= i+k-1; j++)
                value = min(value, DP[i][j] + DP[j+1][i+k] + 1LL * D[i-1] * D[j] * D[i+k]);

            DP[i][i+k] = value;
        }
}

void Print()
{
    printf("%lld\n",DP[1][N]);
}