#include <bits/stdc++.h>
#define fst first
#define snd second
#define int64 long long
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define FORD(i, b, a) for(int i = b; i >= a; i--)
using namespace std;
typedef pair<int,int> ii;
template<class X, class Y> bool maximize(X &a, Y b)
{
if(a >= b) return false;
a = b;
return true;
}
template<class X, class Y> bool minimize(X &a, Y b)
{
if(a <= b) return false;
a = b;
return true;
}
const int N = 2e5 + 55;
int a[N], b[N];
vector<int> company[N];
int main ()
{
int test = 1;
while(test--)
{
int n;
cin >> n;
FOR(i, 1, n)
{
cin >> a[i] >> b[i];
company[a[i]].push_back(b[i]);
}
multiset<int> value;
int64 res = 0;
int currentSize = n, add = 0;
FORD(i, n, 0)
{
for(auto v: company[i])
{
value.insert(v);
}
currentSize -= (int)company[i].size();
while(currentSize + add < i)
{
res += *value.begin();
value.erase(value.begin());
add++;
}
}
cout << res << "\n";
}
}