#K61532. Subsequence Sum Query
Subsequence Sum Query
Subsequence Sum Query
You are given a list of integers and a series of queries. For each query, determine whether there exists a subsequence (not necessarily contiguous) of the given list whose sum exactly equals the query value. A subsequence is defined as any sequence that can be derived from the list by deleting some or no elements without changing the order of the remaining elements.
Formally, given a list of integers \(a_1, a_2, \dots, a_M\) and a query value \(q\), you need to check if there exists a subsequence \(S \subseteq \{1, 2, \dots, M\}\) such that
[ \sum_{i \in S} a_i = q ]
If such a subsequence exists, output YES
; otherwise, output NO
.
Note: The subsequence can be empty (which sums to 0) and the list may contain negative numbers.
inputFormat
The input is given via standard input and consists of the following:
- The first line contains an integer
M
representing the number of integers in the list. - The second line contains
M
space-separated integers. - The third line contains an integer
Q
representing the number of queries. - The fourth line contains
Q
space-separated integers, each being a query value.
outputFormat
For each query, output YES
if there exists a subsequence whose sum equals the query, or NO
if there is no such subsequence. Each answer should be printed on a separate line.
5
1 2 3 -4 5
3
7 -1 0
YES
YES
YES
</p>