#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
struct node{int d,p;
}a[10010];
int heap[10010],n;
void up(int p){
while(p>1){
if(heap[p]<heap[p/2]){
swap(heap[p],heap[p/2]);
p/=2;
}
else break;
}
}
void insert(int val){
heap[++n]=val;
up(n);
}
void down(int p){
int s=p*2;
while(s<=n){
if(s<n&&(heap[s]>heap[s+1]))s++;
if(heap[s]<heap[p]){
swap(heap[s],heap[p]);
p=s;s=2*p;
}
else break;
}
}
void cover(int val){
heap[1]=val;
down(1);
}
bool cmp(const node &x,const node &y){
return x.d<y.d;
}
int main(){
int N,ans;
while(cin>>N){
if(N==0){cout<<"0"<<endl;continue;
}
memset(a,0,sizeof(a));
memset(heap,0,sizeof(heap));
n=0;
for(int i=0;i<N;i++)
cin>>a[i].p>>a[i].d;
sort(a,a+N,cmp);
insert(a[0].p);
for(int i=1;i<N;i++){
if(a[i].d>n)insert(a[i].p);
else if(a[i].d==n&&a[i].p>heap[1])cover(a[i].p);
}
ans=0;
for(int i=1;i<=n;i++)ans+=heap[i];
printf("%d\n",ans);
}
return 0;
}