Cod sursa(job #1770089)

Utilizator tudorcomanTudor Coman tudorcoman Data 3 octombrie 2016 19:03:19
Problema Parantezare optima de matrici Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.76 kb

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long int64;
const int maxN = 512;
int64 d[maxN][maxN];
int64 v[maxN];

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

  int N;
  scanf("%d", &N);
  for(int i = 1; i <= N + 1; ++ i)
    scanf("%lld", &v[i]);

  for(int i = 1; i < N; ++ i)
    d[i][i + 1] = v[i] * v[i + 1] * v[i + 2];

  for(int i = 2; i < N; ++ i) {
    for(int j = 1; j + i <= N; ++ j) {
      int64 minimul = (1LL << 63) - 1;
      for(int k = j; k < j + i; ++ k)
        minimul = min(minimul, d[j][k] + d[k + 1][j + i] + v[j] * v[j + i + 1] * v[k + 1]);
      d[j][j + i] = minimul;
    }
  }

  printf("%lld\n", d[1][N]);
  return 0;
}