链接:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2829
/*
刘汝佳《训练指南》第一章 例题2
UVa 11729 Commando War
贪心 , 用“相邻交换法”证明正确性
------ by shuangde
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define REP(i,n) for(int i=0; i<(n); ++i)
using namespace std;
const int MAXN = 10000;
struct node{
int b, j;
friend bool operator<(const node&a,const node&b){
return a.j > b.j;
}
}arr[MAXN];
int main(){
int n,cas=0;;
while(~scanf("%d",&n) && n){
printf("Case %d: ", ++cas);
REP(i, n) scanf("%d%d",&arr[i].b, &arr[i].j);
sort(arr, arr+n);
int maxx=0, tt=0;
REP(i, n){
tt += arr[i].b;
if(arr[i].j + tt > maxx)
maxx = arr[i].j + tt;
}
printf("%d\n", maxx);
}
return 0;
}