http://icpc.upc.edu.cn/problem.php?cid=1424&pid=8
#include<cstdio>
#include<algorithm>
using namespace std;
struct str
{
int l,r;
bool operator<(const str &str1)const
{
if (l<r&&str1.l>=str1.r)
return true;
if (l<r&&str1.l<=str1.r)
return l<str1.l;
if (l>=r&&str1.l>=str1.r)
return r>str1.r;
return false;
}
} a[10005];
int n,i;
int main()
{
while(scanf("%d",&n))
{
if (n==0)
break;
for(i=0; i<n; i++)
scanf("%d%d",&a[i].l,&a[i].r);
sort(a,a+n);
int t1=0,t2=0;
for(i=0; i<n; i++)
{
t1 += a[i].l;
t2 = max(t1,t2) + a[i].r;
}
printf("%d\n",t2);
}
}