G | Commando War Input: Standard Input Output: Standard Output |
“Waiting for orders we held in the wood, word from thefront never came
By evening the sound of the gunfire was miles away
Ah softly we moved through the shadows, slip awaythrough the trees
Crossing their lines in the mists in the fields on ourhands and our knees
And all that I ever, was able to see
The fire in the air, glowing red, silhouetting thesmoke on the breeze”
Thereis a war and it doesn't look very promising for your country. Now it's time toact. You have a commando squad at your disposal and planning an ambush on animportant enemy camp located nearby. You have N soldiers in your squad.In your master-plan, every single soldier has a unique responsibility and youdon't want any of your soldier to know the plan for other soldiers so thateveryone can focus on his task only. In order to enforce this, you brief everyindividual soldier about his tasks separately and just before sending him tothe battlefield. You know that every single soldier needs a certain amount oftime to execute his job. You also know very clearly how much time you need tobrief every single soldier. Being anxious to finish the total operation as soonas possible, you need to find an order of briefing your soldiers that willminimize the time necessary for all the soldiers to complete their tasks. Youmay assume that, no soldier has a plan that depends on the tasks of hisfellows. In other words, once a soldier begins a task, he can finish it without the necessity of pausing inbetween.
Input
Therewill be multiple test cases in the input file. Every test case starts with aninteger N (1<=N<=1000), denoting the number of soldiers. Each ofthe following N lines describe a soldier with two integers B(1<=B<=10000) & J (1<=J<=10000). B secondsare needed to brief the soldier while completing his job needs J seconds.The end of input will be denoted by a case with N =0 . This case shouldnot be processed.
Output
Foreach test case, print a line in the format, “Case X: Y”, where X is the casenumber & Y is the total number of seconds counted from the start of yourfirst briefing till the completion of all jobs.
Sample Input
3
2 5
3 2
2 1
3
3 3
4 4
5 5
0
Output for Sample Input
Ca se 1: 8
Case 2: 15
贪心,先处理执行时间最长的任务
/*************************************************************************
> File Name: uva11729.cpp
> Author: wjzdmr
> Mail: wjzdmr@gmail.com
> Created Time: 2014年09月22日 星期一 20时35分55秒
************************************************************************/
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <cctype>
#include <map>
#include <set>
#include <bitset>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <limits>
#include <fstream>
using namespace std;
//ios_base::sync_with_stdio(0);
#define mem(A, X) memset(A, X, sizeof A)
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define vi vector<int>
#define all(x) x.begin(), x.end()
#define foreach(e,x) for(__typeof(x.begin()) e=x.begin();e!=x.end();++e)
#define sz(x) (int)((x).size())
#define sl(a) strlen(a)
#define rep(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#define Rep(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define dbg(a) cout << a << endl;
#define fi first
#define se second
typedef long long int64;
int gcd(const int64 &a, const int64 &b) { return b == 0 ? a : gcd(b, a % b); }
int64 int64pow(int64 a, int64 b){ if (b == 0) return 1; int64 t = int64pow(a, b / 2); if (b % 2) return t * t * a; return t * t; }
const int inf = 0x7fffffff;
const double eps = 1e-8;
const double pi = acos(-1.0);
const int MAX_N = 10005;
int n, ans, cas = 0, sum[MAX_N];
struct node {
int b, j;
};
bool cmp(const node &a, const node &b)
{
return a.j > b.j;
}
node p[MAX_N];
void work()
{
mem(sum, 0);
ans = 0;
rep(i, 0, n) {
scanf("%d%d", &p[i].b, &p[i].j);
ans += p[i].b;
}
sort(p, p + n, cmp);
vi v;
rep(i, 0, n) {
int sum = 0;
rep(j, i + 1, n) {
sum += p[j].b;
}
p[i].j -= sum;
if (p[i].j > 0)
v.pb(p[i].j);
}
if (sz(v) > 0) {
sort(all(v));
ans += v[sz(v) - 1];
}
cout << "Case " << ++cas << ": " << ans << endl;
}
int main()
{
while (cin >> n && n) {
work();
}
return 0;
}