<Problem>
http://ntucoder.net/Problem/Details/4394
Uses Crt,math;
Var n:longint;
i,kq,j:longint;
a,t,dp:array[-100..100000] of longint;
Procedure Sort(l,r:longint);
var i,j,x,temp:longint;
begin
i:=l; j:=r; x:=t[(l+r) div 2];
repeat
while t[i]<x do inc(i); while t[j]>x do dec(j);
if i<=j then
begin
temp:=a[j]; a[j]:=a[i]; a[i]:=temp;
temp:=t[j]; t[j]:=t[i]; t[i]:=temp;
inc(i); dec(j);
end;
until i>j;
if l<j then sort(l,j); if i<r then sort(i,r);
end;
Begin
readln(n);
for i:=1 to n do
begin
readln(a[i],t[i]);
end;
Sort(1,n);
//for i:=1 to n do writeln(a[i],' ',t[i]);
kq:=-maxlongint;
for i:=1 to n do
begin
for j:=t[i] downto 0 do
begin
dp[j]:=max(dp[j],dp[j-1]+a[i]);
kq:=max(kq,dp[j]);
end;
end;
writeln(kq);
readln;
End.