Cod sursa(job #578189)

Utilizator vendettaSalajan Razvan vendetta Data 11 aprilie 2011 08:53:34
Problema Parantezare optima de matrici Scor 90
Compilator fpc Status done
Runda Arhiva educationala Marime 0.91 kb
const f = 'podm.in'; g = 'podm.out';
	inf = 9223372036854775807;
	maxn = 550;
	maxn2 = 510;
type i64 = int64;
var
	bst : array[0..maxn2,0..maxn2] of i64;
	d : array[0..maxn] of i64;
	buf : array[1..1 shl 15] of char;
	buf1 : array[1..1 shl 15] of char;
	n, i, j, k, w : longint;
	
function min ( x,y : i64 ) : i64;
	begin
		if x > y then min := y
				 else min := x;
	end;
	
begin
	assign( input,f ); reset( input );
	assign( output,g ); rewrite( output );
	settextbuf( input, buf);
	settextbuf( input, buf1);
	readln( n );
	for i := 0 to n do read( d[i] );
	
	for i := 1 to n do bst[i,i] := 0;
	
	for i := 1 to n - 1 do bst[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;
			bst[i,j] := inf;
			for k := i to j - 1 do
				bst[i,j] := min ( bst[i,j], bst[i,k] + bst[k + 1,j] + d[i-1] * d[k] * d[j]);
			end;
	writeln( bst[1,n] ) ;
end.