#include<bits/stdc++.h>
#define ll long long
#define L(i,j,k) for(int i=(j);i<=(k);i++)
#define R(i,j,k) for(int i=(j);i>=(k);i--)
#define inf 0x3f3f3f3f3f3f3f3f
const ll N=1e6,maxn=1e3,mod=1000000007;
using namespace std;

ll i,j,m,n,x,y,z,l,r,t,k,p,pp,num,sum[N*4],pos,key,cnt,flag,minn,maxx,ans;
ll a[N],b[N],c[N],mapp[maxn][maxn],vis[N],pre[N],fac[N],dp[maxn][maxn];
char cc[N],zz;
double xa[5],ya[5],xb[5],yb[5],ee,ff;
string s,d=" abcdefghijklmnopqrstuvwxyz",g;

map<string,ll>mp;
vector<ll>v[N],vt;
struct qq{ll xx,yy,zz,id;}q[N];
//priority_queue<ll,vector<ll>,greater<ll> >s;

bool cmp(qq u,qq v){
	return u.yy<v.yy;
}
bool cmp1(ll u,ll v){return u>v;}

int main(){
	scanf("%lld",&n);
	L(i,0,n-1)scanf("%lld%lld",&q[i].xx,&q[i].yy);
	sort(q,q+n,cmp);
	j=n-1;num=0;ans=0;
	L(i,0,n-1){
		if(q[i].xx==0)continue;
		if(num>=q[i].yy){
			num+=q[i].xx,ans+=q[i].xx;q[i].xx=0;
		}
		else{
			m=q[i].yy-num;
			while(m>0&&j>=0){
				if(q[j].xx>=m)p=m;
				else p=q[j].xx;
				m-=p;q[j].xx-=p;
				j--;
				num+=p;ans+=p*2;
			}
			if(q[j+1].xx!=0)j++;
			num+=q[i].xx,ans+=q[i].xx;q[i].xx=0;
		}
//		printf("%d %lld %lld %lld\n",i,j,num,ans);
//		for(k=0;k<n;k++)printf("%lld ",q[k].xx);cout<<endl;
	}
	printf("%lld\n",ans);
}