Pagini recente » Cod sursa (job #378714) | Cod sursa (job #364702) | Cod sursa (job #2676752) | Cod sursa (job #2914935) | Cod sursa (job #1049073)
/*#include <iostream>
#include <fstream>
using namespace std;
const char iname[] = "podm.in";
const char oname[] = "podm.out";
#define MAXN 505
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define FOR(i, a, b) for (int i = (a); i <= (b); ++ i)
#define INF 100000000000000000LL
typedef long long i64;
i64 bst[MAXN][MAXN], d[MAXN]; int n;
int main(void)
{
ifstream in(iname);
in >> n;
FOR (i, 0, n) in >> d[i];
in.close();
ofstream out(oname);
FOR (i, 1, n) bst[i][i] = 0;
FOR (i, 1, n - 1) bst[i][i + 1] = d[i - 1] * d[i] * d[i + 1];
FOR (w, 2, n - 1) FOR (i, 1, n - w) {
int j = i + w;
bst[i][j] = INF;
FOR (k, i, j - 1)
bst[i][j] = Min(bst[i][j], bst[i][k] + bst[k + 1][j] + d[i - 1] * d[k] * d[j]);
}
out << bst[1][n]; */
#include <fstream>
#include <iostream>
using namespace std;
ifstream f("podm.in");
ofstream g("podm.out");
int n;
long long d[505], cost[505][505];
int main()
{
f >> n;
for(int i = 0 ; i <= n; i ++)
f >> d[i];
f.close();
long long aux = 0;
for(int i = 1; i <= n; i++) cost[i][i] = 0;
//for(int i = 1; i <= n-1; i++) cost[i][i + 1] = d[i - 1] * d[i] * d[i + 1];
for(int j = 2; j <= n; j++){
for(int i = j-1; i >=1; i--){
long long m = 0xfffffffffffff;
for(int k = i; k < j; k++){
aux = cost[i][k] + cost[k+1][j]+d[i-1]*d[k]*d[j];
if(aux < m) m = aux;
}
cost[i][j] = m;
}
}
g << cost[1][n] <<"\n";
return 0;
}