// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <cmath>
#include <iomanip>
#define dibs reserve
#define OVER9000 1234567890
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define tisic 47
#define soclose 1e-6
#define chocolate win
// so much chocolate
#define patkan 9
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):x)
#define uint unsigned int
using namespace std;
// mylittledoge
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
vector< pair<int,int> > A(N);
int W =0;
for(int i =0; i < N; i++) {
cin >> A[i].ss >> A[i].ff;
W +=A[i].ss;
A[i].ff +=A[i].ss;}
sort(A.begin(),A.end());
int ansA =-1000000000-tisic, ansB =W;
while(ansB-ansA > 1) {
int K =(ansA+ansB)/2;
bool ok =true;
int a =N-1, w =W;
priority_queue<int> q;
for(int i =N-1; i >= 0; i--) {
while(a >= 0 && w-K <= A[a].ff) {
q.push(A[a].ss);
a--;}
if(q.empty()) {ok =false; break;}
w -=q.top();
q.pop();}
if(ok) ansB =K;
else ansA =K;}
cout << ansB << "\n";
return 0;}
// look at my code
// my code is amazing
Ly8gaW9zdHJlYW0gaXMgdG9vIG1haW5zdHJlYW0KI2luY2x1ZGUgPGNzdGRpbz4KLy8gYml0Y2ggcGxlYXNlCiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPGNzdGRsaWI+CiNpbmNsdWRlIDx2ZWN0b3I+CiNpbmNsdWRlIDxzZXQ+CiNpbmNsdWRlIDxtYXA+CiNpbmNsdWRlIDxxdWV1ZT4KI2luY2x1ZGUgPHN0YWNrPgojaW5jbHVkZSA8bGlzdD4KI2luY2x1ZGUgPGNtYXRoPgojaW5jbHVkZSA8aW9tYW5pcD4KI2RlZmluZSBkaWJzIHJlc2Vydm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