Pagini recente » Cod sursa (job #3121769) | Cod sursa (job #143927) | Cod sursa (job #2263790) | Cod sursa (job #3284782) | Cod sursa (job #708930)
Cod sursa(job #708930)
Program parantezare_matrice;
const infinit=1000000000001;
var fi,fo : text;
i,n,w,j,k : longint;
best:array[0..501,0..501] of qword;
d:array[0..510] of qword;
Function min(a,b:qword):qword;
begin
if a<b then min:=a
else min:=b;
end;
begin
assign(fi,'podm.in'); reset(fi); readln(fi,n);
assign(fo,'podm.out'); rewrite(fo);
for i:=0 to n do read(fi,d[i]);
for i:=1 to n do best[i,i]:=0;
for i:=1 to n-1 do best[i,i+1]:=d[i-1]*d[i]*d[i+1];
for w:=2 to n-1 do
for i:=1 to n-w do begin
j:=i+w;
best[i,j]:=infinit;
for k:=i to j-1 do best[i,j]:=min(best[i,j],best[i,k]+best[k+1,j]+d[i-1]*d[k]*d[j]);
end;
write(fo,best[1,n]);
close(fi); close(fo);
end.