1 #include<cstdio>
2 #include<cstdlib>
3 #include<cmath>
4 #include<cstring>
5 #include<algorithm>
6 #include<iostream>
7 #include<vector>
8 #include<map>
9 #include<set>
10 #include<queue>
11 #define inf 1000000000
12 #define maxn 100000+1000
13 #define maxm 500+100
14 #define eps 1e-10
15 #define ll long long
16 using namespace std;
17 inline int read()
18 {
19 int x=0,f=1;char ch=getchar();
20 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
21 while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}
22 return x*f;
23 }
24 priority_queue<int,vector<int> >q;
25 struct rec{int x,y;}a[maxn];
26 bool cmp(rec a,rec b)
27 {
28 return a.x<b.x;
29 }
30 int main()
31 {
32 freopen("input.txt","r",stdin);
33 freopen("output.txt","w",stdout);
34 int n=read();
35 for(int i=1;i<=n;i++)
36 {
37 a[i].x=read();a[i].y=read();
38 }
39 sort(a+1,a+n+1,cmp);
40 //for(int i=1;i<=n;i++)cout<<i<<‘ ‘<<a[i].x<<‘ ‘<<a[i].y<<endl;
41 int time=inf,i=n;ll ans=0;
42 while(time)
43 {
44 while(a[i].x<time)
45 {
46 if(q.empty())time=a[i].x;
47 else
48 {
49 ans+=q.top();q.pop();time--;
50 //cout<<ans<<‘ ‘<<q.top()<<‘ ‘<<a[i].x<<‘ ‘<<i<<endl;
51 }
52 }
53 q.push(a[i].y);
54 while(a[i].x==a[i-1].x)q.push(a[--i].y);
55 i--;
56 }
57 printf("%lld\n",ans);
58 return 0;
59 }