// PRANAV SINGHAL
// IIIT-Allahabad
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define vll vector<ll>
#define send {ios_base::sync_with_stdio(false);}
#define help {cin.tie(NULL); cout.tie(NULL);}
#define nl "\n"
#define ALL(v) (v.begin(), v.end())
void solve(){
 ll n;cin>>n;
 vector<pair<ll,ll> > v;
 ll bg=0;
 for(ll i=1;i<=n;i++){
     ll a,b;cin>>a>>b;
     v.pb({b,a});
 }
 sort(v.begin(),v.end());
 ll i=0,j=v.size()-1;
 ll ans=0;
 while(i<=j){
     if(v[i].first<=bg){
         ans+=v[i].second;
         bg+=v[i].second;
         i++;
     }
     else{
         ll req = min(v[i].first-bg,v[j].second);
         ans+=req*2;
         bg+=req;
         v[j].second-=req;
         if(v[j].second==0) j--;
     }
 }
 cout<<ans<<nl;

}
int main()
{
  send help
  #ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
  #endif
    solve();

   
    
  return 0;
}