#include<bits/stdc++.h>

using namespace std;
using LL = long long;



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

    int n; cin >> n;
    vector<pair<LL, LL>> pi(n);
    for (int i = 0; i < n; ++i) cin >> pi[i].second >> pi[i].first;

    sort(begin(pi), end(pi));
    LL ans = 0;
    for (LL sum = 0, l = 0, r = n - 1; l <= r; ++l) {
       while (sum < pi[l].first && r >= l) {
        pi[r].second -= (pi[l].first - sum);
        if (pi[r].second > 0) {
            ans += (pi[l].first - sum) * 2;
            sum = pi[l].first;
        }
        else {
            ans += (pi[l].first - sum + pi[r].second) * 2;
            sum += (pi[l].first - sum + pi[r].second);
            --r;
        }
       }
       if (r < l) break;
       if (sum < pi[l].first) {
           ans += pi[l].second * 2, sum += pi[l].second;
       }
       else ans += pi[l].second, sum += pi[l].second;
    }
    cout << ans;
    return 0;
}