program book;
var n,i:integer;
f,min,max:longword;
w,v:array[0..402]of word;
procedure heap(var n:integer);
var p:word;
begin
min:=max;
for i:=1 to n-1 do
if w[i]-v[i]+w[i+1]-v[i+1]<min then
begin
min:=w[i]-v[i]+w[i+1]-v[i+1];
p:=i;
end;
w[p+1]:=w[p]+w[p+1];
v[p+1]:=v[p]+v[p+1];
f:=f+min;
n:=n-1;
for i:=p to n do
begin
w[i]:=w[i+1];
v[i]:=v[i+1];
end;
if n>2 then heap(n) else if n=2 then f:=f+w[1]-v[1]+w[2]-v[2];
end;
begin
assign(input,'book.in'); reset(input);
assign(output,'book.out'); rewrite(output);
readln(n);
max:=0; f:=0;
for i:=1 to n do begin readln(w[i],v[i]); max:=max+w[i]-v[i]; end;
heap(n);
writeln(f);
close(input); close(output);
end.