program cwy;
var
n,i,j,k,ans,t:longint;
a:array[1..10000,1..2] of longint;
f:array[1..10000] of longint;
procedure sort(l,r:longint);
var
i,j,mid:longint;
begin
i:=l;
j:=r;
mid:=a[random(j-i+1)+i][2];
repeat
while a[i][2]<mid do inc(i);
while a[j][2]>mid do dec(j);
if not(i>j) then
begin
t:=a[i][2];
a[i][2]:=a[j][2];
a[j][2]:=t;
t:=a[i][1];
a[i][1]:=a[j][1];
a[j][1]:=t;
inc(i);dec(j);
end;
until i>j;
if l<j then sort(l,j);
if i<r then sort(i,r);
end;
begin
assign(input,'9cwy.in');
assign(output,'9cwy.out');
reset(input);
rewrite(output);
readln(n);
for k:=1 to n do
begin
readln(i,j);
a[k][1]:=i;
a[k][2]:=j;
end;
sort(1,n);
for i:=1 to n do
begin
f[i]:=a[i][2]-a[i][1];
for j:=1 to i-1 do
begin
if (a[j][2]<=a[i][1]) then
begin
if f[j]+a[i][2]-a[i][1]>f[i] then
f[i]:=f[j]+a[i][2]-a[i][1];
end
else break;
end;
end;
for i:=1 to n do
if f[i]>ans then ans:=f[i];
writeln(ans);
close(input);
close(output);
end.