The Brick Stops Here
Time limit1sMemory limit128 MB
Given N brick types with copper content and price, answer C queries: pick exactly M distinct types whose copper sum lies in [M*Cmin, M*Cmax] at minimum total price.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Sorting, Greedy, Array
- Solved
- No attempts yet
Problem
A factory manufactures brass bricks. Brass is an alloy of copper and zinc, and every brick weighs exactly grams, of which the copper content may be anywhere from to grams. The factory produces different types of brick; each type has its own copper content and its own price, and all types are listed in a catalog.
A client wants to buy exactly bricks, and they must all be of different types. The chosen bricks are melted together, and the resulting mixture must contain at least and at most grams of copper per kilogram. Because every brick weighs one kilogram, melting bricks yields kilograms whose copper content is the sum of the chosen copper values; the requirement is therefore
Among all selections that satisfy this requirement, the client wants the one with the smallest total price. For each client, determine that minimum total price. The values , , and differ from client to client.
Input
The first line contains an integer (), the number of brick types. Each of the next lines contains two integers: the copper content ( to grams) and the price (in cents) of that brick type. No brick costs more than cents.
The next line contains an integer (), the number of clients. Each of the following lines contains three integers , , and (, , ), describing one client's request.
All input numbers are positive integers.
Output
For each client, print on its own line the minimum possible total price (in cents) for a valid selection of distinct brick types. If no selection meets the client's requirements, print impossible for that client.