#include <bits/stdc++.h>
#define vec vector
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define ll long long
#define pll pair<ll,ll>
#define fs first
#define sc second
// Multiple test cases or not
// #define BATCH true
using namespace std;
void solve() {
int n; cin>>n;
vec<pll> a(n); for (int i=0;i<n;i++)cin>>a[i].sc>>a[i].fs;
sort(a.begin(), a.end());
ll l=0,r=n-1,ccnt=0,ans=0;
while(l<=r) {
// cout<<"l="<<l<<" r="<<r<<endl;
if (ccnt>=a[l].fs) {
// cout<<"1s cnt="<<a[l].sc<<endl;
ans+=a[l].sc;
ccnt+=a[l].sc;
l++;
} else {
ll df = (a[l].fs-ccnt);
// cout<<"df="<<df<<endl;
if (df < a[r].sc && df>0) {
// cout<<"2s less cnt="<<df<<endl;
a[r].sc-= df;
ans+=2*df;
ccnt+=df;
} else {
// cout<<"2s cnt="<<a[r].sc<<endl;
ans+=2*a[r].sc;
ccnt+=a[r].sc;
r--;
}
}
}
cout<<ans<<endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t=1;
#ifdef BATCH
cin >> t;
#endif
while (t--) solve();
}
1632B - Roof Construction | 388A - Fox and Box Accumulation |
451A - Game With Sticks | 768A - Oath of the Night's Watch |
156C - Cipher | 545D - Queue |
459B - Pashmak and Flowers | 1538A - Stone Game |
1454C - Sequence Transformation | 165B - Burning Midnight Oil |
17A - Noldbach problem | 1350A - Orac and Factors |
1373A - Donut Shops | 26A - Almost Prime |
1656E - Equal Tree Sums | 1656B - Subtract Operation |
1656A - Good Pairs | 1367A - Short Substrings |
87A - Trains | 664A - Complicated GCD |
1635D - Infinite Set | 1462A - Favorite Sequence |
1445B - Elimination | 1656C - Make Equal With Mod |
567A - Lineland Mail | 1553A - Digits Sum |
1359B - New Theatre Square | 766A - Mahmoud and Longest Uncommon Subsequence |
701B - Cells Not Under Attack | 702A - Maximum Increase |