Exact Measurement
Time limit1sMemory limit128 MB
Each box offers up to q_i masses of weight 10^k_i; find the fewest boxes to open so the chosen masses sum to exactly x.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Greedy, Math, Bit manipulation
- Solved
- No attempts yet
Problem
Peter works in a chemistry laboratory. For a new experiment he must measure out exactly nanograms (ng) of a reagent. He has a balance and a collection of standard masses.
The masses are stored in sealed boxes. Box holds identical masses, each weighing ng. To take masses out of a box he must open it, and from an opened box he may take any number of its masses, from up to .
Peter wants the total weight of the masses he takes to be exactly ng while opening as few boxes as possible. Determine the minimum number of boxes he must open.
Input
The first line contains two integers and (, ).
Each of the next lines contains two integers and describing one box (, ).
Output
Print a single integer: the minimum number of boxes Peter must open to measure exactly ng. If it is impossible to measure exactly ng, print .