#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int N = 1e5+10;

struct node {
    ll a,b,i;
    bool operator < (const node &other) const {
        return b < other.b;
    }
}st[N];

int main()
{
    ll n,sum = 0;
    cin>>n;
    for(ll i = 1 ;i <= n; i++)
        cin>>st[i].a>>st[i].b, sum += st[i].a;
    
    sort(st+1,st+n+1);

    ll nn = 0, tot = 0;
    for(ll i = 1 ;i <= n; i ++)
    {   

        ll t = 0;

        if(st[i].b >= sum) break;

        if(tot > st[i].b) t = min(st[i].a,sum - tot), tot += t, nn += t;
        else {
            tot = st[i].b;
            nn += min(sum - tot, st[i].a);
            tot += st[i].a;
        }

        // cout<<nn<<' '<<tot<<endl;
    }
    
    cout<<sum * 2 - nn<<endl;

    return 0;
}