Problem 754
Subset Sum Equal to Target
Medium · 20 pts · Dynamic Programming, Knapsack
Problem
Given N positive integers and a target K, decide whether some subset of them (each element used at most once) adds up to exactly K. The empty subset is not allowed, but K is always at least 1, so this never matters. Keep a boolean table possible[s] for every sum s from 0 to K, starting with only possible[0] true, and for each element update the sums from high to low so that every element is used at most once.
Input
The first line contains N and K. The second line contains the N integers.
Output
Print YES if some subset sums to K, otherwise NO.
Constraints
1 <= N <= 1000; 1 <= A[i] <= 1000; 1 <= K <= 1000
Example input
4 5 4 3 2 1
Example output
YES
Explanation
For 4 3 2 1 with K = 5, the subset {4, 1} (or {3, 2}) adds up to 5, so the answer is YES.