#include<bits/stdc++.h>
#include<cmath>
#include<vector>
//#include<pair>
#define ll long long int
using namespace std;
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n,a,b;
    cin>>n;
    ll required=0;
    vector<pair<ll,ll>>h(n);
    for(int i=0;i<n;i++){
        cin>>a>>b;
        required+=a;
        h.push_back(make_pair(b,a));
    }
    sort(h.rbegin(),h.rend());
    ll get=0;
    ll end=n-1;
    ll start=0;
    ll sum=0;
    while(required!=get)
    {
        
        if (h[end].first <=get)
        {
            sum += h[end].second;
            get+= h[end].second;
            h[end].second = 0;
            end--;
            continue;
        }
        ll k = min(h[start].second,h[end].first-get);
        h[start].second-=k;
        get+= k;
        sum+=2*k;
        if (h[start].second==0) start++;
    }
    cout<<sum<<endl;
    return 0;
}  
      	 		 				  						 	  	 	
