#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 1e4+5;
int dp[maxn*maxn],n;
struct why{
int d,p;
friend bool operator < (why syl,why love){
return syl.d<love.d;
}
void input(){
cin>>p>>d;
}
}a[maxn];
signed main(){
while(cin>>n){
memset(dp,0,sizeof(dp));
for(int i = 1;i<=n;i++){
a[i].input();
}
sort(a+1,a+n+1);
int ans = 0;
for(int i = 1;i<=n;i++){
for(int j = a[i].d;j>0;j--){
dp[j] = max(dp[j],dp[j-1]+a[i].p);
ans = max(ans,dp[j]);
}
}
cout<<ans<<endl;
}
return 0;
}