#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define fr first
#define sc second
 
const int M=4e6;
const int mod=1e9+7;
 
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	int n;
	cin>>n;

	vector<pair<int,int>> vt(n);

	for(int i=0;i<n;i++)
	{
		int ai,bi;
		cin>>ai>>bi;

		vt[i].fr=bi;
		vt[i].sc=ai;
	}

	sort(vt.begin(),vt.end(),greater<pair<int,int>>());

	int rubles=0,count=0;

	int i=0,j=n-1;

	while(i<=j)
	{
		if(count>=vt[j].fr)
		{
			rubles+=vt[j].sc;

			count+=vt[j].sc;
			j--;
		}
		else
		{
			int b=min(vt[j].fr-count,vt[i].sc);

			rubles+=2*b;

			count+=b;

			vt[i].sc-=b;

			if(vt[i].sc==0)
				i++;
		}
	}

	cout<<rubles;
	return(0);
}