#include<bits/stdc++.h>

using namespace std;

#define int long long

const int mod = 1000000007;

long long binpow(long long a, long long b, long long m) {
	a %= m;
	long long res = 1;
	while (b > 0) {
		if (b & 1)
			res = res * a % m;
		a = a * a % m;
		b >>= 1;
	}
	return res;
}

int ceil(int x, int y) {
	if (x == 0) return 0;
	int q = 1 + ((x - 1) / y);
	return q;
}

bool isSquare(int x) {
	int y = sqrt(x);
	return y * y == x;
}

void solve() {
	int n;
	cin >> n;
	vector<pair<int, int>> a(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i].second;
		cin >> a[i].first;
	}
	sort(a.begin(), a.end());
	reverse(a.begin(), a.end());
	// for (auto i : a) cout << i.first << " " << i.second << endl;
	// int i = 0, j = n - 1;
	// int price = 0, counts = 0;
	// while (i < j) {
	// 	cout << "counts " << counts << endl;
	// 	cout << " price " << price << endl;
	// 	cout << "a[i] " << a[i].first << " " << a[i].second << endl;
	// 	cout << "a[j] " << a[j].first << " " << a[j].second << endl;

	// 	if (a[i].second >= a[j].first - counts) {
	// 		price += 2 * (a[j].first - counts);
	// 		a[i].second -= (a[j].first - counts);
	// 		counts += (a[j].first - counts);
	// 		price += a[j].second;
	// 		counts += a[j].second;
	// 		a[j].second = 0;
	// 		j--;
	// 		if (a[i].second == 0) i++;
	// 	}
	// 	else {
	// 		price += 2 * a[i].second;
	// 		counts += a[i].second;
	// 		i++;
	// 	}
	// }
	// cout << price << endl;
	// cout << i << " " << a[i].first << " " << a[i].second << endl;
	// cout << "counts " << counts << endl;
	// if (a[i].second > 0) {
	// 	if (counts + a[i].second <= a[i].first) {
	// 		price += 2 * a[i].second;
	// 		counts += a[i].second;
	// 		a[i].second = 0;
	// 	}
	// 	else if (counts >= a[i].first) {
	// 		price += a[i].second;
	// 		counts += a[i].second;
	// 		a[i].second = 0;
	// 	}
	// 	else {
	// 		int to_inc = a[i].first - counts;
	// 		price += 2 * to_inc;
	// 		counts += to_inc;
	// 		a[i].second = a[i].second - to_inc;
	// 		price += a[i].second;
	// 		counts += a[i].second;
	// 		a[i].second = 0;
	// 	}
	// }
	// cout << price << endl;
	int i = 0, j = n - 1;
	int price = 0, counts = 0, req = a[j].first;
	while (i < j) {
		if (a[i].second + counts < req) {
			price += 2 * a[i].second;
			counts += a[i].second;
			a[i].second = 0;
			i++;
		}
		else if (counts >= req) {
			price += a[j].second;
			counts += a[j].second;
			a[j].second = 0;
			j--;
			req = a[j].first;
		}
		else if (a[i].second + counts >= req) {
			price += 2 * (req - counts);
			a[i].second -= req - counts;
			counts += req - counts;
			price += a[j].second;
			counts += a[j].second;
			a[j].second = 0;
			j--;
			req = a[j].first;
			if (a[i].second == 0) i++;
		}
	}
	// cout << "price " << price << endl;
	// cout << "counts " << counts << endl;
	if (a[i].second > 0) {
		if (counts + a[i].second <= a[i].first) {
			price += 2 * a[i].second;
			counts += a[i].second;
			a[i].second = 0;
		}
		else if (counts >= a[i].first) {
			price += a[i].second;
			counts += a[i].second;
			a[i].second = 0;
		}
		else {
			int to_inc = a[i].first - counts;
			price += 2 * to_inc;
			counts += to_inc;
			a[i].second -= to_inc;
			price += a[i].second;
			counts += a[i].second;
			a[i].second = 0;
		}
	}
	cout << price << endl;
}

signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	// int t;
	// cin >> t;
	// while (t--) {
	solve();
	// }

}