Description
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.
Sample Input
2 1 0 2 0
2
3 1 0 2 0 0 2
3
Hint
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.
题意:一个玩家有几张卡片,第一列是正面表示卡边价值的分数,第二列是背面表示还能打了这张牌还能打几张出去,第一次随便出哪张牌,问最多能得多少分。
思路:因为正面分数不为负数所以按背面数值大小排序然后排到背面数值为0的时候按正面数值大小排序即可。
#include <iostream>
#include <algorithm>
using namespace std;
struct card{
int top;
int buttom;
};
bool cmp(const card &c1,const card &c2){
if(c1.buttom!=c2.buttom)
return c1.buttom>c2.buttom;
else
return c1.top>c2.top;
}
int main()
{
card c[1010];
int n,number = 1,ans = 0;
cin>>n;
for(int i=0;i<n;i++){
cin>>c[i].top>>c[i].buttom;
if(c[i].buttom > 0){
number += c[i].buttom-1;
ans += c[i].top;
}
}
sort(c,c+n,cmp);
for(int i=0;number > 0&&i<n; i++){
if(c[i].buttom == 0){
ans +=c[i].top;
number --;
}
}
cout<<ans<<endl;
return 0;
}