Cod sursa(job #1817779)

Utilizator msciSergiu Marin msci Data 28 noiembrie 2016 14:20:50
Problema Parantezare optima de matrici Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.63 kb
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <list>
#include <queue>
#include <stack>
#include <deque>
#include <algorithm>
#include <bitset>
#include <complex>
#include <functional>
#include <string>
#include <limits>
#include <cmath>
#include <cassert>
#include <cstdio>
#include <cstdlib>
using namespace std;
template<typename T> void _dbg(const char *sdbg, T h) { cerr << sdbg << " = " << h << "\n"; }
template<typename T, typename... U> void _dbg(const char *sdbg, T a, U... b) {
	while (*sdbg != ',') cerr << *sdbg++; cerr << " = " << a << ", "; _dbg(sdbg + 1, b...); }
#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr if(0)cout
#endif
static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3f;
template<typename T, typename U> static void amin(T &x, U y) { if (y < x) x = y; }
template<typename T, typename U> static void amax(T &x, U y) { if (y > x) x = y; }

const int NMAX = 555;
int n;
int d[2 * NMAX], dp[NMAX][NMAX];

int main() { ios_base::sync_with_stdio(0); cin.tie(0);
	ifstream fin("podm.in");
	ofstream fout("podm.out");
	fin >> n;
	for (int i = 0; i <= n; i++) fin >> d[i];
	for (int i = 0; i <= n; i++)
		for (int j = 0; j <= n; j++)
			dp[i][j] = numeric_limits<int>::max();
	for (int i = 0; i <= n; i++) dp[i][i] = 0;
	for (int i = n - 1; i >= 1; i--) {
		for (int j = i + 1; j <= n; j++) {
			for (int k = i; k < j; k++) {
				amin(dp[i][j], dp[i][k] + dp[k + 1][j] + d[i - 1] * d[k] * d[j]);
			}
		}
	}
	fout << dp[1][n] << endl;
}