#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define ll long long
void solve(){
	ll n, a, b;
	cin >> n;
	vector<pair<ll, ll>> arr;
	for(int i = 0; i < n; i++){
		cin >> a >> b;
		arr.push_back({b, a});
	}
	sort(arr.begin(), arr.end());
	// for(auto i : arr)
	// 	cout << i.first << " " <<i.second << endl;
	ll start = 0, end = n-1,  ans = 0, buyed = 0;

	// fisrt means minimum to get there.
	// second means to buy that much.
	while(start <= end){
		if(buyed >= arr[start].first){
			buyed += arr[start].second;
			ans += arr[start].second;
			arr[start].second = 0;
			start ++;
		}else{
			ll to_buy = arr[start].first - buyed;
			if(to_buy >= arr[end].second){
				buyed += arr[end].second;
				ans += arr[end].second*2;
				end--;
			}else{
				buyed += to_buy;
				ans += to_buy*2;
				arr[end].second -= to_buy;
			}
		}

	}
	cout << ans << endl;
}
int main(){
	int t = 1;
	// cin >> t;
	while(t--)
		solve();
	return 0;
}