Do it!
Time limit1sMemory limit128 MB
Choose when to shout 'do it!' over time so that positive, negative, and neutral workers finish their 100-unit fixtures with the smallest total finish time.
- Level
Medium7 of 10
- Topics
- Greedy, Math, Brute force, Implementation
- Solved
- No attempts yet
Problem
You are the boss of a small lighting-fixture company with employees. Whenever you want something done, you have taken up the habit of shouting "do it!" over the company intercom. Of your employees, respond positively to your "do it!", respond negatively, and are unaffected.
At time , every employee starts building their own lighting fixture. Each fixture requires units of labor to finish. Normally, each employee contributes units of labor per unit of time (or the amount of labor remaining, whichever is smaller), so an employee normally takes units of time to finish a fixture.
During any unit of time, however, if you shout "do it!" over the intercom, then during that unit of time an employee who responds positively does units of labor, while one who responds negatively does units of labor. An unaffected employee always does units of labor.
Each employee works only on their own fixture, and you may shout "do it!" at most once per unit of time. Your goal is to plan a sequence of "do it!"s so that the sum, over all fixtures, of the times needed to finish them is minimized.
Input
The input consists of several test cases. Each test case is a single line containing four integers , , , and ( and ). The end of input is marked by a line with , which must not be processed.
Output
For each test case, print on its own line the minimum possible sum of the times needed to finish all fixtures.
Hint
In the first example , one optimal strategy is to shout "do it!" during each of the first units of time. The positively-responding employees then contribute units of labor per unit of time and finish in units of time. The negatively-responding employee contributes unit of labor per unit of time for the first units of time and afterwards, finishing at . The unaffected employee always contributes units of labor and finishes at . This gives a total of .
In the second example , an optimal strategy is to never shout "do it!". All four employees then finish at , for a total of .