Floodgates
Time limit1sMemory limit128 MB
Each gate drains Fi per hour at fixed cost Ci when opened. For each query (V, T), find the minimum total cost whose combined capacity Fi*T covers V.
- Level
Medium5 of 10
- Topics
- Brute force, Greedy, Sorting, Dynamic programming
- Solved
- No attempts yet
Problem
A dam has floodgates. Each floodgate has its own flow rate and channel, and opening it can cause flood damage to the downstream area.
Opening floodgate drains m³ of water per hour and incurs a damage cost of . This damage cost is a fixed cost that occurs whenever the gate is opened at all, regardless of how long it stays open.
The dam holds m³ of water, and all of it must be drained within hours. Write a program that finds how to open the floodgates so that the total damage cost is minimized.
Each floodgate operates independently, opening time is measured in whole hours, and a gate can be kept open for at most hours. Therefore, opening gate can drain at most m³ of water.
For example, suppose there are 4 floodgates whose flow rates and damage costs are as follows.
To drain 5000000 m³ within 7 hours, opening only drains m³, which is enough, at a cost of 120000.
To drain 5000000 m³ within 30 hours, opening and together drains 180000 m³ per hour, which is more than enough within 30 hours, at a cost of .
Input
The first line contains the number of floodgates . ()
Each of the next lines contains the flow rate (m³/hour) and the damage cost of gate , separated by a space.
The next line contains the number of queries . ()
Each of the following lines contains and for one query, meaning that the stored m³ of water must all be drained within hours.
(, )
Output
For each query, print one line in the format Case k: X, where is the query number starting from 1 and is the minimum damage cost. If the m³ of water cannot be drained within hours, print IMPOSSIBLE instead of .