Cabbage
Time limit1sMemory limit256 MB
Each child eats only one cabbage variety; given initial stocks, per-variety prices, and a shared budget, find the largest equal portion each child can receive.
- Level
Medium6 of 10
- Topics
- Binary search, Greedy, Math, Implementation
- Solved
- No attempts yet
Problem
The <> Children of the Volga Plain are known to be very fond of pickled cabbage. However, each of them has a favorite cabbage variety and will not eat any other kind. The preferences seem to be random. Different Children can like either different or the same varieties of cabbage. To make everyone happy, the portions must be the same. Alchen, the chief, wants to make the portions as big as possible.
Initially, Alchen has a certain stock of each cabbage variety and a certain sum of money. He can buy extra cabbage with this money, a different amount of each variety. The prices are known. However, he cannot sell the cabbage he already has.
Help Alchen figure out the best portion sizes for his proteges.
Input
The first line of the input file contains a single integer , the number of test cases (). It is followed by blocks.
The first line of a block contains three integers: , the number of pickled cabbage varieties (), , the number of hungry Children (), and , the sum of money allocated for buying extra pickled cabbage ().
The second line of a block contains integers , where is the number of the pickled cabbage variety preferred by the -th Child of the Volga Plain ().
Each of the following lines contains two integers: , the initially available amount of cabbage of the -th variety, in kilograms (), and , the price of a kilogram of cabbage of this variety ().
The sum of over all test cases is at most , and the sum of over all test cases is at most .
Output
The output file must contain lines, and the -th line must contain the answer to the -th test case. The answer to a test is the maximum possible portion size, in kilograms.
The absolute or relative error of each answer must be at most .