//doit4von
//LLJW
//999
//Rip to lil loaded 6locc 6a6y
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
const long long mod=pow(10,9)+7;
typedef long long ll;
#define pb push_back
#define vi vector<int>
#define sz(x) (int)(x).size()
#define ff first
#define ss second
void google(int tc)
{
    cout<<"Case #"<<tc<<": ";
}
ll convert(string z)
{
      ll ans=0;
      int count=0;
      for(int i=z.length()-1;i>=0;i--)
      {
            ans=ans+(z[i]-'0')*pow(10,count);
            count++;
      }
      return ans;
}
bool palindrome(const string &s) {
    int n = s.length();
    for(int i = 0; i < n; i++) {
        if(s[i] != s[n - i - 1]) return false;
    }
    return true;
}
bool primecheck(long long int z)
{
      for(long long int i=2;i*i<=z;i++)
      {
            if(z%i==0)
            return false;
      }
      return true;
}
string boolean (int y)
{
      string ans="";
      while(y>0)
      {
            ans=(char)('0'+y%2)+ans;
            y=y/2;
      }
      return ans;
}
ll rem(ll x)
{
      ll ans=0;
      while(x>0)
      {
            if(x%10)
            ans=ans*10+x%10;
            x=x/10;
      }
      return ans;
}
void SieveOfEratosthenes(int n)
{
    bool prime[n+1];
    memset(prime, true, sizeof(prime));
    prime[1]=false;
    for (int p = 2; p * p <= n; p++)
    {
        if (prime[p] == true)
        {
            for (int i = p * p; i <= n; i += p)
            prime[i] = false;
        }
    }
}
ll binpow ( ll base , ll ex)
{
      ll ans=1;
      while(ex>0)
      {
            if(ex%2==1)
            {
                  ex--;
                  ans=(ans%mod*base%mod)%mod;
            }
            ex=ex/2;
            base=(base%mod * base%mod)%mod;
      }
      return ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin>>n;
pair<ll , ll> ar[n];
ll rem=0ll;
for(int i=0;i<n;i++)
{
      cin>>ar[i].ss>>ar[i].ff;
      rem=rem+ar[i].ss;
}
sort( ar , ar+n);
ll ans=0;
ll pur=0;
for(int i=0;i<n;i++)
{
      if(pur< ar[i].ff)//not eligible for discount
      {
            ll now=std::min( rem , -pur+ar[i].ff);
            ans=ans+2*now;
            pur=pur+now;
            rem=rem-now;
      }
      ll now=std::min ( rem , ar[i].ss);
      ans=ans+now;
      pur=pur+now;
      rem=rem-now;
}
cout<<ans<<"\n";
}

