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

#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define int long long
#define FIN(i,n) for(int i=0;i<n;i++)
#define loop(i, a, b) for (int i = a; i < b; i++)
#define vp vector<pair < int, int > >
#define pi pair<int, int>
#define vll vector<long long>
#define F first
#define S second
#define MINV(v) *min_element(v.begin(),v.end())
#define MINIV(v) min_element(v.begin(),v.end())-v.begin()
#define all(v) v.begin(), v.end()
#define pb push_back
#define ppb pop_back
#define TR(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)

// make_pair()
//vector<vector<char>> v(r, vector<char> (c,0));
int mod=1e9+7;
int INF=1e18;

bool isPrime(int n){if(n<=1)return false;if(n<=3)return true;
    if(n%2==0||n%3==0)return false;for(int i=5;i*i<=n;i=i+6)
    if(n%i==0||n%(i+2)==0)return false;return true;}

int nCr(int n,int r){int res=1;if(n<r)return -1;r=min(r,n-r);
    loop(i,0,r){res*=(n-i);res/=(i+1);}return res; }
    // return ((fact[n]*invfact[r])%mod * invfact[n-r])%mod); 

int nPr(int n,int r){int res=1;if(n<r)return -1;
    loop(i,0,r)res*=(n-i);return res;}
    // return (fact[n]*invfact[n-r])%mod; 

int modexp(int x,int y){
    int res=1;
    while(y>0){
        if(y&1) res=(res*x)%mod;
        x=(x*x)%mod;
        y=y>>1;
    }
    return res;
}

void solve()
{
    int n;
    cin>>n;
    vector<pair<int,int>> v;
    for(int i=1;i<=n;i++){
        int a,b;
        cin>>a>>b;
        v.push_back({b,a});
    }
    sort(all(v));
    int ans=0;
    int ct=0;
    int l=0,r=n-1;
    while(l<=r){
        if(ct>=v[l].F){
            ans+=v[l].S;
            ct+=v[l].S;
            v[l].S=0;
            l++;
            continue;
        }
        if(v[l].F-ct>=v[r].S){
            ans+=2*v[r].S;
            ct+=v[r].S;
            v[r].S=0;
            r--;
        }
        else{
            int req=v[l].F-ct;
            ans+=2*req;
            ct+=req;
            v[r].S-=req;
        }
    }
    cout<<ans;
}

signed main(){
    fast
    int t=1;
    // cin>>t;
    while(t--)
        solve();
    return 0;
}