#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
using namespace std;
int main()
{
int numProduct, numInput, time, total;
int price, deadline;
priority_queue<int, vector<int>, greater<int> > maxProfit;
map<int, vector<int> > product;
while(cin >> numProduct){
numInput = numProduct;
total = 0; time = 0;
product.clear();
while(!maxProfit.empty()) maxProfit.pop();
while(numProduct--){
cin >> price >> deadline;
product[deadline].push_back(price);
}
for(map<int, vector<int> >::iterator it = product.begin(); it != product.end(); it++){
for(int i=0; i<product[it->first].size(); i++){
if(it->first > time){
maxProfit.push(product[it->first][i]);
time++;
} else if(it->first == time && product[it->first][i] > maxProfit.top()){
maxProfit.pop();
maxProfit.push(product[it->first][i]);
}
}
}
while(!maxProfit.empty()){
total += maxProfit.top();
maxProfit.pop();
}
cout << total << endl;
}
}
std::ios::sync_with_stdio(0);
std::cin.tie(0);