地址:  https://vjudge.net/problem/27861/origin

Ilya plays a card game by the following rules.

A player has several cards. Each card contains two non-negative integers inscribed, one at the top of the card and one at the bottom. At the beginning of the round the player chooses one of his cards to play it. If the top of the card contains number ai, and the bottom contains number bi, then when the player is playing the card, he gets ai points and also gets the opportunity to play additional bi cards. After the playing the card is discarded.

More formally: let's say that there is a counter of the cards that can be played. At the beginning of the round the counter equals one. When a card is played, the counter decreases by one for the played card and increases by the number bi, which is written at the bottom of the card. Then the played card is discarded. If after that the counter is not equal to zero, the player gets the opportunity to play another card from the remaining cards. The round ends when the counter reaches zero or the player runs out of cards.

Of course, Ilya wants to get as many points as possible. Can you determine the maximum number of points he can score provided that you know his cards?

Input

The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of cards Ilya has.

Each of the next n lines contains two non-negative space-separated integers — aiand bi (0 ≤ ai, bi ≤ 104) — the numbers, written at the top and the bottom of the i-th card correspondingly.

Output

Print the single number — the maximum number of points you can score in one round by the described rules.

Examples

Input
2
1 0
2 0
Output
2
Input
3
1 0
2 0
0 2
Output
3

Note

In the first sample none of two cards brings extra moves, so you should play the one that will bring more points.

In the second sample you should first play the third card that doesn't bring any points but lets you play both remaining cards.

    以b为基准进行sort排序,再用一个计数器就好了。第三段写得明明白白,题意的理解很重要

    

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
int a,b;
}st[];
bool cmp(node a,node b)
{
if(a.b==b.b)
return a.a>b.a;
else
return a.b>b.b;
}
int main()
{
int n;
cin>>n;
for(int i=;i<n;i++)
cin>>st[i].a>>st[i].b;
sort(st,st+n,cmp);
int sum=,count=,all=n;
for(int i=;i<n;i++)
{
sum+=st[i].a;
count--;
all--;
count+=st[i].b;
if(count==)
break;
if(all==n)
break;
}
cout<<sum<<endl;
}

Cf水题B - Combination的更多相关文章

  1. 做了一道cf水题

    被一道cf水题卡了半天的时间,主要原因时自己不熟悉c++stl库的函数,本来一个可以用库解决的问题,我用c语言模拟了那个函数半天,结果还超时了. 题意大概就是,给定n个数,查询k次,每次查询过后,输出 ...

  2. 一道cf水题再加两道紫薯题的感悟

    . 遇到一个很大的数除以另一个数时,可以尝试把这个很大的数进行,素数因子分解. . 遇到多个数的乘积与另一个数的除法时,求是否能整除,可以先求每一个数与分母的最大公约数,最后若分母数字为1,则证明可整 ...

  3. 某5道CF水题

    1.PolandBall and Hypothesis 题面在这里! 大意就是让你找一个m使得n*m+1是一个合数. 首先对于1和2可以特判,是1输出3,是2输出4. 然后对于其他所有的n,我们都可以 ...

  4. 一道cf水题

    题意:输入数字n表示字符串中元素个数,字符串中只含有RGB三个字符,现在要求任意两个相同的字符他们的下标之差能整除3. 思路:任意两个相同的字符的下标能整除3,也就是任意三个为一组的字符串当中的字符不 ...

  5. 几道cf水题

    题意:给你包含n个元素的数组和k种元素,要求k种元素要用完,并且每种颜色至少用一次,n个元素,如果某几个元素的值相同,这些个元素也不能染成同一种元素. 思路:如果元素个数n小于k或者值相同的元素的个数 ...

  6. cf水题

    题意:输入多组数据,有的数据代表硬币的长宽,有的数据代表钱包的长宽,问你当这组数据代表钱包的长宽时,能不能把它前面出现的所有硬币全部装下. 思路:只要钱包的长宽大于前面出现的所有硬币的长宽就可以装下, ...

  7. 在$CF$水题の记录

    CF1158C CF1163E update after CF1173 很好,我!expert!掉rating了!! 成为pupil指日可待== 下次要记得合理安排时间== ps.一道题都没写的\(a ...

  8. 寒假第一发(CF水题两个)

    地址http://codeforces.com/contest/799 A. Carrot Cakes In some game by Playrix it takes t minutes for a ...

  9. CF 628B New Skateboard --- 水题

    CD 628B 题目大意:给定一个数字(<=3*10^5),判断其能被4整除的连续子串有多少个 解题思路:注意一个整除4的性质: 若bc能被4整除,则a1a2a3a4...anbc也一定能被4整 ...

随机推荐

  1. DFS技巧 折半搜索

    #include<iostream> #include<string> #include<cmath> #include<cstring> #inclu ...

  2. MAC设置允许任何来源

    在升级了macOS Sierra (10.12)版本后在“安全性与隐私”中不再有“任何来源”选项 接下来,我们就打开终端,然后输入以下命令: sudo spctl --master-disable 输 ...

  3. 印度第一颗CPU横空出世!阵势庞大

    我们忙着推进国产芯片的同时,隔壁的印度也没闲着.作为印度顶级高校的印度理工学院(IIT)之马德拉斯校区已经发布了其首颗处理器“Shakti”(代表女性力量的印度神话人物)的SDK软件开发包,并承诺会很 ...

  4. 通过fiddler修改通讯返回值

      1 在fiddler里选中url,右键unlock for editing 2 在fiddler里点击url, 在右面的返回值的 TextView 项里修改数据 3 取消 unlock for e ...

  5. 官网英文版学习——RabbitMQ学习笔记(九)总结

    RabbitMQ与spingboot相整合,主要步骤也很简单: 一.首先需要的是添加依赖 二.需要设置属性,属性主要是设置rabbitmq的地址端口,用户名密码回调等需要用到的一些常量,在整合过程中, ...

  6. 获取指定进程号,并kill掉

    直接上案例: 例子:获取nginx进程 方法:$ps -aux |grep nginx |grep -v grep |awk '{print $2}'  或者 $ps -ef |grep nginx ...

  7. [YOLO]《YOLOv3: An Incremental Improvement》笔记

    相比较于前两篇论文,个人感觉YOLO3作者有点来搞笑的!!!虽然加了一些新的点子进来,但是,论文的开头是这样的: 简单理解就是作者花了很多时间玩Twitter去了,所以没有做啥研究!!!! 然后: 你 ...

  8. 第九届蓝桥杯省赛c/c++真题明码题解答案,另类excel解法思路

    直到快比赛才重视起之前学校给报了蓝桥杯,且这段时间一直在做Python,所以没做什么准备. 赛场上做这道题时连反码补码的知识点都记混,所以直接用了excel做这道题目,分享下做题思路.及题解. 标题: ...

  9. 基于云开发开发 Web 应用(三):云开发相关数据调用

    介绍 在完成了 UI 界面的实现后,接下来可以开始进行和云开发相关的数据对接.完成数据对接后,应用基础就打好了,接下来的就是发布上线以及一些小的 feature 的加入. 配置 在进行相关的配置调用的 ...

  10. redis常用的命令行以及操作

    redis常用的命令行以及操作 转载酱紫人的理直气壮 最后发布于2018-07-30 17:00:41 阅读数 805  收藏 转载地址:https://blog.csdn.net/li_lening ...