#P1539D. PriceFixed

    ID: 2074 Type: RemoteJudge 1000ms 256MiB Tried: 0 Accepted: 0 Difficulty: 5 Uploaded By: Tags>binary searchgreedyimplementationsortingstwo pointers*1600

PriceFixed

Description

Lena is the most economical girl in Moscow. So, when her dad asks her to buy some food for a trip to the country, she goes to the best store  — "PriceFixed". Here are some rules of that store:

  • The store has an infinite number of items of every product.
  • All products have the same price: 22 rubles per item.
  • For every product ii there is a discount for experienced buyers: if you buy bib_i items of products (of any type, not necessarily type ii), then for all future purchases of the ii-th product there is a 50%50\% discount (so you can buy an item of the ii-th product for 11 ruble!).

Lena needs to buy nn products: she must purchase at least aia_i items of the ii-th product. Help Lena to calculate the minimum amount of money she needs to spend if she optimally chooses the order of purchasing. Note that if she wants, she can buy more items of some product than needed.

The first line contains a single integer nn (1n1000001 \leq n \leq 100\,000) — the number of products.

Each of next nn lines contains a product description. Each description consists of two integers aia_i and bib_i (1ai10141 \leq a_i \leq 10^{14}, 1bi10141 \leq b_i \leq 10^{14}) — the required number of the ii-th product and how many products you need to buy to get the discount on the ii-th product.

The sum of all aia_i does not exceed 101410^{14}.

Output the minimum sum that Lena needs to make all purchases.

Input

The first line contains a single integer nn (1n1000001 \leq n \leq 100\,000) — the number of products.

Each of next nn lines contains a product description. Each description consists of two integers aia_i and bib_i (1ai10141 \leq a_i \leq 10^{14}, 1bi10141 \leq b_i \leq 10^{14}) — the required number of the ii-th product and how many products you need to buy to get the discount on the ii-th product.

The sum of all aia_i does not exceed 101410^{14}.

Output

Output the minimum sum that Lena needs to make all purchases.

Sample Input 1

3
3 4
1 3
1 5

Sample Output 1

8

Sample Input 2

5
2 7
2 8
1 2
2 4
1 8

Sample Output 2

12

Note

In the first example, Lena can purchase the products in the following way:

  1. one item of product 33 for 22 rubles,
  2. one item of product 11 for 22 rubles,
  3. one item of product 11 for 22 rubles,
  4. one item of product 22 for 11 ruble (she can use the discount because 33 items are already purchased),
  5. one item of product 11 for 11 ruble (she can use the discount because 44 items are already purchased).

In total, she spends 88 rubles. It can be proved that it is impossible to spend less.

In the second example Lena can purchase the products in the following way:

  1. one item of product 11 for 22 rubles,
  2. two items of product 22 for 22 rubles for each,
  3. one item of product 55 for 22 rubles,
  4. one item of product 33 for 11 ruble,
  5. two items of product 44 for 11 ruble for each,
  6. one item of product 11 for 11 ruble.

In total, she spends 1212 rubles.