#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define ll long long 
#define endl "\n"

bool myCompare(pair<ll,ll> p1, pair<ll,ll> p2){
    if(p1.second==p2.second){
        return p1.first>p2.first;
    }else{
         return p1.second<p2.second;
    }
   
}



int main()
{
    int n;
    ll ans=0;
    ll count=0;
    cin>>n;
    ll first, second, diff=0;
    vector< pair<ll,ll> > a;
    
    

    for(int i=0;i<n;i++){
        cin>>first>>second;
        a.push_back(make_pair(first,second));
       
    }

    sort(a.begin(),a.end(),myCompare);

    ll i=0;
    ll j=n-1;
    while(i<=j){
        if(count>=a[i].second){
            count=count+a[i].first;
            ans=ans+a[i].first;
            i++;
        }else{
         
         ll required= min(a[i].second-count, a[j].first);
         count=count+required;
         ans=ans+2*required;
         a[j].first=a[j].first-required;
         if(a[j].first==0){
             j--;
         }
         











        }
    }

    cout<<ans<<endl;
    
}





    
//    ll i=0;
//    ll j=n-1;
//    while(i<n && j>-1){
//        if(i==0){
//            diff=a[i].second-0;
//        }else{
//            diff=a[i].second-a[i-1].second;
//        }

       
//        while(diff>0){
//            if(a[j].first==diff){
              
//            j=j-1;
//        }else if(a[j].first>diff){
           
//            a[j].first=a[j].first-diff;
//        }else{
//             diff=diff-a[j].first;
//             j=j-1;
          
//        }

       
//         ans=ans+2*

//        }
       







//    }

















