Cod sursa(job #2879152)

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

typedef long long int llint;

llint vec[505];
llint dp[505][505];

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