Signed-off-by: xiaozqh <xiaozqh@gmail.com>
[srcbox.git] / domino.pas
blobb5af5ca3c1fa772f6a13400fad528595a1013fdd
1 var
2 a,b,f,g:array [0..1000] of longint;
3 i,j,n:longint;
5 begin
6 readln(n);
7 for i:=1 to n do begin readln(a[i],b[i]); inc(f[1],a[i]-b[i]); g[i]:=maxlongint; end;
8 for i:=2 to n do
9 begin
10 for j:=1 to i-1 do
11 if abs(f[j]+2*(b[i]-a[i]))<f[i] then begin f[i]:=f[j]+2*(b[i]-a[i]); g[i]:=g[j]+1; end
12 else
13 if (g[j]<g[i]) then begin f[i]:=f[j]; g[i]:=g[j]; end;
14 end;
15 writeln(g[n]);
16 close(input); close(output);
17 end.