约翰有太多的工作要做。为了让农场高效运转,他必须靠他的工作赚钱,每项工作花一个单位时间。 他的工作日从0时刻开始,有10^8个单位时间。在任一时刻,他都可以选择编号1~N的N(1 <= N <= 10^6)项工作中的任意一项工作来完成。 因为他在每个单位时间里只能做一个工作,而每项工作又有一个截止日期,所以他很难有时间完成所有N个工作,虽然还是有可能。 对于第i个工作,有一个截止时间D_i(1 <= D_i <= 10^9),如果他可以完成这个工作,那么他可以获利P_i( 1<=P_i<=10^9 ). 在给定的工作利润和截止时间下,约翰能够获得的利润最大为多少.
Description

Farmer John has so very many jobs to do! In order to run the farm efficiently, he must make money on the jobs he does, each one of which takes just one time unit.

His work day starts at time 0 and has 1,000,000,000 time units (!). He currently can choose from any of N (1 ≤ N ≤ 100,000) jobs conveniently numbered 1..N for work to do. It is possible but extremely unlikely that he has time for all Njobs since he can only work on one job during any time unit and the deadlines tend to fall so that he can not perform all the tasks.

Job i has deadline Di (1 ≤ Di ≤ 1,000,000,000). If he finishes job i by then, he makes a profit of Pi (1 ≤ Pi ≤ 1,000,000,000).

What is the maximum total profit that FJ can earn from a given list of jobs and deadlines? The answer might not fit into a 32-bit integer.

Input

Multipel test cases. For each case :

* Line 1: A single integer: N

* Lines 2..N+1: Line i+1 contains two space-separated integers: Di and Pi

Output

For each case, output one line : A single number on a line by itself that is the maximum possible profit FJ can earn.

Sample Input

3
2 10
1 5
1 7

Sample Output

17

Hint

Complete job 3 (1, 7) at time 1 and complete job 1 (2, 10) at time 2 to maximize the earnings (7 + 10 → 17).

这道题的贪心的策略就是:

先把所有的价值都加进去.然后同步统计一个now统计时间.

如果当前时间已经超过了实现,那么我们就减掉价值最小的.

然后这个价值最小的用一个堆就可以维护.

AC代码:

#include<stdio.h>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
#define ll long long
#define MAX 100008
struct nod
{
ll d;
ll p;
}a[MAX];
bool cmp (nod a,nod b)
{
if(a.d!=b.d)
return a.d<b.d;
return a.p<b.p;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
priority_queue<int,vector<int>,greater<int> >q;
for(int i= ; i<n ; i++)
scanf("%d%d",&a[i].d,&a[i].p);
sort(a,a+n,cmp);
ll sum=;
ll now=;
for(int i= ; i<n ; i++)
{
now++;
sum+=a[i].p;
q.push(a[i].p);
if(now>a[i].d)
{
sum-=q.top();
q.pop();
now--;
}
}
printf("%lld\n",sum);
}
}

最新文章

  1. css布局模式
  2. sql查询最大的见多了,查询第二的呢???
  3. Android中文乱码彻底解决
  4. ubuntu_scrapy 安装
  5. ANDROID_MARS学习笔记_S02_014_GSON解析JSON串为对象
  6. mac安装office2011,提示无法打开文件Normal.dotm,因为内容有错误
  7. MVC 返回 view
  8. json的引号之伤
  9. 20 ViewPager总结
  10. 【Qt编程】基于Qt的词典开发系列&lt;十三&gt;音频播放
  11. 5.2 SW1控制LED1亮灭(中断功能)
  12. [20170703]从备份集取出spfile转化为pfile.txt
  13. lua元表学习
  14. 跨域 jsonp 和 CORS 资料
  15. Android Data Binding Library
  16. LINUX中的ACL
  17. HTML5播放器 MediaElement.js 使用方法
  18. Python :random 随机数生成
  19. [转载] 我的WafBypass之道(SQL注入篇)
  20. 预备作业03:虚拟机安装及Linux操作系统练习

热门文章

  1. jQuery-图片的放大镜显示效果(不需要大小图) ,放大镜图层显示在图片左右侧,不适用table
  2. Python01 python入门介绍
  3. 15-struct(构造函数,重载)
  4. 关于photoshop处理图片的自动化
  5. 10.model/view实例(2)
  6. 树莓派研究笔记(5)-- FM网络收音机
  7. SDUT 3379 数据结构实验之查找七:线性之哈希表
  8. SDUT 3375 数据结构实验之查找三:树的种类统计
  9. for循环 break
  10. (转)深入研究 蒋金楠(Artech)老师的 MiniMvc(迷你 MVC),看看 MVC 内部到底是如何运行的