Pagini recente » Cod sursa (job #284186) | Cod sursa (job #1730784) | Cod sursa (job #1924814) | Cod sursa (job #1595746) | Cod sursa (job #742255)
Cod sursa(job #742255)
#include<stdio.h>
#include<assert.h>
#include<algorithm>
#include<vector>
using namespace std;
struct mat{
long long h, w;
mat(){};
};
const int knmax = 505;
long long matrixes, sol, dp[knmax][knmax];
mat given[knmax];
void read(){
assert(freopen("podm.in", "r", stdin));
scanf("%lld", &matrixes);
for(int i = 1; i <= matrixes; ++i)
scanf("%lld", &given[i].h);
scanf("%lld", &given[matrixes].w);
for(int i = 1; i < matrixes; ++i)
given[i].w = given[i + 1].h;
}
long long compute(int x, int y){
if(x == y)
return 0;
if(dp[x][y])
return dp[x][y];
dp[x][y] = given[x].h * given[x].w * given[y].w + compute(x + 1, y);
for(int i = x + 1; i <= y; ++i)
dp[x][y] = min(dp[x][y], compute(x, i - 1) + compute(i, y) + given[x].h * given[i].h * given[y].w);
return dp[x][y];
}
void solve(){
sol = compute(1, matrixes);
}
void write(){
assert(freopen("podm.out", "w", stdout));
printf("%lld", sol);
}
int main(){
read();
solve();
write();
return 0;
}