并查集
#include<iostream>
#include<algorithm>
#define x first
#define y second
using namespace std;
const int N=10010;
typedef pair<int,int> PII;
PII a[N];
int fa[N];
inline bool cmp(PII a,PII b){
return a.x>b.x;
}
inline int find(int x){
if(fa[x]!=x)
fa[x]=find(fa[x]);
return fa[x];
}
int main(){
int n;
while(~scanf("%d",&n)){
int ans=0,maxy=0;
for(int i=1;i<=n;i++){
scanf("%d%d",&a[i].x,&a[i].y);
maxy=max(maxy,a[i].y);
}
sort(a+1,a+1+n,cmp);
for(int i=0;i<=maxy;i++)
fa[i]=i;
for(int i=1;i<=n;i++){
int x=a[i].x,y=a[i].y;
int p=find(y);
if(p>0){
ans+=x;
fa[p]=p-1;
}
}
printf("%d\n",ans);
}
return 0;
}