program MatrixChainProduct; uses crt; var infile,outfile:text; x,y,i,j,k,L:integer; n:integer; m:array[1..12,1..12] of longint; p:array[1..12] of integer; q:longint; begin assign(infile,'DATA41.txt'); reset(infile); assign(outfile,'OUT41.txt'); rewrite(outfile); for x:=1 to 5 do begin y:=0; read(infile,p[y]); while p[y]<>0 do begin inc(y); read(infile,p[y]); end; readln(infile); n:=y-1; for i:=1 to n do m[i,i]:=0; for L:=2 to n do begin for i:=1 to n-L+1 do begin j:=i+L-1; m[i,j]:=2000000000; for k:=i to j-1 do begin q:=m[i,k]+m[k+1,j]+p[i-1]*p[k]*p[j]; if qm[i,j] then m[i,j]:=q; end; end; end; writeln(outfile,m[i,n]); end; close(infile); close(outfile); end.