#K53607. Subset Sum Existence
Subset Sum Existence
Subset Sum Existence
You are given a multiset of n positive integers and an integer x. Your task is to determine whether there exists a non-empty subset of the multiset whose sum is exactly x. Note that each number in the multiset may be used at most once.
Input Constraints:
- 1 ≤ n ≤ 105 (for example)
- 1 ≤ x ≤ 109 (for example)
- Each element of the multiset is a positive integer.
Hint: Use dynamic programming. In particular, define a boolean DP array where \(dp[s]\) is true if there is a subset with sum \(s\). The recurrence can be written as:
\[ \text{dp}[s] = \text{dp}[s] \vee \text{dp}[s - a_i] \quad \text{for each } a_i \text{ in the multiset, with } s \geq a_i. \]
inputFormat
The first line contains two integers n and x, where n is the number of elements in the multiset and x is the target sum.
The second line contains n space-separated positive integers, representing the multiset.
outputFormat
Print a single line containing YES
if there is a non-empty subset of the multiset that sums to x; otherwise, print NO
.
5 9
2 2 3 5 3
YES
</p>