Packing
Time limit1sMemory limit256 MB
Buy the fewest backpacks from the shop so all items fit without splitting items or exceeding capacities.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Bit manipulation
- Solved
- No attempts yet
Problem
Camping season is here. A trip needs all sorts of gear, and you carry that gear yourself, so deciding what you really need matters. The gear is already picked out, so the only thing left is to pack it into backpacks.
One backpack takes any number of items, as long as their total weight does not exceed its capacity. Items cannot be split, so part of the capacity you buy may go unused.
The catch is that you own no backpacks yet and have to buy them. The shop sells backpacks of several capacities, and they all cost the same. Buy enough backpacks to hold every item while spending as little as possible.
Input
The first line contains the number of items to pack, , and the number of backpacks in the shop, . (, )
The second line contains integers . The value is the weight of the -th item. ()
The third line contains integers . The value is the capacity of the -th backpack. ()
Output
Print on the first line the minimum number of backpacks that hold every item. If packing every item is impossible, print the word NIE instead.
Hint
In the first example you can buy the first backpack and the third backpack. The heaviest item goes into the backpack of capacity 11, and the remaining items go into the backpack of capacity 9.