#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define x first
#define y second
#define ii pair<ll,ll>
const int M=2e5+7;
int n;
ii a[M];
int main() {
	cin>>n;
	ll sum=0;
	for(int i=1; i<=n; i++) {
		cin>>a[i].y>>a[i].x;
		sum+=a[i].y;
	}
	ll ans=0;
	sort(a+1,a+n+1);
	for(int i=n; i>=1; i--) {
		ll val=min(max((ll)0,(sum-a[i].x)),a[i].y);
		ans+=val+(a[i].y-val)*(ll)2;
		sum-=val;
	}
	cout<<ans<<endl;
}
