var
n,i,j,max:longint;
g:Array[0..40000]of boolean;
a,b,f:array[0..40000]of longint;
procedure Sort(l, r: longint);
var
i, j, x, y: integer;
begin
i := l; j := r; x := a[(l+r) DIV 2];
repeat
while a[i] < x do i := i + 1;
while x < a[j] do j := j - 1;
if i <= j then
begin
y := a[i]; a[i] := a[j]; a[j] := y;
y := b[i]; b[i] := b[j]; b[j] := y;
i := i + 1; j := j - 1;
end;
until i > j;
if l < j then Sort(l, j);
if i < r then Sort(i, r);
end;
begin
assign(input,'9cwy.in'); reset(input);
assign(output,'9cwy.out'); rewrite(output);
readln(n);
max:=-maxlongint;
for i:=1 to n do
begin
readln(a[i],b[i]);
if b[i]>max then max:=b[i];
g[a[i]]:=true;
end;
sort(1,n);
a[0]:=-1;
i:=n+1;
while (max>=0) do
begin
f[max]:=f[max+1];
if g[max] then
begin
repeat
dec(i);
if b[i]-a[i]+f[b[i]]>f[max] then
f[max]:=b[i]-a[i]+f[b[i]];
until a[i-1]<>a[i];
end;
dec(max);
end;
writeln(f[0]);
close(input);
close(output);
end.