#include <bits/stdc++.h>

#pragma ide diagnostic ignored "OCUnusedGlobalDeclarationInspection"
#pragma ide diagnostic ignored "OCUnusedMacroInspection"
using namespace std;

#define itn int
#define sqr(a) ((a)*(a))
#define cub(a) ((a)*(a)*(a))
#define forn(i, n) for(int i = 0; i < (int)n; ++i)
#define rep(i, k, n) for(int i = (int)k; i < (int)n; ++i)
#define nrof(i, n) for(int i = (int)n - 1; i >= 0; --i)
#define x first
#define y second
#define mp make_pair
#define sz(a) (int)(a.size())
#define pb push_back
#define eb emplace_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double dd;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int N = 1e6 + 9;
const int M = 2e3 + 9;
const int INFINT = 1e9 + 9;
const ll INFLL = 1e18 + 9;
const ll MOD = (ll) (1000000007);

int solve() {
    int n;
    cin >> n;
    vector<pll> a(n);
    forn(i, n) {
        cin >> a[i].y >> a[i].x;
    }
    // .x - надо на скидку, .y - всего надо
    sort(all(a));
    // l - стараемся со скидкой, r - затариваемся
    int l = 0, r = n - 1;
    ll ans = 0, total = 0;
    while (l < r) {
        ll max_buy = total + a[r].y;
        if (a[l].x <= total) { // скидка < тотал, берем по 1
//            cout << "buy all sale " << l << ' ' << r << endl;
            ans += a[l].y;
            total += a[l].y;
            a[l].y = 0;
            ++l;
        } else if (a[l].x > max_buy) { // сколько бы ни взяли, скидки не будет
//            cout << "buy all max price " << l << ' ' << r << endl;
            ans += a[r].y * 2;
            total += a[r].y;
            a[r].y = 0;
            --r;
        } else {
//            cout << "buy to gr and sale " << l << ' ' << r << endl;
            ll diff = a[l].x - total; // надо докупить до скидки
            ans += diff * 2;
            total += diff;
            a[r].y -= diff;

//            cout << "new r " << a[r].y << endl;

            ans += a[l].y; // затариваемся по скидке
            total += a[l].y;
            a[l].y = 0;
            ++l;
        }
//        cout << ans << endl;
    }
    if (a[l].x <= total) {
        ans += a[l].y;
    } else if (a[l].x > total + a[l].y) {
        ans += a[l].y * 2;
    } else {
        ll diff = a[l].x - total;
        ans += diff * 2;
        a[l].y -= diff;
        ans += a[l].y;
    }
    cout << ans << endl;
    return 0;
}

int main() {
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);

    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int tt = 1;
//    cin >> tt;
    while (tt--) {
        solve();
    }

    return 0;
}