RT,1AC,11WA
#include <bits/stdc++.h>
using namespace std;
struct dat {
int d, p;
} d[100010];
bool cmp_d(dat &a, dat &b) {return a.d < b.d;}
priority_queue<dat> q;
bool operator < (const dat a, const dat b) {
return a.p > b.p;
}
int n, tim = 0;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d%d", &d[i].d, &d[i].p);
}
sort(d + 1, d + n + 1, cmp_d);
q.push(d[1]);
for (int i = 2; i <= n; i++) {
dat f = d[i];
tim += f.d - d[i - 1].d;
if (tim > 0) {
q.push(f);
continue;
}
if (q.top().p < f.p) {
q.pop();
q.push(f);
}
}
int ans = 0;
while (!q.empty()) {
ans += q.top().p;
q.pop();
}
printf("%d\n", ans);
return 0;
}