1 #include <cstdio>
2 #include <queue>
3 #include <algorithm>
4 #define MAX_N 200006
5 using namespace std;
6 struct arr
7 {
8 int t,w;
9 }a[MAX_N];
10 int n;
11 long long s;
12 priority_queue <int> q;
13
14 bool cmp(arr x,arr y)
15 {
16 return x.t>y.t;
17 }
18
19 int main()
20 {
21 scanf("%d",&n);
22 for (int i=1;i<=n;i++)
23 {
24 scanf("%d%d",&a[i].t,&a[i].w);
25 if (a[i].t>n)
26 a[i].t=n;
27 }
28 sort(a+1,a+n+1,cmp);
29 int j=1;
30 for (int i=a[1].t;i>=1;i--)
31 {
32 while (a[j].t==i)
33 {
34 if (a[j].w>0)
35 q.push(a[j].w);
36 j++;
37 }
38 if (!q.empty())
39 {
40 s+=q.top();
41 q.pop();
42 }
43 }
44 printf("%lld",s);
45 }