思路
代码
#include <cstdio>
int n, ans, pre;
bool rec[1000006];
struct Node {
int a, b;
} arr[1000006], tmp;
void qsort(int L, int R) {
int l = L, r = R;
int mid = arr[l + r >> 1].b;
while (l < r) {
while (arr[l].b > mid) l++;
while (arr[r].b < mid) r--;
if (l <= r) {
tmp = arr[l];
arr[l] = arr[r];
arr[r] = tmp;
l++, r--;
}
}
if (L < r) qsort(L, r);
if (l < R) qsort(l, R);
}
inline int read() {
int s = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
return s * f;
}
int main() {
n = read();
for (int i = 1; i <= n; ++i) arr[i].a = read(), arr[i].b = read();
qsort(1, n);
for (int i = 1; i <= n; ++i) {
bool flag = false;
if (arr[i].a <= pre) continue;
for (int j = arr[i].a; j >= 1; --j) {
if (!rec[j]) {
rec[j] = true, ans += arr[i].b;
flag = true;
break;
}
}
if (!flag && pre < arr[i].a) pre = arr[i].a;
}
printf("%d\n", ans);
return 0;
}