#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;

auto io_speed = []() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    return 0;
}();

ll pow_mod(ll a, ll e, ll mod) {
    ll res = 1;
    while(e) {
        if(e&1) res = res * a%mod;
        e>>=1;
        a=a*a%mod;
    }
    return res;
}

ll inv_mod(ll b, ll m) {
    ll a=m, x=0, y=1, r, q;
    while(b) {
        r = a%b;
        q = a/b;
        a = b;
        b = r;
        r = x;
        x = y;
        y = r-q*y;
    }
    x+=m;
    return x%m;
}

void solve() {
    int n;
    cin >> n;
    list<pair<ll,ll>> prod;
    for(int i = 0; i < n; i++) {
        ll ai, bi;
        cin >> ai >> bi;
        prod.push_back({-bi, ai});
    }
    prod.sort();
    
    ll curr = 0, res = 0;
    while(!prod.empty()) {
        while(!prod.empty() && curr >= abs(prod.back().first)) {
            // cout << prod.back().second << " | " << abs(prod.back().first) << endl;
            res+=prod.back().second;
            curr+=prod.back().second;
            prod.pop_back();
        }
        
        if(!prod.empty()) {
            ll d = abs(prod.back().first) - curr;
            ll ai = prod.front().second;
            if(ai >= d) {
                res+=2*d;
                curr+=d;
                prod.front().second-=d;
                // cout << d << " --- " << abs(prod.front().first) << endl;
            } else {
                res+=2*ai;
                curr+=ai;
                // cout << ai << " .... " << abs(prod.front().first) << endl;
                prod.pop_front();
            }
        }
    }

    cout << res << endl;
}

int main() {
    int t = 1;
    // cin >> t;
    for(int i = 1; i <= t; i++) {
        // cout << "Case #"<<i<<": ";
        solve();
    }
}