program balance;
type
rec=record
id,x:longint;
end;
var
n,i,j,ans:longint;
data:array[1..50000]of rec;
f:array[0..50000]of longint;
hash:array[-50000..50000]of longint;
v:array[-50000..50000]of boolean;
procedure swap(var a,b:rec);
var
t:rec;
begin
t:=a;
a:=b;
b:=t;
end;
procedure qsort(l,r:longint);
var i,j,mid:longint;
begin
i:=l;j:=r; mid:=data[(l+r) div 2].x;
repeat
while data[i].x<mid do inc(i);
while data[j].x>mid do dec(j);
if i<=j then begin
swap(data[i],data[j]);
inc(i);dec(j);
end;
until i>j;
if l<j then qsort(l,j);
if i<r then qsort(i,r);
end;{sort}
procedure init;
begin
assign(input,'balance.in');
reset(input);
assign(output,'balance.out');
rewrite(output);
readln(n);
for i:=1 to n do
begin
readln(data[i].id,data[i].x);
if data[i].id=0 then data[i].id:=-1;
end;
qsort(1,n);
f[0]:=0;
if data[1].id=1 then f[1]:=1 else f[1]:=0;
fillchar(v,sizeof(v),0);
for i:=2 to n do
begin
f[i]:=f[i-1]+data[i].id;
if not(v[f[i]]) then
begin
v[f[i]]:=true;
hash[f[i]]:=i;
end;
end;
end;
procedure closef;
begin
close(input);
close(output);
end;
procedure main;
var
t:longint;
begin
ans:=-1;
hash[0]:=1;
for i:=1 to n do
begin
t:=hash[f[i]];
if i>=t+1 then
if data[i].x-data[t+1].x>ans then ans:=data[i].x-data[t+1].x;
end;
writeln(ans);
end;
begin
init;
main;
closef;
end.