题目描述
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 a_{i} ai , and the bottom contains number bi b_{i} bi , then when the player is playing the card, he gets ai a_{i} ai points and also gets the opportunity to play additional bi b_{i} 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 b_{i} 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?
输入格式:
The first line contains a single integer n n n ( 1<=n<=1000 1<=n<=1000 1<=n<=1000 ) — the number of cards Ilya has.
Each of the next n n n lines contains two non-negative space-separated integers — ai a_{i} ai and bi b_{i} bi ( 0<=ai,bi<=104 0<=a_{i},b_{i}<=10^{4} 0<=ai,bi<=104 ) — the numbers, written at the top and the bottom of the i i i -th card correspondingly.
输出格式:
Print the single number — the maximum number of points you can score in one round by the described rules.
输入样例#1:
2
1 0
2 0
输出样例#1:
2
输入样例#2:
3
1 0
2 0
0 2
输出样例#2:
3
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n;
struct la
{
int x,y;
} pp[1001];
bool cmp(la q1,la q2)
{
if(q1.y!=q2.y)
return q1.y>q2.y;
else
return q1.x>q2.x;
}
int main()
{
int i,j,num=1,ans=0;//因为第一张牌不算
cin>>n;
for(i=0; i<n; i++)
{
cin>>pp[i].x>>pp[i].y;
if(pp[i].y>0)
num+=pp[i].y-1,ans+=pp[i].x;//减去自身
}
sort(pp,pp+n,cmp);
for(i=0; i<n&# i++)
if(pp[i].y==0)
ans+=pp[i].x,num--;
cout<<ans;
return 0;
}