Cod sursa(job #2351405)

Utilizator Monstergentleman35Ciopraga Razvan Monstergentleman35 Data 22 februarie 2019 12:57:47
Problema Parantezare optima de matrici Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.56 kb
#include <bits/stdc++.h>
#define oo 9999999

using namespace std;

ifstream fin("podm.in");
ofstream fout("podm.out");

int n,p[505];
int i,j,d,k;
int Recons[505][505];
int minc,calc;

int main()
{
 fin>>n;
 for (i=1;i<=n+1;i++)
  fin>>p[i];

 for (i=1;i<n;i++)
  Recons[i][i+1]=p[i]*p[i+1]*p[i+2];

 for (d=3;d<=n;d++)
  for (i=1,j=d;j<=n;i++,j++)
  {
   minc=oo;
   for (k=i;k<j;k++)
   {
    calc=Recons[i][k]+Recons[k+1][j]+p[i]*p[k+1]*p[j+1];
    minc=min(minc,calc);
   }
   Recons[i][j]=minc;
  }
 fout<<Recons[1][n];
 return 0;
}