#include<bits/stdc++.h>
#include<string>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define umap unordered_map
#define lb lower_bound
#define ub upper_bound
#define nl "\n"
const ll M=1e9+7;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cout<<fixed<<setprecision(15);
	ll T=1;
//	cin>>T;
	while(T--)
	{
		ll n;
		cin>>n;
		vector<pair<ll,ll>> v;
		for(int i=0;i<n;i++)
		{
			ll x,y;
			cin>>x>>y;
			v.pb(mp(y,x));
		}
		sort(v.begin(), v.end());
		ll l=0,r=n-1,bought=0,ans=0;
		while(l<=r)
		{
			if(v[r].se==0)
				r--;
			ll z=min(v[r].se,v[l].fi-bought);

			bought+=z;
			ans+=2*z;
			v[r].se-=z;

			while(bought>=v[l].fi && l<=r)
			{
				ans+=v[l].se;
				bought+=v[l].se;
				v[l].se=0;
				l++;
			}
			if(l==r && v[l].se==0) break;
		}
		cout<<ans<<nl;
	}
}