// Problem: D. PriceFixed
// Contest: Codeforces - Codeforces Round #727 (Div. 2)
// URL: https://codeforces.com/problemset/problem/1539/D
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// Problem: D. PriceFixed
// Contest: Codeforces - Codeforces Round #727 (Div. 2)
// URL: https://codeforces.com/contest/1539/problem/D
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//author -> mystic_ankush
#include<bits/stdc++.h>
#define ll long long int
#define pb push_back
#define ff first
#define ss second
#define ii insert
#define mem(l,r) memset(l,r,sizeof(l))
#define sorta(a,n) sort(a+1,a+1+n)
#define sortv(v) sort(v.begin(),v.end())
#define revs(s) reverse(s.begin(),s.end())
#define fastio ios::sync_with_stdio(false), cin.tie(NULL);
const int N=1e5+5;
const int mod=1e9+7;
const ll int_max=1e18;
const ll int_min=-1e18;
#define rep(i,j,k) for(ll i=j;i<=k;i++)
#define repr(i,j,k) for(ll i=j;i>=k;i--)
const long double PI = acos(-1);
using namespace std;
bool cmp(pair<ll,ll>a,pair<ll,ll>b)
{
	return a.ss<b.ss;
}
void solve()
{
 	ll n;
 	cin>>n;
 	vector<pair<ll,ll>>v;
 	rep(i,1,n)
 	{
 		ll x,y;
 		cin>>x>>y;
 		v.pb({x,y});
 	}
 	sort(v.begin(),v.end(),cmp);
 	ll l=0,r=n-1;
 	ll till=0;
 	ll ans=0;
 	while(l<r)
 	{
 		if(v[l].ss<=till)
 		{
 			ans+=(v[l].ff);
 			till+=v[l].ff;
 			l++;
 		}
 		else
 		{
 			ll req=v[l].ss-till;
 			if(req>v[r].ff)
 			{
 				ans+=(2*v[r].ff);
 				till+=v[r].ff;
 				r--;
 			}
 			else
 			{
 				ans+=(req*2);
 				till+=req;
 				till+=v[l].ff;
 				ans+=v[l].ff;
 				l++;	 
 				v[r].ff-=req;
 			}
 		}
 	}
 //	cout<<l<<' '<<r<<' '<<till<<'\n';
 	if(till>=v[l].ss)
 	{
 		ans+=v[l].ff;
 	}
 	else
 	{
 		ll req=v[l].ss-till;
 		if(req<=v[l].ff)
 		{
 			ans+=req*2;
 			ans+=(v[l].ff-req);
 		}
 		else
 		{
 			ans+=v[l].ff*2;
 		}
 	}
 	cout<<ans<<'\n';
}
int main() {
    fastio
    solve();    
}