#include <bits/stdc++.h>
using namespace std;typedef long double ld;typedef double db;
typedef long long int ll;typedef unsigned long long int ul;
bool cmp(pair<ll,ll> &a,pair<ll,ll> &b){
    if(a.second<b.second || a.second>b.second){
        return a.second<b.second;
    }
    else{
        return a.first<=b.first;
    }
}
void solve(){
    ll n;
    cin >> n;
    std::vector<pair<ll,ll>> v;
    ll a,b;
    for(ll i=0;i<n;i++){
        cin >> a >>b;
        v.push_back(make_pair(a,b));
    }
    sort(v.begin(), v.end(),cmp);
    ll tot=0;
    for(ll i=0;i<n;i++){
        // cout<<v[i].first<<" "<<v[i].second<<"\n";
        tot+=v[i].first;
    }
    ll cnt=0;
    ll cost=0;
    for(ll i=0;i<n;i++){
        if(i==0){
            cnt=min(v[i].second,tot);
            cost=2*cnt;
            tot-=cnt;
            ll aa=min(v[i].first,tot);
            cnt+=aa;
            tot-=aa;
            cost+=aa;
        }
        else{
            if(cnt<v[i].second){
                ll bb=v[i].second-cnt;
                ll aa=min(bb,tot);
                cost+=2*aa;
                cnt+=aa;
                tot-=aa;
                aa=min(v[i].first,tot);
                cost+=aa;
                cnt+=aa;
                tot-=aa;
            }
            else{
                ll aa=min(v[i].first,tot);
                cost+=aa;
                cnt+=aa;
                tot-=aa;
            }
        }
    }
    cout<<cost<<"\n";
}
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 
    // freopen("output.txt", "r", stdin);freopen("output.txt", "w", stdout);
    ll t=1;
    // cin >>t; 
    while(t--) solve();
}