#C8842. Achieve Target Weight
Achieve Target Weight
Achieve Target Weight
You are given (n) wooden slats with weights (w_1, w_2, \dots, w_n) and a target weight (W). Your task is to determine whether there exists a subset of these slats whose total weight is exactly (W). This is a classic subset sum problem, and one common approach is to use dynamic programming. In particular, you can define a boolean array (dp) of size (W+1) where (dp[i]=true) indicates that some subset of the given slats sums to (i).
inputFormat
The input is read from standard input. The first line contains an integer (T) representing the number of test cases. Each test case consists of three lines:
- An integer (n) denoting the number of slats.
- A line with (n) space-separated integers representing the weights of the slats. (If (n = 0), this line will be empty.)
- An integer (W) representing the target weight.
outputFormat
For each test case, output a single line containing either 'YES' if it is possible to achieve the target weight exactly, or 'NO' otherwise. The output should be written to standard output.## sample
2
4
1 3 4 5
7
3
2 2 2
5
YES
NO
</p>