小根堆
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
#define op < //小根堆
using namespace std;
typedef long long ll;
const int N=1e5+5;
struct Binary_heap{
int heap[N],sz;
void up(int p){
while(p>1&&heap[p] op heap[p>>1])swap(heap[p],heap[p>>1]),p>>=1;
}
void push(int x){
heap[++sz]=x;
up(sz);
}
void down(int p){
int ls=p<<1;
while(ls<=sz){
if(ls<sz&&heap[ls|1] op heap[ls])ls|=1;
if(heap[ls] op heap[p])swap(heap[ls],heap[p]),p=ls,ls=p<<1;
else break;
}
}
void pop(){
heap[1]=heap[sz--];
down(1);
}
void remove(int p){
heap[p]=heap[sz--];
up(p),down(p);
}
int top(){
return heap[1];
}
}P;
int n;
int main(){
while(~scanf("%d",&n)){
P.sz=0;
vector<pair<int,int> >q(n);
for(int i=0;i<n;++i)scanf("%d%d",&q[i].second,&q[i].first);
sort(q.begin(),q.end());
for(int i=0;i<n;++i){
if(q[i].first==P.sz&&q[i].second>P.top())P.pop(),P.push(q[i].second);
else if(q[i].first>P.sz)P.push(q[i].second);
}
int ans=0;
while(P.sz)ans+=P.heap[P.sz--];
printf("%d\n",ans);
}
return 0;
}