Unequalled Consumption
Time limit1sMemory limit128 MB
Given up to 5 candy weights, find for each query P the smallest total weight whose number of representations (as an unordered multiset count) is at least P, using binary search over weight combined with counting compositions via generating functions or DP.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Binary search, Math, Combinatorics
- Solved
- No attempts yet
Problem
The Association of Candy Makers is preparing to launch a new product. The idea is old with a novel twist: it simply sells boxes of candies. But since people are what they consume and everyone wants to be unique these days, the Association wants every candy box to be unique, in the sense that no two boxes contain the same composition of candy types.
The Association can make only a small number of different candy types, and though limited in imagination, it is virtually limitless in resources, so it can produce as many of each type as it wants. The candy types have weights (some types may weigh the same), and to simplify pricing the Association wants every candy box to have the same total weight.
With these restrictions, the Association can make only a limited number of boxes. For instance, with three candy types weighing , , and grams, different boxes can be made with total weight grams: two of type 1, or two of type 2, or one of type 3, or one each of types 1 and 2. The Association would like to make at least one box for everyone in the cosmos. Given the number of people in the cosmos, find the smallest possible total weight such that different boxes containing exactly grams of candy can be made.
Input
The input contains several data sets (at most ). Each data set is given on four lines:
- The first line contains an integer (), the number of candy types.
- The second line contains integers (), the weight in grams of each type.
- The third line contains an integer (), the number of queries.
- The fourth line contains integers (), the queries.
The input ends with a line containing a single in place of ; that terminating data set must not be processed.
Output
For the -th data set, print a line Set i, then one line per query. For query , print the smallest positive weight (in grams) for which at least different boxes of total weight exactly grams exist. If there is no such weight, print no candy for you for that query instead. When a weight exists, it is at most .