#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
//#include <bits/stdc++.h>
//#include <unordered_map>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
#define rep(i, l, r) for(ll i = l, i##end = r; i <= i##end; i++)
#define repo(i, l, r) for(ll i = l, i##end = r; i < i##end; i++)
#define per(i, l, r) for(ll i = l, i##end = r; i >= i##end; i--)
#define pero(i, l, r)	for(ll i = l, i##end = r; i > i##end; i--)
#define mm1(a) memset(a, -1, sizeof(a))
#define mm2(a) memset(a, 0, sizeof(a))
#define mm3(a) memset(a, inf, sizeof(a))
#define mm4(a) memset(a, INF, sizeof(a))
#define swap(x,y) x^=y^=x^=y
#define debug(x) cerr << #x << " : " << x << " " << endl
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int dir[4][2] = { {1, 0}, {0, -1}, {1, 0}, {0, 1} };
ll ksm(ll a, ll n, ll mod) {
    ll t = 1;
    for (; n; n >>= 1, (a *= a) %= mod)
        if (n & 1)
            (t *= a) %= mod;
    return t;
}



map<ll, ll>mp;
vector<pll>v;



int main() {


    ll n;
    cin >> n;
    for (ll i = 1; i <= n; i++) {
        ll x, y;
        cin >> x >> y;
        mp[y] += x;
    }
    for (auto it : mp) {
        v.push_back({it.first, it.second});
    }
    sort(v.begin(), v.end());
    ll l = 0, r = v.size() - 1;
    ll res = 0, geshu = 0;
    while (l < r) {
        while (l < r && v[l].first <= geshu) {
            geshu += v[l].second;
            res += v[l].second;
            l++;
        }
//        cout << "---: " << l << " " << r << " " << res << " " << geshu << endl;
        if (l >= r)
            break;
        ll need = v[l].first - geshu;
        while (l < r && need > v[r].second) {
            need -= v[r].second;
            res += 2 * v[r].second;
            geshu += v[r].second;
            r--;
        }
//        cout << "---: " << l << " " << r << " " << res << " " << geshu << endl;
        if (l >= r)
            break;
        v[r].second -= need;
        res += 2 * need;
        geshu += need;
//        cout << "---: " << l << " " << r << " " << res << " " << geshu << endl;
    }
    if (geshu >= v[l].first)
        cout << res + v[l].second << endl;
    else if (geshu + v[l].second > v[l].first)
        cout << res + (geshu + v[l].second - v[l].first) + 2 * (v[l].first - geshu) << endl;
    else
        cout << res + 2 * (v[l].second) << endl;







    return 0;
}




