#P8060. Coin Sum Representability

    ID: 21244 Type: Default 1000ms 256MiB

Coin Sum Representability

Coin Sum Representability

You are given a set \( A \) consisting of \( n \) positive integers. Define the set \( A' \) as the set of all non-negative integers \( x \) such that \( x \) can be represented as a sum of some (possibly repeated) elements from \( A \). Formally, a non-negative integer \( x \) belongs to \( A' \) if and only if there exist non-negative integers \( c_1,c_2,\dots, c_n \) with

[ x = c_1 \cdot a_1 + c_2 \cdot a_2 + \cdots + c_n \cdot a_n, ]

where \( a_1,a_2,\dots, a_n \) are the elements in \( A \). Note that by convention the sum using zero elements (i.e. all \( c_i = 0 \)) is taken as \( 0 \), so \( 0 \) always belongs to \( A' \).

Your task is to answer \( q \) queries. For each query, you are given an integer \( x \); determine whether \( x \) belongs to \( A' \) (i.e. whether it can be represented as a sum of the elements of \( A \)).

Example: If \( A = \{2, 5, 7\} \), then some numbers in \( A' \) are:

  • \( 0 \) (using 0 coins)
  • \( 2 \) (\( 2 \) alone)
  • \( 4 \) (\( 2+2 \))
  • \( 12 \) (for example, \( 5+7 \) or \( 2+2+2+2+2+2 \))

However, numbers like \( 1 \) and \( 3 \) cannot be represented as sums of elements of \( A \), and thus do not belong to \( A' \).

inputFormat

The input is given in the following format:

n
a1 a2 ... an
q
x1
x2
... 
 xq

Where:

  • The first line contains a single integer \( n \), the number of elements in \( A \).
  • The second line contains \( n \) space-separated positive integers \( a_1, a_2, \dots, a_n \).
  • The third line contains a single integer \( q \), the number of queries.
  • Each of the next \( q \) lines contains a single integer \( x \) representing a query.

outputFormat

For each query, print a single line containing YES if \( x \) can be represented as a sum of the provided numbers (with repetition allowed), or NO otherwise.

sample

3
2 5 7
4
0
1
4
12
YES

NO YES YES

</p>