1.根据过期时间进行排序
2.以小根堆为数据结构,创建集合。当该集合中的最大过期时间为期限,与集合中商品的数量进行比较,(一天卖一个)当商品数量大于最大过期时间(卖不完了),则弹出价值最小的商品。
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int,int> PII;
int main(){
/*
按照过期时间排序
集合:最大的售卖时间为期限 与商品数量进行比较
*/
int n;
while(cin>>n){
vector<PII> products(n);
for(int i=0;i<n;i++)cin>>products[i].second>>products[i].first;
sort(products.begin(),products.end());//根据过期日期从小到大进行排序
//创建小根堆
priority_queue<int,vector<int>,greater<int>>heap;
for(auto p:products){
heap.push(p.second);//将利润先放入堆
//当过期的天数小于商品数 则弹出一个利润最小的商品
if(heap.size()>p.first)heap.pop();
}
int res=0;
while(heap.size())res+=heap.top(),heap.pop();
cout<<res<<endl;
}
return 0;
}