题意:
n
≤
2
e
5
,
m
≤
n
,
p
≤
1
e
9
n\le2e5,m\le n,p\le 1e9
n≤2e5,m≤n,p≤1e9
思路:
首先需要发现一些性质,假设
p
r
e
i
pre_i
prei代表所有
m
j
<
i
m_j< i
mj<i的
p
j
p_j
pj和。可以发现,如果我们处理到了人数达到
i
i
i才能主动投票的某些人的时候,所有
<
i
<i
<i的人无论是被收买来的,还是没有花费任何代价来的,都不需要关心。我们只需要关心所有
m
j
≥
i
m_j\ge i
mj≥i的人。如果
p
r
e
i
pre_i
prei的人数达不到
i
i
i,那么我们只能从所有
m
j
≥
i
m_j\ge i
mj≥i的人中选出前
i
−
p
r
e
i
i-pre_i
i−prei小的人收买他们,才能达到
i
i
i的要求,之后才能进行
i
+
1
i+1
i+1。
当然上面的过程是正着想的,具体的代码可以对
m
i
m_i
mi开一个桶,倒着来,每次将
m
j
=
i
m_j=i
mj=i的
p
j
p_j
pj都加入优先队列中,拿出来即可。
复杂度
O
(
n
l
o
g
n
)
O(nlogn)
O(nlogn)
// Problem: E1. Voting (Easy Version)
// Contest: Codeforces - Educational Codeforces Round 75 (Rated for Div. 2)
// URL: https://codeforces.com/problemset/problem/1251/E1
// Memory Limit: 512 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;
//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;
int n;
int p[N];
vector<int>v[N];
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);
int _; scanf("%d",&_);
while(_--) {
scanf("%d",&n);
for(int i=0;i<=n;i++) v[i].clear();
for(int i=1;i<=n;i++) {
int x; scanf("%d%d",&x,&p[i]);
v[x].pb(p[i]);
}
int cnt=0,pre=n;
LL ans=0;
priority_queue<int,vector<int>,greater<int>>q;
for(int i=n;i>=1;i--) {
pre-=v[i].size();
int need=i-pre;
for(auto x:v[i]) q.push(x);
while(cnt<need) {
ans+=q.top(); q.pop();
cnt++;
}
}
printf("%lld\n",ans);
}
return 0;
}
/*
*/