地址:  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. 吴裕雄--天生自然java开发常用类库学习笔记:线程常用的操作方法

    class MyThread implements Runnable{ // 实现Runnable接口 public void run(){ // 覆写run()方法 for(int i=0;i< ...

  2. 关于cvPyrSegmentation(src, dst, storage, &comp, level, threshold1, threshold2)函数报错的问题解答

    先挂上我写的代码: #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <highgui.h> #incl ...

  3. 微信小程序实现左滑删除效果(原生/uni-app)

    实现效果 列表中侧滑删除 删除不同时存在 scrollview上下滑动与侧滑删除不影响 uni-app实现 html部分 <template> <scroll-view :scrol ...

  4. golang开启随机端口并获取端口

    listener, err := net.Listen("tcp", ":0") if err != nil { panic(err) } fmt.Printl ...

  5. django 中从外界借助多个网站时 static 的存放和整理

    在 模板之家中  前端页面直接上去抓取  可是遇到重复  或者 版本不统一  所以 在每个app下面建立自己的 stastic 在制作的html  页面上方 导入静态页面 {% load static ...

  6. spring-@ResponseBody返回时的编码处理

    下面是一个解决方案 @RequestMapping(value = "/queryall", method = GET, produces = "application/ ...

  7. MyBatis parameterType、resultType的数据类型

    parameterType.resultType的数据类型要写全限定类名或者别名. mybatis已经给常用的数据类型起好了别名,参考mybatis.pdf  3.1.1小节: 基本数据类型很少用,一 ...

  8. 九十五、SAP中查看自定义包的所有模块,对象,函数主,事务等

    一.输入SE80 二.选择包,再查下Z* 三.可以看到,查下出来的包 四.可以看到我们想要的内容了

  9. 097-PHP循环使用next取数组元素二

    <?php function return_item($arr, $num = 0) { //定义函数 if ($num < 0) { end($arr); //将数组指针指向最后一个元素 ...

  10. 留学英文论文写作Abstract三种类型

    所谓Abstract,就是对所写论文主要内容的精炼概括.Abstract是美国人的说法,英国的科技期刊喜欢称之为Summary.在英文中,有资料是这么对其定义的:Abstract is a sketc ...