题解2-优先队列
2024-10-27 10:28:11
发布于:上海
7阅读
0回复
0点赞
#pragma GCC O(1)
#pragma GCC O(2)
#pragma GCC O(3, "Ofast", "inline")
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 9;
struct node {
int v, s;
}a[maxn];
bool cmp(node A, node B) {
return A.s > B.s;
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i].v >> a[i].s;
sort(a + 1, a + 1 + n, cmp);
long long sum = 0, ans = 0;
priority_queue<int, vector<int>, greater<int> > q;
for (int i = 1; i <= n; i++) {
while (q.size() >= a[i].s) {
sum -= q.top();
q.pop();
}
q.push(a[i].v);
sum += a[i].v;
ans = max(ans, sum);
}
cout << ans;
return 0;
}
这里空空如也
有帮助,赞一个