辣鸡并查集
用并查集记录最近的可行点
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
const int MAXN = 100005;
int fa[MAXN], n, ans;
struct good{
int cost, loc;
bool operator < (const good & b) const {
if(cost != b.cost) return cost > b.cost;
return loc > b.loc;
}
}a[MAXN];
int find(int x) {
if(x <= 0) return 0;
if(fa[x] != x) return fa[x] = find(fa[x]);
return fa[x];
}
int main() {
while(cin >> n) {
ans = 0;
for(int i = 1; i < MAXN; i++) fa[i] = i;
for(int i = 1; i <= n; i++) {
cin >> a[i].cost >> a[i].loc;
}
sort(a + 1, a + 1 + n);
for(int i = 1; i <= n; i++) {
if(find(a[i].loc)) {
//cout << a[i].loc << endl;
ans += a[i].cost;
fa[fa[a[i].loc]] = fa[a[i].loc] - 1;
}
}
cout << ans << endl;
}
return 0;
}