Cod sursa(job #527823)

Utilizator DraStiKDragos Oprica DraStiK Data 1 februarie 2011 12:31:31
Problema Parantezare optima de matrici Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include <algorithm>
using namespace std;

#define INF (1LL<<60)
#define DIM 505

long long bst[DIM][DIM];
int d[DIM];
int n;

void read ()
{
    int i,j;

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

    for (i=1; i<=n; ++i)
        for (j=1; j<=n; ++j)
            bst[i][j]=INF;
}

void calc (int x,int y)
{
    int i;

    if (x==y)
    {
        bst[x][x]=0;
        return ;
    }

    if (x+1==y)
    {
        bst[x][x+1]=1LL*d[x-1]*d[x]*d[x+1];
        return ;
    }

    for (i=x; i<y; ++i)
    {
        if (bst[x][i]==INF)
            calc (x,i);
        if (bst[i+1][y]==INF)
            calc (i+1,y);
        bst[x][y]=min (bst[x][y],bst[x][i]+1LL*d[x-1]*d[i]*d[y]+bst[i+1][y]);
    }
}

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

    read ();
    calc (1,n);
    printf ("%lld",bst[1][n]);

    return 0;
}