题目
每次尽可能选大的,不知道怎么证正确性,但感觉是对的
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=10002;
int n,i,pos,fa[N],ans;
struct node{
int x,y;
}a[N];
int find(int x){
return x==fa[x]?x:find(fa[x]);
}
bool cmp(node a,node b){
return a.x>b.x;
}
int main(){
while (~scanf("%d",&n)){
for (i=1;i<=n;i++) scanf("%d%d",&a[i].x,&a[i].y);
for (i=1;i<N;i++) fa[i]=i;
sort(a+1,a+n+1,cmp);
for (ans=0,i=1;i<=n;i++){
pos=find(a[i].y);
if (pos) ans+=a[i].x,fa[pos]=pos-1;
}
printf("%d\n",ans);
}
}