using namespace std;
#include <bits/stdc++.h>
typedef pair<long long,long long> pll;
long long solve(long long n,vector<pll>& a,long long s)
{
    sort(a.begin(),a.end());
    /*vector<pll> b;
    long long i = 0;
    while(i < n)
    {
        long long j = i;
        long long temp = 0;
        while(j < n && a[j].first == a[i].first)
            temp += a[j].second;
        b.push_back(make_pair(a[i].first,))
    }*/
    long long res = 0;
    long long items = 0;
    for(int i = 0;i<n;i++)
    {
        if(items >= s)
            break;
        pll temp = a[i];
        long long sell = temp.first - items;
        if(sell < 0)
            sell = 0;
        //long long sell = max(0,temp.first-items);
        if(sell + items > s)
            sell = s - items;
        res += (2*sell);
        items += sell;
        sell = temp.second;
        if(sell + items > s)
            sell = s - items;
        res += (sell);
        items += sell;
        /*if(temp.first <= items)
        {
            res += temp.second;
            items += temp.second;
        }
        else
        {
            if(items + temp.second <= temp.first)
            {
                items += temp.second;
                res += (2*temp.second);
            }
            else
            {
                res = res + (2*(temp.first-items));
                res = res + (temp.second+items-temp.first);
                items += temp.second;
            }
        }*/
    }
    return res;
}

int main()
{
       long long n;
        cin >> n;
        vector<pll> a;
    long long s = 0;
        for(int j = 0;j<n;j++)
        {
            long long x,y;
            cin >> x;
            cin >> y;
            s += x;
            a.push_back(make_pair(y,x));
        }
        cout << solve(n,a,s);
}