#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <cassert>
#include <numeric>
#include <cstring>
#include <tuple>
#include <stack>
#include <deque>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <sstream>

using namespace std;

#define ll long long

ll MOD = 1e9 + 7;

void test_case() {
	int n; cin >> n;

	vector<pair<ll, ll>> items(n); // bar, need
	vector<ll> have(n, 0);
	ll ans = 0;
	ll total_have = 0;
	for (auto& pp : items) cin >> pp.second >> pp.first;

	sort(items.begin(), items.end());
	int r = n - 1; // reverse iter

	for (int i = 0; i < n; ++i) {
		if (have[i] == items[i].second) // we already have this item
			break;

		while (r >= 0 && have[i] < items[i].second && total_have < items[i].first) { // buy from the back
			ll buying = min(items[r].second - have[r], items[i].first - total_have);
			ans += buying * 2;
			total_have += buying;
			have[r] += buying;
			if (have[r] == items[r].second)
				r--;
		}

		if (have[i] == items[i].second) // we already have this item
			break;

		ll buying = items[i].second - have[i];
		ans += buying;
		total_have += buying;
		have[i] += buying;
	}

	cout << ans << endl;
}

int main()
{
	//ll t;
	//cin >> t;

	//while (t--) {
		test_case();
	//}
}

