Cod sursa(job #2879177)

Utilizator cezar_titianuTitianu Cezar cezar_titianu Data 28 martie 2022 12:01:11
Problema Parantezare optima de matrici Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.63 kb
#include <fstream>

typedef long long int ullint;

ullint vec[505];
ullint dp[505][505];

int main() {
	std::ifstream fin("podm.in");
	std::ofstream fout("podm.out");
	int nrn;
	fin >> nrn;
	for (int index = 0; index <= nrn; index++) {
		fin >> vec[index];
		dp[index][index] = 0;
	}
	for (int pos1 = nrn - 2; pos1 >= 0; pos1--) {
		for (int pos2 = pos1 + 1; pos2 < nrn; pos2++) {
			dp[pos1][pos2] = 0x7fffffffffffffff;
			for (int posm = pos1; posm < pos2; posm++) {
				dp[pos1][pos2] = std::min(dp[pos1][pos2], dp[pos1][posm] + dp[posm + 1][pos2] + vec[pos1] * vec[posm + 1] * vec[pos2 + 1]);
			}
		}
	}
	fout << dp[0][nrn - 1];
	return 0;
}