For the Tet holiday, Jack, who is known as Sol's father, is preparing ~n~ lucky money envelopes for Sol - Jack's son. In the ~i~-th envelope, there is an amount of money ~a_i~ and a number ~b_i~ (where ~b_i ≥ 0~). If ~b_i > 0~, Jack's son is allowed to choose ~b_i~ additional envelopes. This additional selection is cumulative. First, Sol selects any envelope. Suppose Jack's son currently has a total amount of money ~A~ and is allowed to choose ~B~ more envelopes (where ~B > 0~). If Sol selects the ~i~-th additional envelope, the total amount of money becomes ~A + a_i~ and the number of additional envelopes that can be chosen becomes ~B - 1 + b_i~. This process continues until Jack's son can no longer choose additional envelopes (~B = 0~) or all envelopes have been selected.
Task: Help Sol determine the maximum amount of money he can get by choosing the envelopes in the best possible order.
Input:
- The first line contains the integer ~n~ ~(1 ≤ n ≤ 10^5)~.
- Each of the following ~n~ lines contains two integers ~a_i~ and ~b_i~ separated by a space (~1 ≤ a_i,b_i ≤ 100~).
Output:
- Output a single integer representing the maximum amount of money Sol can get.
Sample Input
6
2 2
3 1
5 1
3 0
4 0
6 0
Sample Output
20
Bình luận
hello Tang