思路:
维护两个堆
一个按时间 (从后到前)的
另一个是按价值(从大到小)的
从时间的堆向价值的堆倒
每回(合法状态下)取当前的堆顶
判一判
//By SiriusRen
#include <queue>
#include <cstdio>
#include <algorithm>
using namespace std;
long long ans;
int n,t=1000000000;
struct Node{int d,p;}node[100500];
struct cmp{
bool operator () (Node a,Node b){
return a.p<b.p;
}
};
struct cmp2{
bool operator () (Node a,Node b){
return a.d<b.d;
}
};
priority_queue<Node,vector<Node>,cmp2>pq_time;
priority_queue<Node,vector<Node>,cmp>pq_value;
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d%d",&node[i].d,&node[i].p);
pq_time.push(node[i]);
}
pq_time.push((Node){0,0});
while(!pq_time.empty()){
Node tp=pq_time.top();pq_time.pop();
if(pq_value.empty())t=tp.d;
pq_value.push(tp);
while(!pq_value.empty()){
if(t>pq_time.top().d){
ans+=pq_value.top().p;
pq_value.pop();
t--;
}
else break;
}
}
printf("%lld
",ans);
}