Codeforces Round 109 (Div. 2)
B
Combination
Codeforces
1100
原题
← AC →
翻译中…

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 $*a*_{*i*}$, and the bottom contains number $*b*_{*i*}$, then when the player is playing the card, he gets $*a*_{*i*}$ points and also gets the opportunity to play additional $*b*_{*i*}$ 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 $*b*_{*i*}$, 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 — $*a*_{*i*}$ and $*b*_{*i*}$ ($0 ≤ *a*_{*i*}, *b*_{*i*} ≤ 10^{4}$) — 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.

翻译中…

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.

翻译中…

样例

Input #1
2
1 0
2 0
Output #1
2
InputCopy OutputCopy
Input #2
3
1 0
2 0
0 2
Output #2
3
InputCopy OutputCopy
题目信息
题号
155B
时间限制
2 s
内存限制
256 MB
标签
贪心
排序

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。