#include<bits/stdc++.h>
using namespace std;
const int N=1e4+10;
struct P{
int p,d;
}a[N];
bool cmp(P x,P y){
return x.d<y.d;
}
int main(){
int n;
while(cin>>n){
priority_queue<int,vector<int>,greater<int> >q;
for(int i=0;i<n;i++){
scanf("%d%d",&a[i].p,&a[i].d);
}
sort(a,a+n,cmp);
for(int i=0;i<n;i++){
if(a[i].d>q.size()){
q.push(a[i].p);
}
else{
int t=q.top();
if(a[i].p>t){
q.pop();
q.push(a[i].p);
}
}
}
int ans=0;
while(q.size()){
ans+=q.top();
q.pop();
}
printf("%d\n",ans);
}
return 0;
}