#K41062. Non-Adjacent Gift Boxes Selection
Non-Adjacent Gift Boxes Selection
Non-Adjacent Gift Boxes Selection
You are given a sequence of M gift boxes arranged in a line. Each box i (1 ≤ i ≤ M) contains a gift with a positive integer value Ai.
Your task is to determine whether there exists a subset of these boxes that can be selected such that:
- The sum of the values of the selected boxes is exactly V.
- No two selected boxes are adjacent (i.e. if box i is selected, then box i+1 cannot be selected).
In mathematical terms, if we define a binary indicator xi with xi = 1 when the i-th box is chosen and 0 otherwise, then the condition is to check if there exists a sequence {x1, x2, ..., xM} satisfying:
$$\sum_{i=1}^{M} x_i \cdot A_i = V, \quad \text{and} \quad x_i + x_{i+1} \le 1 \quad \text{for all } 1 \le i < M. $$You need to print Yes
if such a selection exists, or No
otherwise.
inputFormat
The input is given via stdin and follows the format below:
M V A1 A2 ... AM
Where:
- M is the number of gift boxes.
- V is the target sum that needs to be achieved by selecting non-adjacent boxes.
- The second line contains M space-separated integers representing the values in the gift boxes.
outputFormat
The output should be printed to stdout and consists of a single line:
Yes
or
No
indicating whether a valid selection exists.
## sample5 9
3 2 5 10 7
Yes