#C9113. Minimum Number of Cuts

    ID: 53171 Type: Default 1000ms 256MiB

Minimum Number of Cuts

Minimum Number of Cuts

You are given a wooden plank of length (L) and a list of (n) required piece lengths ({l_1, l_2, \dots, l_n}). Your task is to determine the minimum number of cuts required to divide the plank into these pieces such that the entire plank is used. Note that if (\sum_{i=1}^{n} l_i \neq L), then it is impossible to obtain the required pieces, and you should output -1.

In an ideal scenario where the pieces exactly add up to the total length, the number of cuts needed is (n-1) (since no cut is needed before the first piece and each subsequent piece requires a cut).

inputFormat

Input is read from standard input. The first line contains an integer (L), the length of the plank. The second line contains an integer (n), the number of required pieces. The third line contains (n) space-separated integers representing the required lengths.

outputFormat

Output a single integer representing the minimum number of cuts needed. If it is impossible to cut the plank into the required pieces (i.e. when (\sum_{i=1}^{n} l_i \neq L)), output -1.## sample

10
3
2 3 5
2