题意:有n个人,你可以花费pi让第i个人投票,或者拉够mi个人为你投票,这个人就会为你投票。
现在你想让所有人都为你投票,需要花费的最小代价是多少?
思路:可以发现,当第i个人开始为你投票时,m值小于mi的肯定都投完票了。那么在m>mi的人里,至少有mi-[小于mi的人数]的人要付钱给他,然后倒着扫一遍。
#include<bits/stdc++.h>
#define ll long long
#define MP make_pair
#define PII pair<int, int>
using namespace std;
const int N = 2e5 + 10;
int t, n, num[N];
PII a[N];
int main() {
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%d%d", &a[i].first, &a[i].second);
sort(a + 1, a + n + 1);
priority_queue<int, vector<int>, greater<int> > q;
for(int i = 1; i <= n; i++)
num[i] = a[i].first - i + 1;
ll ans = 0, cnt = 0;
for(int i = n; i > 0; i--) {
q.push(a[i].second);
if(a[i].first != a[i - 1].first) {
while(cnt < num[i]) {
ans += q.top();
q.pop();
cnt++;
}
}
}
printf("%lld\n", ans);
}
return 0;
}