/*
power code taken from geeks for geeks
*/
// cout << setprecision (2) << fixed << 1.2;

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;

// THINGS TO REMEMBER
// ENDL is slow, '\n' is fast
// Clear everything (including graphs) between test cases
// use anti-anti-hash: https://codeforces.com/blog/entry/62393
// pb-ds: https://codeforces.com/blog/entry/11080
// check when to use LLONG_MAX/LLONG_MIN vs INT_MAX or INT_MIN
// You frequently suffer from confirmation bias - you trust your initial solution and miss simple things. 
// When you hit a roadblock, remember to rethink the solution ground up, not just try hacky fixes


#define fflush fflush(stdout)
#define PLL pair<ll, ll>

const ll INF = 1e18 + 5;
const ll MAX_N = (1LL << 18) + 5;
const ll MODD = 1e9 + 7;
const ld PI = 3.14159265358979323846;


ll max(ll a,ll b){return a>b?a:b;}
ll min(ll a,ll b){return a<b?a:b;}
double max(double a,double b){return a>b?a:b;}
double min(double a,double b){return a<b?a:b;}

ll modd(ll a){if(a<0LL) return a*-1LL;return a;}
ld modd(ld a){if(a<0.0) return a*-1.0;return a;}

ll modulo(ll a){return (a + MODD) % MODD; }

ll iii = 0;
void _debug() {cout << "here " << (iii++) << endl;}
ll power(ll k, ll p){
    if (p == 0)
        return 1LL;
 
    ll P = power(k, p/2LL);
    P = (P * P) % MODD;
 
    if (p % 2 == 1)
        P = (P * k) % MODD;
 
    return P;
}


struct Str {
    ll ai, bi;
};

bool comp_v(Str &a, Str &b) {
    if(a.bi == b.bi) {
        return a.ai < b.ai;
    }
    return a.bi < b.bi;
}

void solve() {
    ll n;
    cin >> n;
    ll a[n], b[n];
    for(ll i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
    }

    vector<Str> v;
    for(ll i = 0; i < n; i++) {
        v.push_back({a[i], b[i]});
    }

    sort(v.begin(), v.end(), comp_v);

    ll lo = 0, hi = v.size() - 1;
    ll cnt = 0, ans = 0;
    while(lo <= hi) {
        if(cnt >= v[lo].bi) {
            ll items = v[lo].ai;
            ans += items;
            cnt += items;
            v[lo].ai -= items;
        }
        else if(cnt + v[hi].ai >= v[lo].bi) {
            ll items = v[lo].bi - cnt;
            ans += items * 2;
            cnt += items;
            v[hi].ai -= items;

            items = v[lo].ai;
            ans += items;
            cnt += items;
            v[lo].ai -= items;
        }
        else {
            ll items = v[hi].ai;
            ans += items * 2;
            cnt += items;
            v[hi].ai -= items;
        }
        if(v[lo].ai == 0) {
            lo++;
        }
        if(v[hi].ai == 0) {
            hi--;
        }

        // cout << " lo hi " << lo << " " << hi << " " << ans << endl;

    }

    if(lo < v.size() && v[lo].ai > 0) {
        if(v[lo].bi <= cnt) {
            ans += v[lo].ai;
        }
        else {
            ans += v[lo].ai * 2;
        }
    }

    cout << ans << endl;
        
}

int main(){


    #ifndef ONLINE_JUDGE 

    // For getting input from input.txt file 
    freopen("input.txt", "r", stdin); 

    // Printing the Output to output.txt file 
    freopen("output.txt", "w", stdout); 
    #endif 


    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    


    ll t=1;

    // cin >> t;

    // _debug();


    for(ll i = 0; i < t; i++) {
        
        // cout << "---------------case " << i + 1 << endl;

        // cout << "Case #" << i + 1 << ": ";

        solve();
    }
    
    return 0;
}