program bing;
type
sb=record
a:integer;
b:longint;
end;
var
f1,f2:text;
n:longint;
s:array[1..50000] of sb;
hash:array[-50000..50000] of longint;
procedure init;
var
i:longint;
begin
assign(f1,'balance.in');reset(f1);
assign(f2,'balance.out');rewrite(f2);
readln(f1,n);
for i:=1 to n do
readln(f1,s[i].a,s[i].b);
end;
procedure kp(l,r:longint);
var
i,j:longint;
t:sb;
x:longint;
begin
i:=l;j:=r;
x:=s[(i+j)shr 1].b;
repeat
while s[i].b<x do inc(i);
while s[j].b>x do dec(j);
if i<=j then
begin
t:=s[i];s[i]:=s[j];s[j]:=t;
inc(i);dec(j);
end;
until i>j;
if l<j then kp(l,j);
if i<r then kp(i,r);
end;
procedure wokao;
var
i,max:longint;
sum:array[0..50000] of longint;
begin
max:=0;
fillchar(hash,sizeof(hash),0);
sum[0]:=0;
for i:=1 to n do
begin
if s[i].a=0 then sum[i]:=sum[i-1]-1 else sum[i]:=sum[i-1]+1;
if hash[sum[i]]=0 then hash[sum[i]]:=i;
end;
for i:=1 to n do
begin
if (i>1+hash[sum[i]])and(s[i].b-s[1+hash[sum[i]]].b>max) then max:=s[i].b-s[1+hash[sum[i]]].b;
end;
writeln(f2,max);
close(f1);close(f2);
end;
begin
init;
kp(1,n);
wokao;
end.