#C8797. Insert Operators to Reach Target Sum

    ID: 52818 Type: Default 1000ms 256MiB

Insert Operators to Reach Target Sum

Insert Operators to Reach Target Sum

You are given an array of integers and a target integer \(k\). Your task is to determine whether it is possible to insert either a plus (+) or a minus (-) sign in front of each number (in the given order) such that the resulting expression evaluates to \(k\). Formally, you need to decide if there exists a sequence \(\{s_i\}\) where \(s_i \in \{+1, -1\}\) for \(i = 1, 2, \ldots, n\) such that:

[ \sum_{i=1}^{n} s_i \cdot a_i = k, ]

Note that if the array is empty (i.e. \(n=0\)), the sum is defined as 0. This problem can be solved using a depth-first search (DFS) or backtracking approach.

inputFormat

The first line of input contains an integer \(T\) (the number of test cases).

For each test case, the first line contains two space-separated integers \(n\) and \(k\), where \(n\) is the number of elements in the array and \(k\) is the target sum. If \(n > 0\), the second line contains \(n\) space-separated integers representing the array elements.

Example:

5
4 10
1 2 3 4
3 5
1 1 1
3 12
4 4 4
5 -1
1 2 3 4 5
0 0

outputFormat

For each test case, output a single line containing YES if it is possible to obtain the target sum by appropriately choosing the signs for each number, and NO otherwise.

Example:

YES
NO
YES
YES
YES
## sample
2
4 10
1 2 3 4
3 5
1 1 1
YES

NO

</p>