#include<bits/stdc++.h> #define fi first #define se second using PII = std::pair<int, int>; constint N = 1e4 + 100; int n, fa[N], mxt, ans; PII a[N]; intget(int x){ return x == fa[x] ? x : fa[x] = get(fa[x]); } intmain() { while (std::cin >> n) { mxt = ans = 0; for (int i = 1; i <= n; ++i) scanf("%d %d", &a[i].fi, &a[i].se), mxt = std::max(mxt, a[i].se); std::sort(a + 1, a + n + 1, [&](PII x, PII y){ return x.fi > y.fi; }); for (int i = 0; i <= mxt; ++i) fa[i] = i; for (int i = 1, t; i <= n; ++i) if ((t = get(a[i].se)) > 0) { ans += a[i].fi; fa[t] = t - 1; } printf("%d\n", ans); } return0; }