#K93752. Minimum Cost of Rod Cuts
Minimum Cost of Rod Cuts
Minimum Cost of Rod Cuts
You are given n rods with various lengths. Your task is to determine the minimum number of cuts required to produce a rod (or multiple pieces from a single rod) of exactly length m.
If a rod's length is exactly m, then no cuts are needed. Otherwise, if a rod of length \(L\) is greater than m and can be evenly divided into pieces of length m (i.e. \(L \bmod m = 0\)), you can cut it into \(\frac{L}{m}\) pieces. In this case, the number of cuts needed is given by the formula:
\[
\text{cuts} = \frac{L}{m} - 1
\]
Your goal is to find the minimum number of cuts required among all rods that meet these criteria. If no rod can produce a piece of length m using the above rules, output -1
.
Note: Only one rod is cut at a time and pieces from different rods cannot be combined.
inputFormat
The input is provided via standard input (stdin). The first line contains two space-separated integers, n and m, where n is the number of rods and m is the target length. The second line contains n space-separated integers representing the lengths of the rods.
outputFormat
Output a single integer to standard output (stdout): the minimum number of cuts required to obtain a rod (or pieces from one rod) of length m, or -1
if it is not possible.
5 5
8 5 3 10 7
0