#include<bits/stdc++.h>
using namespace std;

#pragma GCC target ("avx2")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#define debug(x)			\
	for(auto element:x)     \
		cout<<element<<" "; \
	cout<<endl
#define debugp(x)          \
for (auto element : x)     \
	cout << element.first << " " << element.second << endl
#define db(x) cout << #x << " = " << x << '\n'
#define makegraph(adj, num)   \
for (int i = 0; i < num; i++) \
{                             \
	int u, v;                 \
	cin >> u >> v;            \
	adj[u].push_back(v);      \
	adj[v].push_back(u);      \
}
#define rep(i,k,n) for(int i=k;i<n;i++)
#define rept(i,k,n) for(auto i=k;i!=n;++i)
#define drep(i,k,n) for(int i=k;i>=n;i--)
#define pb push_back
#define mp make_pair
#define all(a) a.begin(),a.end()
#define endl '\n'
#define ld long double
#define int long long
#define clr(s) memset(s,0,sizeof(s))
#define valset(arr,size,val) rep(i,0,size) arr[i]=val;
#define Tj return
#define out 0
#ifdef segtree
	#define left st,(st+en)/2,2*ind+1,seg
	#define right (st+en)/2+1,en,2*ind+2,seg
	#define lidx 2*ind+1
	#define ridx 2*ind+2
	#define mid (st+en)/2
#endif // segtree
typedef vector<int> vi;
typedef vector<pair<int,int>> vii;
typedef pair<int,int> ii;
typedef pair<int,pair<int,int>> iii;
typedef map<int,int> mii;
typedef map<string,int> msi;
typedef map<char,int> mci;
typedef set<int> si;
const int intmax = 2147483647;
const int intmin = -2147483648;
const int mod = 1000000007;
const int mod2 = 998244353;

template<typename... T>
void read(T&... args)
{
	((cin >> args ), ...);
}

template<typename... T>
void print(T... args)
{
	((cout << args << " "), ...);
	cout<<endl;
}


void solve()
{
	int n;
	cin>>n;
	vii v(n);
	rep(i,0,n){
		cin>>v[i].second>>v[i].first;
	}
	sort(all(v), greater<ii>());
	int j=0,i=n-1;
	int items = 0, ans = 0;
	while(i>=j){
		// db(i);
		// db(j);
		if(i==j){
			// db(i);
			// db(v[i].first);
			// db(items);
			// db(v[i].second);
			// db(ans);
			if(v[i].first > items){
				if(v[i].first-items >= v[i].second){
					ans += 2*v[i].second;
				}
				else{
					ans += 2*(v[i].first-items);
					ans += v[i].second + items - v[i].first;
				}
			}
			else ans += v[i].second;
			break;
		}
		else if(v[i].first <= items){
			// db(i);
			// db(v[i].first);
			// db(items);
			items += v[i].second;
			ans += v[i].second;
			i--;
		}
		else if(v[j].second > v[i].first - items){
			// db(i);
			// db(j);
			// db(v[j].second);
			// db(v[i].first - items);
			ans += 2*(v[i].first - items);
			v[j].second -= v[i].first - items;
			items += v[i].second + v[i].first - items;
			ans += v[i].second;
			if(v[j].second==0) j++;
			i--;
		}
		else{
			// db(j);
			// db(v[j].second);
			ans += 2*v[j].second;
			items += v[j].second;
			v[j].second = 0;
			j++;
		}
		// db(ans);
	}
	print(ans);
}

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int t;
	t=1;
	while(t--)
		solve();
	Tj out;
}