#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
#define OldTomato ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
typedef long long ll;
const int N = 2e5+10;
int n,m,k,T;
struct node{
   ll x,y;
   bool operator<(const node&rhs)const
   {
      return y < rhs.y;
   }
}a[N];
int main(void)
{
//   OldTomato ;
   scanf("%d",&n);
   for(ll i=1;i<=n;++i)
   {
     scanf("%lld%lld",&a[i].x,&a[i].y);
//     cin>>a[i].x>>a[i].y;
   }
   sort(a+1,a+1+n);
   ll i=1,j = n;
   ll ans = 0;
   ll now = 0; //��ǰ�������Ʒ����. 
   while(i!=j)
   {
      if(a[i].y <= now)
      {
         now += a[i].x;
         ans += a[i].x;
         a[i].x = 0;
	  }
	  else 
	  {
	     ll tmp = min(a[j].x,a[i].y - now); //����
		 now += tmp;
		 ans += tmp * 2;
		 a[j].x -= tmp;  
	  }
	  if(a[i].x==0) i ++ ;
	  if(a[j].x==0) j -- ;
   }
   ll tmp =  min(a[i].x,max(0ll,a[i].y - now));
   ans += 2 * tmp;
   ans += a[i].x - tmp;
   printf("%lld\n",ans);
//   cout<<ans<<endl;	
   return 0;
}
