#include<bits/stdc++.h>
#include<iostream>
#include<vector>
#include<string>
#include<cstring>
#include<cstdio>
#include<map>
#include<set>
#include<utility>
#include<algorithm>
#include<queue>
#include<cstdlib>
using namespace std;

// - - - - - - Data Types - - - - - - //

#define SF1(A)              scanf("%d", &A)
#define SF2(A,B)            scanf("%d %d", &A, &B)
#define SF3(A,B,C)          scanf("%d %d %d", &A, &B, &C)

typedef long long int LLI;
typedef unsigned long long int ULL;
#define SL1(A)              scanf("%lld", &A)
#define SL2(A,B)            scanf("%lld %lld", &A, &B)
#define SL3(A,B,C)          scanf("%lld %lld %lld", &A, &B, &C)

#define SD1(A)              scanf("%lf", &A)
#define SD2(A,B)            scanf("%lf %lf", &A, &B)
#define SD3(A,B,C)          scanf("%lf %lf %lf", &A, &B, &C)

// - - - - - - Vectors - - - - - - //
typedef vector<int> VI;
typedef vector<LLI> VLLI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef vector<VI> VVI;
typedef vector<VS> VVS;

#define scanVI(V, N)        for(int I=0; I<N; I++){ int X; SF1(X); V.PB(X); }
#define scanVLLI(V, N)      for(int I=0; I<N; I++){ LLI X; SL1(X); V.PB(X); }
#define scanVS(V, N)        for(int I=0; I<N; I++){ string X; cin >> X; V.PB(X); }
#define scanVD(V, N)        for(int I=0; I<N; I++){ double X; SD1(X); V.PB(X); }

// - - - - - - Maps - - - - - - //
typedef map<int, int> MII;
typedef map<int, string> MIS;
typedef map<int, char> MIC;
typedef map<string, int> MSI;
typedef map<char, int> MCI;
typedef map<int, VI> MIVI;

// - - - - - - Pairs - - - - - - //
typedef pair<int, int> PII;
typedef pair<string, string> PSS;
typedef pair<char, char> PCC;
typedef pair<int, string> PIS;
typedef pair<int, char> PIC;
typedef pair<string, char> PSC;
typedef pair<LLI, LLI> PLL;

// - - - - - - Sets - - - - - - //
typedef set<int> SI;
typedef set<LLI> SLLI;
typedef set<string> SS;
typedef set<char> SC;
// - - - - - - - - - - - - - - - - - - //

//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")

clock_t Start;
inline void runTime(){ 
	#ifdef gHost
	fprintf(stderr, "\n>>Runtime: %.10f sec\n", (double) (clock() - Start) / CLOCKS_PER_SEC); 
	#endif
}

#define TIMER				runTime();
#define PF                  printf
#define SF                  scanf
#define PB                  push_back
#define SR(N)               right<<setw(N)
#define SL(N)               left<<setw(N)
#define PREC(N)             fixed << setprecision(N)
#define RJUST(N, C)         right<<setw(N)<<setfill(C)
#define LJUST(N, C)         left<<setw(N)<<setfill(C)
#define POP                 pop_back()
#define PP                  prev_permutation
#define NP                  next_permutation
#define MP                  make_pair
#define CLRN(A)          	memset(A, -1, sizeof(A))
#define CLR(A)              memset(A, 0, sizeof(A))
#define BitCount(A)         __builtin_popcount(A)
#define ALL(A)              A.begin(), A.end()
#define ALLN(A, N)          (A, A+N)
#define BSRCN(A, N, X)      binary_search(ALLN(A, N), X)
#define BSRC(A, Z)          binary_search(ALL(A), Z)
#define UNQ(V)          	sort(ALL(V)),(V).resize(unique(ALL(V))-V.begin())
#define MAX                 100007
#define IMAX                INT_MAX
#define LMAX                2000000000000000000LL
#define MIN                 -10000007
#define PI                  acos(-1)
#define BR                  puts("")
#define FastIO              { ios_base::sync_with_stdio(false); cin.tie(nullptr); }
#define READ()              freopen("input.txt", "r", stdin)
#define WRITE()             freopen("output.txt", "w", stdout)
#define IO                  READ(); WRITE();
#define SEED                srand((rand()-time(NULL)))
#define ran()               (((((1ULL*rand())<<31)|(1ULL*rand()))<<13)^((((1ULL*rand())<<32)|(1ULL*rand()))))
#define rran(A, B)          ((ran() % ((B) - (A) + 1)) + (A))
#define SZE(A)              (int)A.size()
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
/*----------------------Graph Moves----------------*/
int ROW[]={+1,-1,+0,+0};
int COL[]={+0,+0,+1,-1};

int X[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
int Y[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move

int KX[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
int KY[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move

VI basePrime={1009, 1013, 1019, 1021, 1031, 1223, 1229, 1231, 1237, 1249, 1289, 1291, 1297, 1301, 1303, 353, 359, 367, 373, 379, 859, 863, 877, 881, 883,
                1931, 1933, 1949, 1951, 1973, 401, 409, 419, 421, 431, 1709,  1721, 1723, 1733, 1741, 3499, 3511, 3517, 3527, 3529, 929, 937, 941, 947, 953};

/*----------------------------------------------------------------------------------------------------------------------------------------------*/

template<class XXX> XXX GCD(XXX a, XXX b) { return b == 0 ? a : GCD(b , a % b); }
template<class XXX> XXX LCM(XXX a, XXX b) { return a * (b/GCD(a, b)); }
template <class T> inline void fastRead(T &r){
    r=0; register int f=1; register char ch=getchar();
    while (ch<'0' or ch>'9'){f=(ch=='-'?-1:1), ch=getchar();}
    while (ch>='0' and ch<='9'){r=r*10+ch-'0', ch=getchar();}
    r*=f;
}
template<class XXX> inline XXX moduler(XXX num, XXX mod) {return (num>=mod?num-=mod:num);}

/// BITMASK
inline int Set(int pos, int N){return N=N | (1<<pos);}
inline int Reset(int pos, int N){return N= N & ~(1<<pos);}
inline bool Check(int pos, int N){return (bool)(N & (1<<pos));}

inline void OPFILE(){
    #ifdef gHost
    	Start=clock();
        freopen("D:\\OneDrive - Daffodil International University\\Programming\\CPP\\input.txt", "r", stdin);
        freopen("D:\\OneDrive - Daffodil International University\\Programming\\CPP\\output.txt", "w", stdout);
    #endif
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - END - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //

int main()
{ 
	OPFILE(); 

    int n; cin >> n;
    vector<pair<LLI, LLI>> p;
    for(int i=0; i<n; i++)
    {
        LLI a, b; cin >> a >> b;
        p.PB({b, a});
    }

    sort(ALL(p));

    LLI ans=0, bought=0;
    for(int i=0, j=n-1; i<n; )
    {
        if(!p[i].second) 
        {
            i++;
            continue;
        }
        if(bought>=p[i].first)
        {
            ans+=p[i].second;
            bought+=p[i].second;
            i++; 
            continue;
        }

        LLI take=min(p[j].second, max(0LL, p[i].first-bought));
        ans+=take*2;
     
        p[j].second-=take;
        bought+=take; 

        if(!p[j].second and j) j--;  
    }

    cout << ans << endl;
    

	TIMER;
}
### Prompt

Construct a Cpp code solution to the problem outlined:

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>
#include<iostream>
#include<vector>
#include<string>
#include<cstring>
#include<cstdio>
#include<map>
#include<set>
#include<utility>
#include<algorithm>
#include<queue>
#include<cstdlib>
using namespace std;

// - - - - - - Data Types - - - - - - //

#define SF1(A)              scanf("%d", &A)
#define SF2(A,B)            scanf("%d %d", &A, &B)
#define SF3(A,B,C)          scanf("%d %d %d", &A, &B, &C)

typedef long long int LLI;
typedef unsigned long long int ULL;
#define SL1(A)              scanf("%lld", &A)
#define SL2(A,B)            scanf("%lld %lld", &A, &B)
#define SL3(A,B,C)          scanf("%lld %lld %lld", &A, &B, &C)

#define SD1(A)              scanf("%lf", &A)
#define SD2(A,B)            scanf("%lf %lf", &A, &B)
#define SD3(A,B,C)          scanf("%lf %lf %lf", &A, &B, &C)

// - - - - - - Vectors - - - - - - //
typedef vector<int> VI;
typedef vector<LLI> VLLI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef vector<VI> VVI;
typedef vector<VS> VVS;

#define scanVI(V, N)        for(int I=0; I<N; I++){ int X; SF1(X); V.PB(X); }
#define scanVLLI(V, N)      for(int I=0; I<N; I++){ LLI X; SL1(X); V.PB(X); }
#define scanVS(V, N)        for(int I=0; I<N; I++){ string X; cin >> X; V.PB(X); }
#define scanVD(V, N)        for(int I=0; I<N; I++){ double X; SD1(X); V.PB(X); }

// - - - - - - Maps - - - - - - //
typedef map<int, int> MII;
typedef map<int, string> MIS;
typedef map<int, char> MIC;
typedef map<string, int> MSI;
typedef map<char, int> MCI;
typedef map<int, VI> MIVI;

// - - - - - - Pairs - - - - - - //
typedef pair<int, int> PII;
typedef pair<string, string> PSS;
typedef pair<char, char> PCC;
typedef pair<int, string> PIS;
typedef pair<int, char> PIC;
typedef pair<string, char> PSC;
typedef pair<LLI, LLI> PLL;

// - - - - - - Sets - - - - - - //
typedef set<int> SI;
typedef set<LLI> SLLI;
typedef set<string> SS;
typedef set<char> SC;
// - - - - - - - - - - - - - - - - - - //

//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")

clock_t Start;
inline void runTime(){ 
	#ifdef gHost
	fprintf(stderr, "\n>>Runtime: %.10f sec\n", (double) (clock() - Start) / CLOCKS_PER_SEC); 
	#endif
}

#define TIMER				runTime();
#define PF                  printf
#define SF                  scanf
#define PB                  push_back
#define SR(N)               right<<setw(N)
#define SL(N)               left<<setw(N)
#define PREC(N)             fixed << setprecision(N)
#define RJUST(N, C)         right<<setw(N)<<setfill(C)
#define LJUST(N, C)         left<<setw(N)<<setfill(C)
#define POP                 pop_back()
#define PP                  prev_permutation
#define NP                  next_permutation
#define MP                  make_pair
#define CLRN(A)          	memset(A, -1, sizeof(A))
#define CLR(A)              memset(A, 0, sizeof(A))
#define BitCount(A)         __builtin_popcount(A)
#define ALL(A)              A.begin(), A.end()
#define ALLN(A, N)          (A, A+N)
#define BSRCN(A, N, X)      binary_search(ALLN(A, N), X)
#define BSRC(A, Z)          binary_search(ALL(A), Z)
#define UNQ(V)          	sort(ALL(V)),(V).resize(unique(ALL(V))-V.begin())
#define MAX                 100007
#define IMAX                INT_MAX
#define LMAX                2000000000000000000LL
#define MIN                 -10000007
#define PI                  acos(-1)
#define BR                  puts("")
#define FastIO              { ios_base::sync_with_stdio(false); cin.tie(nullptr); }
#define READ()              freopen("input.txt", "r", stdin)
#define WRITE()             freopen("output.txt", "w", stdout)
#define IO                  READ(); WRITE();
#define SEED                srand((rand()-time(NULL)))
#define ran()               (((((1ULL*rand())<<31)|(1ULL*rand()))<<13)^((((1ULL*rand())<<32)|(1ULL*rand()))))
#define rran(A, B)          ((ran() % ((B) - (A) + 1)) + (A))
#define SZE(A)              (int)A.size()
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
/*----------------------Graph Moves----------------*/
int ROW[]={+1,-1,+0,+0};
int COL[]={+0,+0,+1,-1};

int X[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
int Y[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move

int KX[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
int KY[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move

VI basePrime={1009, 1013, 1019, 1021, 1031, 1223, 1229, 1231, 1237, 1249, 1289, 1291, 1297, 1301, 1303, 353, 359, 367, 373, 379, 859, 863, 877, 881, 883,
                1931, 1933, 1949, 1951, 1973, 401, 409, 419, 421, 431, 1709,  1721, 1723, 1733, 1741, 3499, 3511, 3517, 3527, 3529, 929, 937, 941, 947, 953};

/*----------------------------------------------------------------------------------------------------------------------------------------------*/

template<class XXX> XXX GCD(XXX a, XXX b) { return b == 0 ? a : GCD(b , a % b); }
template<class XXX> XXX LCM(XXX a, XXX b) { return a * (b/GCD(a, b)); }
template <class T> inline void fastRead(T &r){
    r=0; register int f=1; register char ch=getchar();
    while (ch<'0' or ch>'9'){f=(ch=='-'?-1:1), ch=getchar();}
    while (ch>='0' and ch<='9'){r=r*10+ch-'0', ch=getchar();}
    r*=f;
}
template<class XXX> inline XXX moduler(XXX num, XXX mod) {return (num>=mod?num-=mod:num);}

/// BITMASK
inline int Set(int pos, int N){return N=N | (1<<pos);}
inline int Reset(int pos, int N){return N= N & ~(1<<pos);}
inline bool Check(int pos, int N){return (bool)(N & (1<<pos));}

inline void OPFILE(){
    #ifdef gHost
    	Start=clock();
        freopen("D:\\OneDrive - Daffodil International University\\Programming\\CPP\\input.txt", "r", stdin);
        freopen("D:\\OneDrive - Daffodil International University\\Programming\\CPP\\output.txt", "w", stdout);
    #endif
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - END - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //

int main()
{ 
	OPFILE(); 

    int n; cin >> n;
    vector<pair<LLI, LLI>> p;
    for(int i=0; i<n; i++)
    {
        LLI a, b; cin >> a >> b;
        p.PB({b, a});
    }

    sort(ALL(p));

    LLI ans=0, bought=0;
    for(int i=0, j=n-1; i<n; )
    {
        if(!p[i].second) 
        {
            i++;
            continue;
        }
        if(bought>=p[i].first)
        {
            ans+=p[i].second;
            bought+=p[i].second;
            i++; 
            continue;
        }

        LLI take=min(p[j].second, max(0LL, p[i].first-bought));
        ans+=take*2;
     
        p[j].second-=take;
        bought+=take; 

        if(!p[j].second and j) j--;  
    }

    cout << ans << endl;
    

	TIMER;
}
```