#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ll long long
#define maxn 100005
using namespace std;
int n;
struct node{
	ll a,b;
}s[maxn];
bool cmp(node A,node B){
	return A.b>B.b;
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%lld%lld",&s[i].a,&s[i].b);
	sort(s+1,s+n+1,cmp);
	int i=1,j=n;ll num=0,ans=0;
	while(i<=n&&i<=j){
		while(j>=i){
			if(s[j].b>num)break;
			else num+=s[j].a,ans+=s[j].a,s[j].a=0,j--;
		}
		if(j<i)break;
		ll t=min(s[j].b-num,s[i].a);
		s[i].a-=t;num+=t;ans+=2*t;
		if(s[i].a==0)i++;
	}
	cout<<ans<<endl;
	return 0;
}

