// Bhagya Kamal Jain

#include<bits/stdc++.h>

#define ll          long long
#define pb          push_back
#define endl        '\n'
#define mii         map<ll int,ll int>
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define hell        1000000007
#define sim         template < class c
#define ris         return * this
#define dor         > debug & operator <<
#define eni(x)      sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {

using namespace std;

sim > struct rge {c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cout << *x, 0);
sim > char dud(...);
struct debug 
{
	~debug() 
	{ 
		cout<<endl; 
	}
	eni(!=) cout << boolalpha << i; ris; }
	eni(==) ris << range(begin(i), end(i)); }
	sim, class b dor(pair < b, c > d) 
	{
		ris << "(" << d.first << ", " << d.second << ")";
	}
	sim dor(rge<c> d) 
	{
	  	*this << "[";
	  	for (auto it = d.b; it != d.e; ++it)
	    	*this << ", " + 2 * (it == d.b) << *it;
	  	ris << "]";
	}
};
#define fuck(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#define N  100005

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int TESTS=1;
	// cin>>TESTS;
	while(TESTS--)
	{
		ll int n;
		cin >> n;
		vector<pii> v(n);
		for(ll int i = 0; i < n; i++)
		{
			cin >> v[i].S >> v[i].F;
		}
		sort(all(v));
		ll int arr[n + 1], brr[n + 1];
		for(ll int i = 0; i < n; i++)
		{
			brr[i + 1] = v[i].F;
			arr[i + 1] = v[i].S;
		}
		ll int cost = 0;
		ll int num = 0;
		ll int it = n;
		for(ll int i = 1; i <= n; i++)
		{
			if(arr[i] == 0)
			{
				break;
			}
			while(num < brr[i] && it > i)
			{
				ll int tmp = min(arr[it], brr[i] - num);
				// cout << "-- " << it << " " << tmp << endl;
				num += tmp;
				arr[it] -= tmp;
				cost += 2 * tmp;
				if(arr[it] == 0) it--;
			}
			if(num >= brr[i])
			{
				// cout << "a " << i << endl;
				cost += arr[i];
				num += arr[i];
			}
			else
			{
				// cout << "b " << i << endl;
				ll int tmp = min(brr[i] - num, arr[i]);
				arr[i] -= tmp;
				cost += 2 * tmp;
				num += tmp;
				cost += arr[i];
				num += arr[i];
			}
			// cout << "## " << i << " " << cost << " " << num << endl;
		}
		cout << cost << endl;
	}
	return 0;
}