Weights
Time limit3sMemory limit128 MB
Given container capacities and weights whose masses form a divisibility chain, maximize how many weights fit.
- Level
Medium7 of 10
- Topics
- Greedy, Sorting, Math, Prefix sum
- Solved
- No attempts yet
Problem
The Byteotian Institute of Experimental Physics is moving to a new building, and transporting its large collection of precision weights has turned out to be tricky.
The Institute has a number of containers, each with a limited strength (the maximum total mass it can hold). We want to put as many weights as possible into the containers; any weights that do not fit are discarded. A container may hold any number of weights, as long as their total mass does not exceed its strength, and a container may also be left empty.
The weights have a special property: for any two of them, the mass of one is an integer multiple of the mass of the other (in particular, two weights may have equal mass).
Given the strengths of the containers and the masses of the weights, determine the maximum number of weights that can be placed into the containers.
Input
The first line contains two integers and (): the number of containers and the number of weights.
The second line contains integers (): the strengths of the containers, in milligrams.
The third line contains integers (): the masses of the weights, in milligrams. For any two weights, one mass is an integer multiple of the other.
Output
Print a single integer: the maximum number of weights that can be placed into the containers without exceeding any container's strength.