Pagini recente » Cod sursa (job #2767336) | Cod sursa (job #1107627) | Istoria paginii runda/bulangandit11 | Cod sursa (job #2326633) | Cod sursa (job #535189)
Cod sursa(job #535189)
// http://infoarena.ro/problema/podm
#include <fstream>
#include <algorithm>
using namespace std;
#define maxSize 510
// cu 0x3f3f3f3f - WA pe doua teste
#define INFINITY 99999999999999999LL
ofstream out("podm.out");
int numberOfMatrices;
long long size[maxSize];
long long best[maxSize][maxSize];
void read();
int main() {
read();
for(int i=0;i<=numberOfMatrices;i++)
//best[i][i] = 0;
for(int i=1;i<=numberOfMatrices-1;i++)
best[i][i+1] = size[i-1] * size[i] * size[i+1];
for(int s=2;s<=numberOfMatrices-1;s++)
for(int i=1;i<=numberOfMatrices-s;i++) {
int j = i + s;
best[i][j] = INFINITY;
for(int k=i;k<=j-1;k++)
best[i][j] = min(best[i][j],best[i][k] + best[k+1][j] + size[i-1]*size[k]*size[j]);
}
out << best[1][numberOfMatrices] << "\n";
out.close();
return (0);
}
void read() {
ifstream in("podm.in");
in >> numberOfMatrices;
for(int i=0;i<=numberOfMatrices;i++)
in >> size[i];
in.close();
}