双倍经验 P2949 已过,用的是 multiset 维护。
#pragma GCC optimize(3, "Ofast", "inline")
#include <iostream>
#include <cassert>
#include <queue>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <ctime>
#include <map>
#include <set>
using namespace std;
// #define int long long
#define debug puts("qwq")
#define open(x) freopen(#x".in", "r", stdin);freopen(#x".out", "w", stdout);
#define close fclose(stdin);fclose(stdout);
namespace FastIO {
template <typename T = int>
inline T read() {
T s = 0, w = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-') w = -1;
c = getchar();
}
while (isdigit(c)) s = (s << 1) + (s << 3) + (c ^ 48), c = getchar();
return s * w;
}
template <typename T>
inline void read(T &s) {
s = 0;
int w = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-') w = -1;
c = getchar();
}
while (isdigit(c)) s = (s << 1) + (s << 3) + (c ^ 48), c = getchar();
s = s * w;
}
template <typename T, typename... Arp> inline void read(T &x, Arp &...arp) {
read(x), read(arp...);
}
template <typename T>
inline void write(T x, char ch) {
if (x < 0) x = -x, putchar('-');
static char stk[25];
int top = 0;
do {
stk[top++] = x % 10 + '0', x /= 10;
} while (x);
while (top) putchar(stk[--top]);
putchar(ch);
return;
}
template <typename T>
inline void smax(T &x, T y) {
if (x < y) x = y;
}
template <typename T>
inline void smin(T &x, T y) {
if (x > y) x = y;
}
void quit() {
exit(0);
}
} using namespace FastIO;
vector<int> g[101010];
int lsh[101010];
struct node { int x, y; } a[101010];
signed main() { int n;
while (~scanf("%d", &n)) {
int ans=0;
for (int i=1;i<=n;++i) a[i].y=read(),lsh[i]=a[i].x=read();
sort(lsh+1,lsh+1+n);
int len=unique(lsh+1,lsh+1+n)-lsh-1;
for (int i=1;i<=len;++i) g[i].clear();
for (int i=1;i<=n;++i) {
g[lower_bound(lsh+1,lsh+1+len,a[i].x)-lsh].push_back(a[i].y);
}
multiset<int, greater<int> > st;
for (int i=len;i;--i) {
for (auto j:g[i]) st.insert(j);
int x=lsh[i]-lsh[i-1];
while (x-- && st.size()) {
ans += *st.begin(); st.erase(st.begin());
}
} write(ans, '\n'); }
return 0;
}