#include<bits/stdc++.h>
using namespace std;
// kya socha tha phir nahi aayege .le aa gaye tera baap ban kar.
#define int long long int
#define endl "\n"
const int mod=1000000007;
mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
const int M=2000006;
int f[M],inv[M];
int expo(int base,int exponent,int mod)
{
	int ans=1;
	while(exponent!=0)
	{
		if(exponent&1)
		{
			ans=(1LL*ans*base)%mod;
		}
		base=(1LL*base*base)%mod;
		exponent>>=1;
	}
	return ans%mod;
}/*
void solve()
{
    int n,x,t;
    cin>>n>>x>>t;
   int z=min(n-1,t/x);
    if(z==0){
        cout<<0<<endl;
        return;
    }
    int y=max(0ll,z*(z-1)/2)+z*(n-z);
    cout<<y<<endl;
}
*/
void compute()
{
	f[0]=1;
	for(int i=1;i<M;i++)
	{
		f[i]=(1LL*i*f[i-1])%mod;
		
	}
	inv[M-1]=expo(f[M-1],mod-2,mod);
	for(int i=M-2;i>=0;i--)
	{
		inv[i]=(1LL*(i+1)*inv[i+1])%mod;
	}
	
}
int C(int n,int r)
{
	return(1LL*((1LL*f[n]*inv[r])%mod)*inv[n-r])%mod;

}
/*
void solve()
{
	int n,q;
	cin>>n>>q;
	string s;
	cin>>s;
	map<char,int>bmap;
	set<char>s1;
	int cnt=1;
	for(int i=0;i<s.size();i++)
	{
		if(s1.find(s[i])==s1.end())
		{
			int a=s[i];
			bmap[s[i]]=a-96;
			s1.insert(s[i]);
		}
		else
		{
			continue;
		}
		
		
	}
	int prefix[s.size()+1];
	prefix[0]=0;
	for(int i=1;i<=s.size();i++)
	{
		prefix[i]=prefix[i-1]+bmap[s[i-1]];
		
	}
	while(q--)
	{
		int l,r;
		cin>>l>>r;
	    cout<<prefix[r]-prefix[l-1]<<endl;
	}
	return;
}
*/
/*
void solve()
{
	int n,x,k;
	cin>>n>>x>>k;
	vector<int>a(n);
	vector<int>v;
	int noofbarrier=0;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	if(n==1)
	{
		cout<<noofbarrier+1<<endl;
		return;
	}
	sort(a.begin(),a.end());
	for(int i=1;i<n;i++)
	{
		int diff=a[i]-a[i-1];
		if(diff>k)
		{
			int val;
		  
	   if(diff%k==0)
	   {
	   	val=diff/k-1;
	   }
	   else
	   {
	   	val=diff/k;
	   }
	   v.push_back(val);
	   noofbarrier+=1;
			
		}
	}
	sort(v.begin(),v.end());
	for(auto x1:v)
	{
		int val=x1;
		if(val>x)
		{
			break;
			
		}
		else
		{
			if(x-val>=0)
			{
				x-=val;
				noofbarrier-=1;
			}
		}
		
	}
	cout<<noofbarrier+1<<endl;
	return;
}
*/
void solve()
{
	int n;
	cin>>n;
	vector<pair<int,int>>v(n);
	for(int i=0;i<n;i++)
	{
		cin>>v[i].second>>v[i].first;
	}
	sort(v.begin(),v.end());
	int s=0;
	int e=n-1;
	int ans=0;
	int b=0;
	while(s<=e)
	{
		if(v[s].first<=b)
		{
			b+=v[s].second;
			ans+=v[s].second;
			s++;
		}
		else
		{
			int req=min(v[s].first-b,v[e].second);
			b+=req;
			ans+=2*req;
			v[e].second-=req;
			if(v[e].second==0)
			{
				e--;
			}
			
		}
		
	}
	cout<<ans<<endl;
}
 

int32_t main(){
      ios_base::sync_with_stdio(0);
      cin.tie(0);
      // srand(time(0));
      int t;
      t=1;
        // t=1;
      while(t--){
        // cout<<"Case #"<<tt<<": ";
            solve();
            // tt++;
      }
      return 0;
}