AcWing 145. 超市
原题链接
简单
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
typedef pair<int,int> PII;
int main()
{
int n;
while(cin >> n)
{
vector<PII> products(n);
priority_queue<int,vector<int>,greater<int>> small_heap; // 最小堆
for(int i = 0;i < n;i++)
{
cin >> products[i].second >> products[i].first; // 时间+利润
}
sort(products.begin(),products.end());
for(auto p : products)
{
small_heap.push(p.second);
if(p.first < small_heap.size()) small_heap.pop(); // 去除最小值
}
int res = 0;
while(!small_heap.empty())
{
res += small_heap.top();
small_heap.pop();
}
cout << res << endl;
}
return 0;
}