#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

ll n;
vector< pair<ll,ll>> vect;

bool comp( pair<ll,ll>  v1, pair<ll,ll> v2){
    return v1.second < v2.second;
}

ll min(ll a, ll b){
    if (a>b) return b;
    else return a;
}

ll max(ll a, ll b){
    if (a>b) return a;
    else return b;
}

void solve(){
    ll cost = 0;
    ll number_of_products = 0 ;
    ll chippest = 0;
    ll more_expansive = n-1;
    ll req;
    ll buy;
    while (chippest < more_expansive){
        req = max(vect[chippest].second - number_of_products,0) ; 
        if( req == 0){//buy the lowest with reduction
            cost += vect[chippest].first ;
            number_of_products += vect[chippest].first;
            vect[chippest].first = 0;
            chippest++;
        }
        else{//buy the more expansive
            buy = min(req,vect[more_expansive].first);
            cost = cost + buy*2;
            number_of_products += buy;
            vect[more_expansive].first -= buy;
            if(vect[more_expansive].first == 0){
                more_expansive--;
            }
        }
    }
    while (vect[chippest].first > 0){
        req = max(vect[chippest].second - number_of_products,0);
        if( req == 0 ){//we buy them with reduction
            cost += vect[chippest].first;
            break;
        }
        else{//without reduction
            buy = min(req,vect[chippest].first);
            cost = cost + buy*2;
            number_of_products += buy;
            vect[chippest].first -= buy;
        }
        number_of_products++;
    }
    cout << cost << endl;
}

int main(){ 
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n ;
    ll a,b;
    for (ll i = 0; i < n; i++){
        cin >> a >> b;
        vect.push_back(make_pair(a,b));
    }
    sort(vect.begin(), vect.end(), comp);
    solve();
    return 0;
}
