#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define pb push_back
#define pp pop_back
#define pii pair<int,int>
#define piii pair<int,pii>
#define fi first
#define se second
const int mod = 1e9+7;

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif

    int n;
    cin>>n;
    vector<pii> v;
    for(int i=0;i<n;i++) {
        int a,b;
        cin>>a>>b;
        v.pb({b,a});
    }
    sort(v.begin(),v.end());
    
    int sum=0,k=0;
    int s = 0, e = n-1;
    while(s<=e) {
        int &a = v[s].se;
        int &b = v[s].fi;
        if(k>=b) {
            sum += a;
            k += a;
            s++;
        }
        else {
            int &aa = v[e].se;
            int &bb = v[e].fi;
            if(aa >= b-k) {
                aa -= (b-k);
                sum += 2*(b-k);
                k += (b-k);
            }
            else {
                sum += 2*aa;
                k += aa;
                e--;
            }
        }
    }
    cout<<sum;
    return 0;
}
