#include<bits/stdc++.h>
using namespace std;

#define fast                    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define int                     long long 
#define fr1(i,a,b)              for(int i=a;i<b;i++)
#define r(n)                    int n; cin>>n;
#define r2(a,b)                 int a,b; cin>>a>>b;
#define r3(a,b,c)               int a,b,c; cin>>a>>b>>c; 
#define rs(s)                   string s; cin>>s;
#define rc(ch)                  char ch; cin>>ch;
#define cy                      cout<<"YES"
#define cn                      cout<<"NO"
#define nl                      cout<<"\n"
#define endl                    "\n"
#define pb                      push_back
#define fr                      first
#define sc                      second
#define ia(a,n)                 for(int i=0;i<n;i++)cin >> a[i];
#define oa(a,n)                 for(int i=0;i<n;i++) cout<<a[i]<<" "; 
#define iv(a)                   for(auto &x : a)cin >> x;
#define sa(a,n)                 sort(a,a+n)
#define pv(v)                   for(auto x: v) cout<<x<<" ";   
#define sz(x)                   ((long long)x.size())
#define vi                      vector<int>
#define uci                     unordered_map<char, int>
#define uic                     unordered_map<int, char>
#define uii                     unordered_map<int, int>
#define mci                     map<char, int>
#define mic                     map<int, char>
#define mii                     map<int, int>
#define pii                     pair<int, int>
#define pci                     pair<char, int>
#define pic                     pair<int, char>
#define vpii                    vector<pii>
#define mod                     1000000007
#define max3(a,b,c)             max(a, max(b, c))
#define max4(a,b,c,d)           max(a, max(b, max(c, d)))
#define min3(a,b,c)             min(a, min(b, c))
#define sumOfArray(v)           accumulate(v.begin(), v.end(), 0); 
#define CountInArray(v,x)       count(v.begin(), v.end(), x)
#define maxElemOfArray(v)       max_element(v.begin(), v.end())
#define mst(arr,x)              memset(arr, x, sizeof(arr));
#define dbg(x)                  cout<<#x<<"="<<(x)<<endl;
#define all(v)                  (v).begin(), (v).end()
#define dbg2(x,y)               cout<<#x<<"="<<(x)<<" "<<#y<<"="<<(y)<<endl;
#define dbg3(x,y,z)             cout<<#x<<"="<<(x)<<" "<<#y<<"="<<(y)<<" "<<#z<<"="<<(z)<<endl;
#define dbg4(x,y,z,w)           cout<<#x<<"="<<(x)<<" "<<#y<<"="<<(y)<<" "<<#z<<"="<<(z)<<" "<<#w<<"="<<(w)<<endl;
#define intmax                  INT_MAX
#define intmin                  INT_MIN
bool cmp(int a,int b){
    return a&1 < b&1;
}   
int a[4] = {1, 0, -1, 0};
int b[4] = {0, 1, 0, -1};

void solve(){
    r(n);
    vector<pii> arr(n);
    fr1(i,0,n){
        cin >> arr[i].sc >> arr[i].fr;
    }
    sort(all(arr));
    int cnt = 0, money = 0;
    int s = 0, e = n - 1;
    //fr = discount after buy  sc = atleast
    while(s <= e){
        if(cnt >= arr[s].fr){
            cnt += arr[s].sc;
            money += arr[s].sc;
            s++;
        }
        else{
            int mini = min(arr[s].fr - cnt, arr[e].sc);
            cnt += mini;
            money += 2 * mini;
            if(mini == arr[e].sc){
                e--;
            }
            else{
                arr[e].sc -= mini;
            }
        }
    }
    cout << money;
}
signed main(){
    fast; 
    cout << fixed << setprecision(25);
    cerr << fixed << setprecision(10);
    auto start = std::chrono::high_resolution_clock::now();
    
    solve();
    // int t=1;cin>>t;while(t--){solve();nl;}
    
    auto stop = std::chrono::high_resolution_clock::now(); 
    auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start);
    
}
// https://atcoder.jp/contests/abc106/tasks/abc106_c
    // https://atcoder.jp/contests/abc105/tasks/abc105_c
    // https://atcoder.jp/contests/abc104/tasks/abc104_c
    // https://atcoder.jp/contests/abc102/tasks/arc100_a
    // https://atcoder.jp/contests/abc098/tasks/arc098_a
### Prompt

Please create a solution in cpp to the following problem:

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

The first line contains a single integer n (1 ≤ n ≤ 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 ≤ a_i ≤ 10^{14}, 1 ≤ b_i ≤ 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

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

Examples

Input


3
3 4
1 3
1 5


Output


8


Input


5
2 7
2 8
1 2
2 4
1 8


Output


12

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.

### Response

```cpp
#include<bits/stdc++.h>
using namespace std;

#define fast                    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define int                     long long 
#define fr1(i,a,b)              for(int i=a;i<b;i++)
#define r(n)                    int n; cin>>n;
#define r2(a,b)                 int a,b; cin>>a>>b;
#define r3(a,b,c)               int a,b,c; cin>>a>>b>>c; 
#define rs(s)                   string s; cin>>s;
#define rc(ch)                  char ch; cin>>ch;
#define cy                      cout<<"YES"
#define cn                      cout<<"NO"
#define nl                      cout<<"\n"
#define endl                    "\n"
#define pb                      push_back
#define fr                      first
#define sc                      second
#define ia(a,n)                 for(int i=0;i<n;i++)cin >> a[i];
#define oa(a,n)                 for(int i=0;i<n;i++) cout<<a[i]<<" "; 
#define iv(a)                   for(auto &x : a)cin >> x;
#define sa(a,n)                 sort(a,a+n)
#define pv(v)                   for(auto x: v) cout<<x<<" ";   
#define sz(x)                   ((long long)x.size())
#define vi                      vector<int>
#define uci                     unordered_map<char, int>
#define uic                     unordered_map<int, char>
#define uii                     unordered_map<int, int>
#define mci                     map<char, int>
#define mic                     map<int, char>
#define mii                     map<int, int>
#define pii                     pair<int, int>
#define pci                     pair<char, int>
#define pic                     pair<int, char>
#define vpii                    vector<pii>
#define mod                     1000000007
#define max3(a,b,c)             max(a, max(b, c))
#define max4(a,b,c,d)           max(a, max(b, max(c, d)))
#define min3(a,b,c)             min(a, min(b, c))
#define sumOfArray(v)           accumulate(v.begin(), v.end(), 0); 
#define CountInArray(v,x)       count(v.begin(), v.end(), x)
#define maxElemOfArray(v)       max_element(v.begin(), v.end())
#define mst(arr,x)              memset(arr, x, sizeof(arr));
#define dbg(x)                  cout<<#x<<"="<<(x)<<endl;
#define all(v)                  (v).begin(), (v).end()
#define dbg2(x,y)               cout<<#x<<"="<<(x)<<" "<<#y<<"="<<(y)<<endl;
#define dbg3(x,y,z)             cout<<#x<<"="<<(x)<<" "<<#y<<"="<<(y)<<" "<<#z<<"="<<(z)<<endl;
#define dbg4(x,y,z,w)           cout<<#x<<"="<<(x)<<" "<<#y<<"="<<(y)<<" "<<#z<<"="<<(z)<<" "<<#w<<"="<<(w)<<endl;
#define intmax                  INT_MAX
#define intmin                  INT_MIN
bool cmp(int a,int b){
    return a&1 < b&1;
}   
int a[4] = {1, 0, -1, 0};
int b[4] = {0, 1, 0, -1};

void solve(){
    r(n);
    vector<pii> arr(n);
    fr1(i,0,n){
        cin >> arr[i].sc >> arr[i].fr;
    }
    sort(all(arr));
    int cnt = 0, money = 0;
    int s = 0, e = n - 1;
    //fr = discount after buy  sc = atleast
    while(s <= e){
        if(cnt >= arr[s].fr){
            cnt += arr[s].sc;
            money += arr[s].sc;
            s++;
        }
        else{
            int mini = min(arr[s].fr - cnt, arr[e].sc);
            cnt += mini;
            money += 2 * mini;
            if(mini == arr[e].sc){
                e--;
            }
            else{
                arr[e].sc -= mini;
            }
        }
    }
    cout << money;
}
signed main(){
    fast; 
    cout << fixed << setprecision(25);
    cerr << fixed << setprecision(10);
    auto start = std::chrono::high_resolution_clock::now();
    
    solve();
    // int t=1;cin>>t;while(t--){solve();nl;}
    
    auto stop = std::chrono::high_resolution_clock::now(); 
    auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start);
    
}
// https://atcoder.jp/contests/abc106/tasks/abc106_c
    // https://atcoder.jp/contests/abc105/tasks/abc105_c
    // https://atcoder.jp/contests/abc104/tasks/abc104_c
    // https://atcoder.jp/contests/abc102/tasks/arc100_a
    // https://atcoder.jp/contests/abc098/tasks/arc098_a
```