#include <fstream>
#include <cstdio>
#include <iostream>
#include <vector>
#include <set>

using namespace std;

struct jump
{
	int a, t;
	jump() {};
	jump(int a, int t): a(a), t(t) {};
};

struct cmp1
{
	bool operator()(const jump *fi, const jump *se) const
	{
		return (fi->t < se->t) || (fi->t == se->t && fi->a < se->a);
	}
}; 

struct cmp2
{
	bool operator()(const jump *fi, const jump *se) const
	{
		return (fi->a < se->a) || (fi->a == se->a && fi->t > se->t); 
	}
}; 

int main()
{
	ifstream cin("jumps.in");
	ofstream cout("jumps.out");
	int n;
	cin >> n;
	multiset<jump*, cmp1> day;
	multiset<jump*, cmp2> ma;
	int ok = 0;
	long long ans = 0;
	for (int i = 0; i < n; i++)
	{
		int a, t;
		cin >> a >> t;
		jump* j = new jump(a, t);
		day.insert(j), ma.insert(j);
	}
	while (ok < n)
	{
		cerr << ok << endl;
		for (multiset<jump*, cmp1>::iterator it = day.begin(); it != day.end() && (*it)->t <= ok; ma.erase(*it), day.erase(it), ok++, it = day.begin());
		if (!ma.empty())
		{
			ans += (*ma.begin())->a;
			ok++;
			day.erase(*ma.begin());
			ma.erase(ma.begin());
		}
	}
	cout << ans;                                     
}
