#include <bits/stdc++.h>
 
using namespace std;
 
#define F first
#define S second
 
#define pb push_back
#define mp make_pair
 
#define ll long long
 
#define pll pair<ll, ll>
#define pil pair<int, ll>
#define pli pair<ll, int>
#define pii pair<int, int>
 
#define end end
#define beg begin
#define all(_) beg(_), end(_)
 
#define smx(y, x) y = max(x, y)
#define smn(y, x) y = min(x, y)
 
void setIO() {
	ios_base::sync_with_stdio(0);
 
	cin.tie(0);
	cout.tie(0);
}
 
const int N = 1e6 + 5;
const int M = 1e5 + 5;
const int L = 2e1 + 1;
 
const ll inf = 9e17;
const ll mod = 1e9 + 7;
 
int xyz = 1; // test cases

ll n, m, d;

ll sum;
ll ans = inf;

pll arr[N];

void tst(ll cur, ll rem) {
	smn(ans, cur + rem * 2);
}

bool cmp(pll a, pll b) {
	return a.S < b.S;
}

void run() {
	cin >> n;

	for (int i = 1; i <= n; i++)
		cin >> arr[i].F >> arr[i].S;
	
	for (int i = 1; i <= n; i++)
		sum += arr[i].F;
	
	tst(0, sum);

	sort(arr + 1, arr + 1 + n, cmp);

	ll cst = 0;
	ll use = 0;
	for (int i = 1, j = n; i <= n; i++) {
		while (arr[i].F && j >= 1) {
			if (use >= arr[i].S) {
				use += arr[i].F;
				cst += arr[i].F;
				arr[i].F = 0;
			}
			else {
				ll dif = min(arr[i].S - use, arr[j].F);
				arr[j].F -= dif;
				use += dif;
				cst += dif * 2;
			}

			if (!arr[j].F) j--;

			tst(cst, sum - use);
		}
		tst(cst, sum - use);
	}

	cout << ans << "\n";
}
 
int main() {
	setIO();
	
	while (xyz--) run();
 
	#ifdef LOCAL
	cin.clear(); cout.flush(); system("pause");
	#endif

	return 0;
}