//100gods

#include <bits/stdc++.h>
#define int long long
#define vec2 vector<vector<int>>
#define fio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define FRE freopen("elegant.in","r",stdin);freopen("elegant.out","w",stdout)
#define pii pair <int,int>
#define vii vector <int>
#define vpii vector <pii>
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define minii LLONG_MAX
#define all(x) x.begin(), x.end()
#define mem(a,b) memset(a,b,sizeof(a))
#define double long double
#define eb emplace_back
const double pi = 3.1415926535898;
const int mod=1e9+7;
//const int mod=998244353;

using namespace std;

int32_t main()
{
    fio;
    int n,x,y;
    cin>>n;
    vpii v;
    int cur=0,ans=0;
    int ii=n-1;
    for (int i=0;i<n;i++)
    {
        cin>>x>>y;
        v.pb({y,x});
    }
    sort(all(v));
    for (int i=0;i<n;i++)
    {
        if (v[i].ss==0)
        {
            break;
        }
        //cout<<i<<" "<<ii<<" "<<v[i].ff<<" "<<v[i].ss<<"\n";
        if (v[i].ff<=cur)
        {
            cur+=v[i].ss;
            ans+=v[i].ss;
            v[i].ss=0;
            //cout<<ans<<" "<<cur<<"\n";
            continue;
        }
        //cout<<ans<<" "<<cur<<"\n";
        while (cur<v[i].ff&&v[i].ss&&ii>i)
        {
            x=min(v[ii].ss,v[i].ff-cur);
            ans+=2*x;
            v[ii].ss-=x;
            cur+=x;
            if (v[ii].ss==0)
            {
                ii--;
            }
        }
        if (ii==i)
        {
            x=min(v[ii].ss,v[i].ff-cur);
            ans+=2*x;
            v[ii].ss-=x;
            cur+=x;
            if (v[ii].ss==0)
            {
                ii--;
            }
        }
        ans+=v[i].ss;
        cur+=v[i].ss;
        //cout<<ans<<" "<<cur<<"\n";
    }
    cout<<ans;
    return 0;
}
