CodeForces 155B

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 — ai and 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.

Ilya按照以下规则玩纸牌游戏。

玩家有几张牌。每张卡包含两个非负整数,一个在卡的顶部,一个在卡的底部。在回合开始时,玩家选择其中一张牌进行游戏。如果纸牌的顶部包含数字ai,而底部包含数字bi,那么当玩家在玩纸牌时,他将获得ai积分,并且还将有机会多玩bi张卡。玩完后,纸牌被丢弃。

更正式地说:假设有一个可以玩的纸牌柜台。在回合开始时,计数器等于1。玩纸牌时,已玩纸牌的计数器递减1,而数字bi则增加,该数字被写在纸牌的底部。然后,所打出的纸牌将被丢弃。如果在那之后计数器不等于零,则玩家有机会从剩余的纸牌中再玩一张纸牌。当计数器达到零或玩家纸牌用完时,回合结束。

当然,Ilya希望获得尽可能多的积分。如果您知道他的牌,是否可以确定他可以得分的最大分数?

输入
第一行包含一个整数n(1≤n≤1000)-Ilya拥有的纸牌数量。

接下来的n行中的每行包含两个非负空格分隔的整数ai和bi(0≤ai,bi≤104)—数字,分别写在第i张卡的顶部和底部。

输出
打印单个数字-根据所述规则,您在一轮中可以得分的最大分数。

例子
输入值
2
1 0
2 0
输出
2
输入
3
1 0
2 0
0 2
输出
3
注意
在第一个示例中,两张牌都不会带来额外的移动,因此您应该玩能带来更多积分的那张。

在第二个示例中,您应该首先玩第三张牌,该牌不带任何分数,但可以同时玩剩下的两张牌。

题目大意:
输入一个n表示有n张纸牌,然后输入n行,每行输入a和b,表示纸牌的正面和反面,即 这张牌可以获得a分,并且可以玩的牌的数量+b,假设顺序不做限制,在剩余纸牌数量>0并且剩下可玩的牌数>0时,求可以获得的最大分数是多少。

解题思路:
这道题是一道贪心的题,我们要获得最大的分数,就要尽可能拿多的牌,而可以拿的牌的数量由b决定,我们可以开一个结构体数组用于存放每张牌的a和b,然后按照b的值给数组排序,每次我们拿b最大的牌,如果b相等,我们拿a大的牌,这样就能保证在剩余牌不为0的情况下,尽可能的多拿牌了,就算最大的牌是0,我们也可以保证在拿这张0牌的时候分数尽可能的高。AC代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int _max=1e3+50;
struct node {int a,b;};//排序参数在下面
bool cmp(node x,node y){if(x.b==y.b) return x.a>y.a; return x.b>y.b;};
node p[_max];
int main()
{
	int n;
	cin>>n;
	for(int i=0;i<n;i++)
	  cin>>p[i].a>>p[i].b;
	sort(p,p+n,cmp);
	int ans=0;
	int k=0,cnt=1;
	while(cnt--)//cnt充当计数器,刚开始的计数器为1
	{
		ans+=p[k].a;
		cnt+=p[k++].b;//每次计数器要加上b的值
		if(cnt==0||k==n)//k用来记录当前拿了几张牌,全拿完则为n
		  break;
	}
	cout<<ans<<endl;
	//system("pause");
	return 0;
}
原文地址:https://www.cnblogs.com/Hayasaka/p/14294278.html