#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll , ll> II;
const int N = 2e5 + 10;
const int block = 1410;
const int logN = 18 + 1;
const int M = (1 << 10);
const ll MOD = 1e9 + 7;
const ll INF = 1e18;


II a[N];
ll cur , ans;

void process(){
    int n;
    cin >> n;
    for (int i = 1; i <= n ; i++) cin >> a[i].second >> a[i].first;
    sort(a + 1 , a + 1 + n);
    int id = n;
    for (int i = 1; i <= n ; i++)
    {
        while (i <= id && a[i].first > cur)
        {
            ll _min = min(a[i].first - cur, a[id].second);
            cur += _min;
            a[id].second -= _min;
            ans += 2 *_min;
            if (a[id].second == 0) id--;
        }
        ans += a[i].second;
        cur += a[i].second;
    }
    cout << ans <<endl;
}

int main(){
   ios_base::sync_with_stdio(false);
   cin.tie();

   process();
   return 0;
}
