#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define FAST1 ios_base::sync_with_stdio(false);
#define FAST2 cin.tie(NULL);
bool sort_on_second(pair<ll,ll> left, pair<ll,ll> right){
return left.second<right.second;
}
void solve(){
ll n;
cin>>n;
vector<pair<ll,ll>> arr(n);
ll ans=0;
for(ll i=0;i<n;i++)
{
cin>>arr[i].first>>arr[i].second;
ans+=2*arr[i].first;
}
sort(arr.begin(),arr.end(),sort_on_second);
reverse(arr.begin(),arr.end());
ll i=0,j=n-1;
ll cur =0;
while(i<=j){
ll tar=arr[j].second;
if(i==j){
ll can_take=arr[i].first;
ll to_take=max(0ll,arr[i].second-cur);
can_take=max(0ll,can_take-to_take);
ans-=can_take;
break;
}
else if(cur>=tar){
cur+=arr[j].first;
ans-=arr[j].first;
j--;
}
else if(cur+arr[i].first<=arr[j].second){
cur+=arr[i].first;
arr[i].first=0;
i++;
}
else{
ll to_take=arr[j].second-cur;
cur+=to_take;
cur+=arr[j].first;
arr[i].first-=to_take;;
ans-=arr[j].first;
j--;
}
}
cout<<ans<<endl;
}
int main(){
FAST1;
FAST2;
ll t=1;
while(t--){
solve();
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBsbCBsb25nIGxvbmcKI2RlZmluZSBGQVNUMSBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTsKI2RlZmluZSBGQVNUMiBjaW4udGllKE5VTEwpOwoKYm9vbCBzb3J0X29uX3NlY29uZChwYWlyPGxsLGxsPiBsZWZ0LCBwYWlyPGxsLGxsPiByaWdodCl7CiAgICByZXR1cm4gbGVmdC5zZWNvbmQ8cmlnaHQuc2Vjb25kOwp9CnZvaWQgc29sdmUoKXsKICAgIGxsIG47CiAgICBjaW4+Pm47CiAgICB2ZWN0b3I8cGFpcjxsbCxsbD4+IGFycihuKTsKICAgIGxsIGFucz0wOwogICAgZm9yKGxsIGk9MDtpPG47aSsrKQogICAgewogICAgICAgIGNpbj4+YXJyW2ldLmZpcnN0Pj5hcnJbaV0uc2Vjb25kOwogICAgICAgIGFucys9MiphcnJbaV0uZmlyc3Q7CiAgICB9CiAgICBzb3J0KGFyci5iZWdpbigpLGFyci5lbmQoKSxzb3J0X29uX3NlY29uZCk7CiAgICByZXZlcnNlKGFyci5iZWdpbigpLGFyci5lbmQoKSk7CiAgICBsbCBpPTAsaj1uLTE7CiAgICBsbCBjdXIgPTA7CiAgICB3aGlsZShpPD1qKXsKICAgICAgICBsbCB0YXI9YXJyW2pdLnNlY29uZDsKICAgICAgICBpZihpPT1qKXsKICAgICAgICAgICAgbGwgY2FuX3Rha2U9YXJyW2ldLmZpcnN0OwogICAgICAgICAgICBsbCB0b190YWtlPW1heCgwbGwsYXJyW2ldLnNlY29uZC1jdXIpOwogICAgICAgICAgICBjYW5fdGFrZT1tYXgoMGxsLGNhbl90YWtlLXRvX3Rha2UpOwogICAgICAgICAgICBhbnMtPWNhbl90YWtlOwogICAgICAgICAgICBicmVhazsKICAgICAgICB9CiAgICAgICAgZWxzZSBpZihjdXI+PXRhcil7CiAgICAgICAgICAgIGN1cis9YXJyW2pdLmZpcnN0OwogICAgICAgICAgICBhbnMtPWFycltqXS5maXJzdDsKICAgICAgICAgICAgai0tOwogICAgICAgIH0KICAgICAgICBlbHNlIGlmKGN1cithcnJbaV0uZmlyc3Q8PWFycltqXS5zZWNvbmQpewogICAgICAgICAgICBjdXIrPWFycltpXS5maXJzdDsKICAgICAgICAgICAgYXJyW2ldLmZpcnN0PTA7CiAgICAgICAgICAgIGkrKzsKICAgICAgICB9CiAgICAgICAgZWxzZXsKICAgICAgICAgICAgbGwgdG9fdGFrZT1hcnJbal0uc2Vjb25kLWN1cjsKICAgICAgICAgICAgY3VyKz10b190YWtlOwogICAgICAgICAgICBjdXIrPWFycltqXS5maXJzdDsKICAgICAgICAgICAgYXJyW2ldLmZpcnN0LT10b190YWtlOzsKICAgICAgICAgICAgYW5zLT1hcnJbal0uZmlyc3Q7CiAgICAgICAgICAgIGotLTsKICAgICAgICB9CiAgICB9CiAgICBjb3V0PDxhbnM8PGVuZGw7Cgp9CgppbnQgbWFpbigpewogICAgRkFTVDE7CiAgICBGQVNUMjsKICAgIGxsIHQ9MTsKICAgIHdoaWxlKHQtLSl7CiAgICAgICAgc29sdmUoKTsKICAgIH0KfQo=