PriceFixed
Contest: Codeforces Round 727 (Div. 2)
Difficulty rating: 1600
Tags: binary search, greedy, implementation, sortings, two pointers

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: $$$2$$$ rubles per item.
- For every product $$$i$$$ there is a discount for experienced buyers: if you buy $$$b_i$$$ items of products (of any type, not necessarily type $$$i$$$), then for all future purchases of the $$$i$$$-th product there is a $$$50\%$$$ discount (so you can buy an item of the $$$i$$$-th product for $$$1$$$ ruble!).

Lena needs to buy $$$n$$$ products: she must purchase at least $$$a_i$$$ items of the $$$i$$$-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.

Input format:
The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 100\,000$$$) — the number of products.

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

The sum of all $$$a_i$$$ does not exceed $$$10^{14}$$$.

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

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

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

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

In total, she spends $$$8$$$ 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 $$$1$$$ for $$$2$$$ rubles,
2. two items of product $$$2$$$ for $$$2$$$ rubles for each,
3. one item of product $$$5$$$ for $$$2$$$ rubles,
4. one item of product $$$3$$$ for $$$1$$$ ruble,
5. two items of product $$$4$$$ for $$$1$$$ ruble for each,
6. one item of product $$$1$$$ for $$$1$$$ ruble.

In total, she spends $$$12$$$ rubles.

Example input:
3
3 4
1 3
1 5

Example output:
8

Example input:
5
2 7
2 8
1 2
2 4
1 8

Example output:
12

Editorial:
Let $$$m$$$ be the sum of all $$$a_i$$$
Important greedy observations:
1. If there is an item which costs 1, then we will not make the answer worse by buying this item.
2. If all prices are 2, then we will not make the answer worse by buying the item with max $$$b_i$$$.
Therefore we can sort all items by $$$b_i$$$ and on each iteration we will only need to consider two items: with max $$$b_i$$$ and with min $$$b_i$$$ among us all not bought items.
Another important observation: We already know how many items with price 2 we should buy to be able to buy something with a discount. This means that we can buy multiple items with full price together. Similarly, we can buy multiple items with a discount at once. This solution can be implemented using a two pointers technique which allows to find the answer in $$$O(n \cdot \log{n})$$$.