There is a war and it doesn't look very promising for your country. now it's time to act. you have a commando squad at your disposal and planning an ambush on an important enemy camp located nearby. you have N Soldiers in your squad. in your master-plan, every single soldier has a unique responsibility and you don't want any of your soldier to know the plan for other soldiers so that everyone can focus on his task only. in order to enforce this, you brief every individual soldier about his tasks separately and just before sending him to the battlefield. you know that every single soldier needs a certain amount of time to execute his job. you also know very clearly how much time you need to brief every single soldier. being anxious to finish the total operation as soon as possible, you need to find an order of briefing your soldiers that will minimize the time necessary for all the soldiers to complete their tasks. you may assume that, no soldier has a plan that depends on the tasks of his fellows. in other words, once a soldier begins a task, he can finish it without the necessity of pausing in.
Input
There will be multiple test cases in the input file. Every test case starts with an integerN (1 <= n <= 1000), Denoting the number of soldiers. Each of the following n lines describe a soldier with two integersB (1 <= B <= 10000)&J (1 <= j <= 10000).BSeconds are needed to brief the soldier while completing his job needsJSeconds. The end of input will be denoted by a caseN = 0.This case shoshould not be processed.
Output
For each test case, print a line in the format, "Case X: Y ", where X is the case number & Y is the total number of seconds counted from the start of your first briefing till the completion of all jobs.
Sample input output for sample input
3 2 5 3 2 2 1 3 3 3 4 5 5 0 |
Case 1: 8 Case 2: 15 |
Problem setter: Hammad mahmudur Rahman, special thanks: manzurur Rahman Khan
Link: http://uva.onlinejudge.org/external/117/11729.html
Meaning: N subordinates, each of which requires Bi minutes to explain the task and let the task be completed in the next two minutes. Determine the order to complete the task as early as possible.
Greedy, sort by JI in ascending order, and then solve the problem.
# Include <stdio. h> # Include < String . H> # Include <Iostream> # Include <Algorithm> Using Namespace STD; Const Int Maxn =1010 ; Struct Node { Int B, J;} node [maxn]; Bool CMP (node A, Node B ){ Return A. j> B. J ;} Int Main (){ Int N; Int Icase = 0 ; While (Scanf ( " % D " , & N) = 1 && N) {icase ++ ; For ( Int I = 0 ; I <n; I ++ ) Scanf ( " % D " , & Node [I]. B ,& Node [I]. J); sort (node, node + N, CMP ); Int Ans = 0 ; Int TMP = 0 ; For ( Int I = 0 ; I <n; I ++ ) {TMP + = Node [I]. B; ans = Max (ANS, TMP +Node [I]. J);} printf ( " Case % d: % d \ n " , Icase, ANS );} Return 0 ;}