#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char buf[10000000], *ptr = buf - 1;
inline int readint(){
int n = 0;
char ch = *++ptr;
while(ch < ‘0‘ || ch > ‘9‘) ch = *++ptr;
while(ch <= ‘9‘ && ch >= ‘0‘){
n = (n << 1) + (n << 3) + ch - ‘0‘;
ch = *++ptr;
}
return n;
}
const int maxn = 100000 + 10;
typedef long long ll;
struct Node{
int d, p;
Node(){}
Node(int _d, int _p): d(_d), p(_p){}
bool operator < (const Node &x) const{
return d < x.d;
}
}a[maxn], t;
class cmp{
public:
bool operator () (const Node &a, const Node &b){
return a.p > b.p;
}
};
priority_queue<Node, vector<Node>, cmp> q;
int main(){
fread(buf, sizeof(char), sizeof(buf), stdin);
int n = readint();
for(int i = 1; i <= n; i++){
a[i].d = readint();
a[i].p = readint();
}
sort(a + 1, a + n + 1);
int tot = 0;
long long ans = 0;
for(int i = 1; i <= n; i++){
if(tot == a[i].d){
t = q.top();
if(a[i].p > t.p){
q.pop();
q.push(a[i]);
ans += a[i].p - t.p;
}
}
else{
tot++;
ans += a[i].p;
q.push(a[i]);
}
}
printf("%lld\n", ans);
return 0;
}