tokitsukaze and Soldier
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
typedef pair<long, long> PII;
#define x first
#define y second
int n;
vector<PII> a;
priority_queue<PII, vector<PII>, greater<PII>> pq;
bool cmp(PII a, PII b){
return a.y > b.y;
}
signed main(){
cin >> n;
a.resize(n);
for(int i = 0; i < n; ++i){
cin >> a[i].x >> a[i].y;
}
sort(a.begin(), a.end(), cmp);
long long sum = 0, res = 0;
for(int i = 0; i < n; ++i){
pq.push(a[i]);
sum += a[i].x;
while(pq.size() > a[i].y){
auto t = pq.top(); pq.pop();
sum -= t.x;
}
res = max(res, sum);
}
cout << res << endl;
return 0;
}