#include<bits/stdc++.h>
using namespace std;
int n,peo_wait,ans;
struct node{
int d,u;
}cow[25001];
inline int cmp(node x,node y){return x.u<y.u;}
int main(){
scanf("%d",&n);
for(register int i=1;i<=n;i++){
scanf("%d%d",&cow[i].u,&cow[i].d);
}
sort(cow+1,cow+n+1,cmp);
for(register int i=1;i<=n;i++){
peo_wait+=cow[i].u;
ans+=cow[i].d;
}
if(peo_wait>=ans-cow[n].d)peo_wait-=(ans-cow[n].d);
else peo_wait=cow[1].u;
printf("%d\n",ans+peo_wait);
return 0;
}