Package for this problem was not updated by the problem writer or Codeforces administration after we’ve upgraded the judging servers. To adjust the time limit constraint, solution execution time will be multiplied by 2. For example, if your solution works for 400 ms on judging servers, then value 800 ms will be displayed and used to determine the verdict.

Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ACM-ICPC mode for virtual contests.
If you've seen these problems, a virtual contest is not for you - solve these problems in the archive.
If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive.
Never use someone else's code, read the tutorials or communicate with other person during a virtual contest.

No tag edit access

B. Combination

time limit per test

2 secondsmemory limit per test

256 megabytesinput

standard inputoutput

standard outputIlya 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.

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.

Codeforces (c) Copyright 2010-2017 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Mar/25/2017 00:58:49 (p1).

Desktop version, switch to mobile version.
User lists

Name |
---|