#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;

/*
Two pointer approach. Sort the array according to quantity need for discount.
Now, keep a track of how many items you've already bought and current cost.
Keep one pointer 'l' at start, and another pointer 'r' at end.
Consider l first, if already bought > discount quantity needed for this product,
then buy all items of this product for 1 Rubel. If it is not so, then it makes
sense to buy those items which have the highest discount quantity needed. Only buy
as many items needed as required to make alreadBought > l's discount quantity needed.
So in this case, start from right(r) pointer, and keep buying products for 2 Rubel
as long as alreadyBought < discount Quantity Needed for product l. 
*/

int main(){
	ios_base::sync_with_stdio(false);
	ll n;
	cin >> n;
	vector<pair<ll, ll> > a;
	ll quantityNeeded, discountQuantity;
	for(ll i=0; i<n; i++){
		cin >> quantityNeeded >> discountQuantity;
		a.push_back(make_pair(discountQuantity, quantityNeeded));
	}
	sort(a.begin(), a.end());
	ll l = 0, r = n-1;
	ll alreadyBought = 0, cost = 0;
	while(l<=r){
		if(a[l].first <= alreadyBought){
			alreadyBought += a[l].second;
			cost += a[l].second;
			l++;
		} else {
			ll toBuy = a[l].first - alreadyBought;
			while(r>=l && toBuy){
				if(a[r].second <= toBuy){
					toBuy -= a[r].second;
					alreadyBought += a[r].second;
					cost += 2*a[r].second;
					a[r].second = 0;
					r--;
				} else {
					a[r].second -= toBuy;
					alreadyBought += toBuy;
					cost += 2*toBuy;
					toBuy = 0;
				}
			}
		}
	}
	cout << cost << endl;
}