#K36722. Subset Rope Selection
Subset Rope Selection
Subset Rope Selection
You are given n ropes with various lengths and a target length m. Your task is to determine whether there exists a subset of these ropes such that their total length is exactly m meters.
The problem can be formally stated as follows:
Find a subset S of the set of ropes such that \[ \sum_{i \in S} a_i = m, \] where \(a_i\) is the length of the i-th rope.
If such a subset exists, output YES
; otherwise, output NO
.
inputFormat
The input is read from standard input and consists of two lines:
- The first line contains two space-separated integers n and m, where n (1 ≤ n ≤ 20) is the number of ropes and m is the target length in meters.
- The second line contains n space-separated integers representing the lengths of the ropes.
outputFormat
Output a single line to standard output: YES
if there exists a subset of ropes whose total length equals m, otherwise output NO
.
4 10
2 3 7 9
YES