program balance(input,output);
var
a,sum,b:array[0..50000] of longint;
hash:array[-50000..50000] of longint;
i,j,k,m,n,ans:longint;
procedure qsort(r,l:longint);
var i,j,x,y:longint;
begin
i:=r; j:=l; x:=b[(i+j) div 2];
repeat
while b[i]<x do inc(i);
while b[j]>x do dec(j);
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;
inc(i); dec(j);
end;
until i>j;
if i<l then qsort(i,l);
if j>r then qsort(r,j);
end;
begin
assign(input,'balance.in');assign(output,'balance.out');
reset(input); rewrite(output);
readln(n);
for i:=-n to n do hash[i]:=0;
for i:=1 to n do readln(a[i],b[i]);
qsort(1,n);
sum[0]:=0;
for i:=1 to n do
if a[i]=0 then sum[i]:=sum[i-1]-1 else sum[i]:=sum[i-1]+1;
hash[0]:=0;
for i:=1 to n do
if (hash[sum[i]]=0)and(sum[i]<>0) then hash[sum[i]]:=i;
ans:=0;
for i:=1 to n do
if (b[i]-b[hash[sum[i]]+1]>ans)and(i>hash[sum[i]]) then
ans:=b[i]-b[hash[sum[i]]+1];
writeln(ans);
close(input);close(output);
end.