#pragma GCC optimize ("Ofast")
#pragma GCC optimization("unroll-loops")
#pragma GCC target("avx,avx2,fma")
#include"iostream"
#include"cassert"
#include"cmath"
#include"cstdio"
#include"cstdlib"
#include"map"
#include"set"
#include"queue"
#include"vector"
#include"algorithm"
#include"numeric"
#include"stack"
using namespace std;
typedef long long ll;
#define int ll
#define all(x) x.begin(), x.end()
inline int in(){int x;scanf("%lld",&x);return x;}
int32_t main()
{
	int n,tot=0,have=0,ans=0;cin>>n;
	vector<pair<int,int>> a(n);
	for(auto &[i1,i2]:a){cin>>i2>>i1;tot+=i2;}
	sort(all(a));
	//can take from other piles
	for(int i=0;i<n;i++){
		if(have<a[i].first){
			int take=min(a[i].first-have,tot);
			ans+=take*2;tot-=take;have+=take;
		}
		int take=min(tot,a[i].second);
		tot-=take;ans+=take;have+=take;
	}
	cout<<ans<<"\n";

	
}

