#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
    long long int n;
    cin >> n;
    vector<pair<long long int,long long int>>v;
    for(int i=0;i<n;i++){
            long long int x;
            long long int y;
            cin >> x >> y;
            v.push_back(make_pair(y,x));
    }
    sort(v.begin(),v.end());
    long long int s=0;
    long long int e=n-1;
    long long int ans=0;
    long long int total=0;
    while(s<=e){
        if(v[s].first<=total){
            ans=ans+v[s].second;
            total=total+v[s].second;
            s++;
        }
        else {
            long long int need=min(v[s].first-total,v[e].second);
            total=total+need;
            v[e].second=v[e].second-need;
            ans=ans+2*need;
            if(v[e].second==0){
                e--;
            }
            
        }
    }
    cout << ans << endl;


    return 0;
}
