Nested Candy Boxes
InterviewTime limit3sMemory limit1024 MB
Given box counts a_i per nesting level, find the fewest boxes to open to collect at least x candies for each query.
- Level
Medium5 of 10
- Topics
- Greedy, Sorting, Prefix sum, Binary search
- Solved
- No attempts yet
Statement
For New Year, Andrey received a candy box. Inside it he found several identical, smaller boxes, and inside those are even smaller boxes, and so on. Formally, a candy is a box of level , and a box of level contains boxes of level . The box Andrey received has level .
Today friends will visit Andrey, and he wants to share some candies with them. To do so, he has to open some boxes. Andrey cannot open a box that is inside a box he has not opened yet. For several values of , find the minimum number of boxes Andrey must open to get at least candies. Andrey does not know yet how many friends will come, so he asks for answers to several values of .
Input
The first line contains and (), the number of boxes and the number of queries. The second line contains integers (). The third line contains integers (). It is guaranteed that each does not exceed the total number of candies in a level box.
Output
Print integers. The -th number is the minimum number of boxes Andrey must open to get at least candies.
Hint
In the first example, the only candy is hidden under five levels of boxes. In the second example, to get 13 candies Andrey must open the largest box, then two boxes of level 2, and finally five of the six level 1 boxes.