Problem:


Exhaustive Search

Write a program which reads a sequence A of n elements and an integer M, and outputs "yes" if you can make M by adding elements in A, otherwise "no". You can use an element only once.

You are given the sequence A and q questions where each question contains Mi.

Input

In the first line n is given. In the second line, n integers are given. In the third line q is given. Then, in the fourth line, q integers (Mi) are given.

Output

For each question Mi, print yes or no.

Constraints

n ≤ 20

 q ≤ 200 

1 ≤ elements in A ≤ 2000

1 ≤ Mi ≤ 2000

Sample Input 1

5
1 5 7 10 21
8
2 4 17 8 22 21 100 35

Sample Output 1

no
no
yes
yes
yes
yes
no
no

Notes

You can solve this problem by a Burte Force approach. Suppose solve(p, t) is a function which checkes whether you can make t by selecting elements after p-th element (inclusive). Then you can recursively call the following functions:

solve(0, M) 

solve(1, M-{sum created from elements before 1st element}) 

solve(2, M-{sum created from elements before 2nd element}) 

...

The recursive function has two choices: you selected p-th element and not. So, you can check solve(p+1, t-A[p]) and solve(p+1, t) in solve(p, t) to check the all combinations.

For example, the following figure shows that 8 can be made by A[0] + A[2].

Template in C
-->



Sample input:
5
1 5 7 10 21
8
2 4 17 8 22 21 100 35


Sample output:
no
no
yes
yes
yes
yes
no
no

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int, input().split()))
q = int(input())
mi = map(int, input().split())

def solve(i, m):
    if (m == 0):
        return True
    if (i >= n):
        return False
    res = solve(i + 1, m) or solve(i + 1, m - A[i])
    return res

for i in range(q):
    if solve(0, mi):
        print("yes")
    else:
        print("no")

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int, input().split()))
q = int(input())
mi = set(map(int, input().split()))

def solve(i, m):
    if (m == 0):
        return True
    if (i >= n):
        return False
    res = solve(i + 1, m) or solve(i + 1, m - A[i])
    return res

for i in range(q):
    if solve(0, mi):
        print("yes")
    else:
        print("no")

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int, input().split()))
q = int(input())
mi = map(int, input().split())

def solve(i, m):
    if (m == 0):
        return True
    if (i >= n):
        return False
    res = solve(i + 1, m) or solve(i + 1, m - A[i])
    return res

for j in range(q):
    if solve(0, mi):
        print("yes")
    else:
        print("no")

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int, input().split()))
q = int(input())
mi = map(int, input().split())
S = {}

def solve(i, m):
    if (m == 0):
        return True
    if (i >= n):
        return False
    
    res = solve(i + 1, m) or solve(i + 1, m - A[i])
    return res

for j in range(1, q + 1):
    if solve(0, mi):
        print("yes")
    else:
        print("no")

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int, input().split()))
q = int(input())
mi = map(int, input().split())
S = {}

def solve(i, m):
    if (m == 0):
        return True
    if (i >= n):
        return False
    
    res = solve(i + 1, m) or solve(i + 1, m - A[i])
    return res

for j in range(q):
    if solve(0, mi):
        print("yes")
    else:
        print("no")

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int, input().split()))
q = int(input())
mi = map(int, input().split())
tmp = {}

def solve(i, m):
    if (m == 0):
        return True
    if (i >= n):
        return False
    if (i,m) in tmp:
        return tmp[(i,m)]
    res = solve(i + 1, m) or solve(i + 1, m - A[i])
    tmp[(i,m)] = res
    return res

for j in range(q):
    if solve(0, mi):
        print("yes")
    else:
        print("no")

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int, input().split()))
q = int(input())
mi = map(int, input().split())
t = {}

def solve(i, m):
    if (m == 0):
        return True
    if (i >= n):
        return False
    if (i,m) in t:
        return t[(i,m)]
    res = solve(i + 1, m) or solve(i + 1, m - A[i])
    t[(i,m)] = res
    return res

for j in range(q):
    if solve(0, mi):
        print("yes")
    else:
        print("no")

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int, input().split()))
q = int(input())
m = list(map(int, input().split()))

def solve(i, m):
  if m == 0:
    return True
  if i >= n :
    return False
  res = solve(i + 1, m) or solve(i + 1, m - a[i])
  return res

for tmp_m in m:
  if sum(a) < tmp_m:
    print('no')
  elif solve(0, tmp_m):
    print('yes')
  else:
    print('no')

Submission status: Runtime Error

Solution code:
n = int(sys.stdin.readline())
A = list(map(int, input().split()))
q = int(input())
M = list(map(int, input().split()))


def solve(i, m): 
    if m == 0:
        return True
    elif i >= n:
        return False
    res = solve(i + 1, m) or solve(i + 1, m - A[i])
    return res
 
for m in M:
    if solve(0, m):
        print('yes')
    else:
        print('no')

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int, input().split()))
q = int(input())
M = list(map(int, input().split()))

table = [[0]*max(M)]*n

def solve(i, m): 
    if m == 0:
        return True
    elif i >= n:
        return False
    elif table[i][m] != 0:
        return table[i][m]
    else:
        res = solve(i + 1, m) or solve(i + 1, m - A[i])
        table[i][m] = res
        return res

for m in M:
    if solve(0, m):
        print('yes')
    else:
        print('no')

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int, input().split()))
q = int(input())
M = list(map(int, input().split()))

table = [[0 for i in range(max(M))] for j in range(n)]

def solve(i, m): 
    if m == 0:
        return True
    elif i >= n:
        return False
    elif table[i][m] != 0:
        return table[i][m]
    else:
        res = solve(i + 1, m) or solve(i + 1, m - A[i])
        table[i][m] = res
        return res

for m in M:
    if solve(0, m):
        print('yes')
    else:
        print('no')

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int, input().split()))
q = int(input())
M = list(map(int, input().split()))

table = [[0 for i in range(n)] for j in range(max(M))]

def solve(i, m): 
    if m == 0:
        return True
    elif i >= n:
        return False
    elif table[i][m] != 0:
        return table[i][m]
    else:
        res = solve(i + 1, m) or solve(i + 1, m - A[i])
        table[i][m] = res
        return res

for m in M:
    if solve(0, m):
        print('yes')
    else:
        print('no')

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int, input().split()))
q = int(input())
M = list(map(int, input().split()))

table = [[0 for i in range(n)] for j in range(max(M)+1)]

def solve(i, m): 
    if m == 0:
        return True
    elif i >= n:
        return False
    elif table[i][m] != 0:
        return table[i][m]
    else:
        res = solve(i + 1, m) or solve(i + 1, m - A[i])
        table[i][m] = res
        return res

for m in M:
    if solve(0, m):
        print('yes')
    else:
        print('no')

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int, input().split()))
q = int(input())
M = list(map(int, input().split()))

table = [[0 for i in range(max(M)+1)] for j in range(n)]

def solve(i, m): 
    if m == 0:
        return True
    elif i >= n:
        return False
    elif table[i][m] != 0:
        return table[i][m]
    else:
        res = solve(i + 1, m) or solve(i + 1, m - A[i])
        table[i][m] = res
        return res

for m in M:
    if solve(0, m):
        print('yes')
    else:
        print('no')

Submission status: Runtime Error

Solution code:
# coding=utf-8
def is_made_sum(array: list, target: int) -> bool:
    if target == 0:
        return True
    elif target < 0:
        return False
    elif not array:
        return False
    else:
        choice = array[0]
        result1 = is_made_sum(array[1:], target)
        result2 = is_made_sum(array[1:], target - choice)
        return result1 | result2


if __name__ == '__main__':
    n = int(input())
    A = list(map(int, input().split()))
    q = int(input())
    M = list(map(int, input().split()))

    for i in range(q):
        answer = is_made_sum(A, M[i])
        if answer:
            print('yes')
        else:
            print('no')

Submission status: Runtime Error

Solution code:
# coding=utf-8
def is_made_sum(array: list, target: int) -> bool:
    if target == 0:
        return True
    elif target < 0:
        return False
    elif not array:
        return False
    else:
        choice = array[0]
        result1 = is_made_sum(array[1:], target)
        result2 = is_made_sum(array[1:], target - choice)
        return result1 or result2


if __name__ == '__main__':
    n = int(input())
    A = list(map(int, input().split()))
    q = int(input())
    M = list(map(int, input().split()))

    for i in range(q):
        answer = is_made_sum(A, M[i])
        if answer:
            print('yes')
        else:
            print('no')

Submission status: Runtime Error

Solution code:
def main():
    """
    2^20 = 1,048,576
    q = 200
    200,000,000
    limit 5s ???????????????OK
    """
    n = int(input())
    A = list(map(int, input().split()))
    q = int(input())
    M = map(int, input().split())

    for m in M:
        if solve(m, A, n):
            print("yes")
        else:
            print("no")


def solve(m, A, n):
    for b in f(n):
        s = sum(a * bit for a, bit in zip(A, b))
        if m == s:
            return True

    return False


def f(n):
    for i in range(2 ** n):
        # 0/1???n??????????????????bit????????¨
        # 0b ?????????
        bit = bin(i)[2:]
        yield map(int, bit.zfill(n))

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from itertools import product


def main():
    """
    2^20 = 1,048,576
    q = 200
    200,000,000
    limit 5s ???????????????OK
    """
    n = int(input())
    A = list(map(int, input().split()))
    q = int(input())
    M = map(int, input().split())

    for m in M:
        if m == 0 or solve(m, A, n):
            print("yes")
        else:
            print("no")


def solve(m, A, n):
    # for b in f1(n):
    for b in product([0, 1], repeat=n):
        s = sum(a * bit for a, bit in zip(A, b))
        if m == s:
            return True

    return False


def f1(n):
    for i in range(2 ** n):
        # 0/1???n??????????????????bit????????¨
        # 0b ?????????
        bit = bin(i)[2:]
        yield map(int, bit.zfill(n))

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:

def main():
    base = get_input()
    li = get_input()
    base.sort()

    for l in li:
        if check_list(base, 0, l, len(base)):
            print('yes')
        else:
            print('no')


def check_list(base, idx, key, length):
    if key == 0:
        return True
    elif idx >= length:
        return False

    res = check_list(base, idx + 1, key, length) or check_list(base, idx + 1, key - base[idx], length)
    return res


def get_input():
    _ = int(input())
    li = [int(x) for x in input().split(' ')]
    return li


main()

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(10000)


def main():
    base = get_input()
    li = get_input()
    base.sort()

    for l in li:
        if check_list(base, 0, l, len(base)):
            print('yes')
        else:
            print('no')


def check_list(base, idx, key, length):
    if key == 0:
        return True
    elif idx >= length:
        return False

    res = check_list(base, idx + 1, key, length) or check_list(base, idx + 1, key - base[idx], length)
    return res


def get_input():
    _ = int(input())
    li = [int(x) for x in input().split(' ')]
    return li


main()

Submission status: Runtime Error

Solution code:
input()
a = [int(i) for i in input().split()]

def solve(i, m):
    if m == 0:
        return True
    if i >= len(a):
        return False
    return solve(i+1, m) or solve(i+1, m - a[i]) # m - a[i] ??§????????°???????????£???????????????????????????

input()
for i in input().split():
    print('yes') if solve(0, int(i)) else print('no')

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10000)

input()
a = [int(i) for i in input().split()]

def solve(i, m):
    if m == 0:
        return True
    if i >= len(a):
        return False
    return solve(i+1, m) or solve(i+1, m - a[i]) # m - a[i] ??§????????°???????????£???????????????????????????

input()
for i in input().split():
    print('yes') if solve(0, int(i)) else print('no')

Submission status: Runtime Error

Solution code:
from itertools import combinations
n = int(input())
A = tuple(map(int, input().split()))
def solve(m):
    if m in A:
        return "yes"
    for i in range(2, n + 1):
        for C in combinations(A, i):
            if sum(C) == m:
                return "yes"
    return "no"
q = int(input())
for m in map(int, input().split()):
    print(solve(m))

Submission status: Runtime Error

Solution code:
from itertools import combinations
n = int(input())
A = tuple(map(int, input().split()))
SET = set(A)
def solve(m):
    if m in A:
        return "yes"
    for i in range(1, n):
        for C in combinations(A, i):
            sumC = sum(C)
            if sumC < m and m - sumC in SET - set(C):
                return "yes"
    return "no"
q = int(input())
for m in map(int, input().split()):
    print(solve(m))

Submission status: Runtime Error

Solution code:
from itertools import combinations
n = int(input())
A = tuple(map(int, input().split()))
SET = set(A)
def solve(m):
    if m in A:
        return "yes"
    dp = [0]
    for a in A:
        q = []
        for d in dp:
            q.append(d)
            q.append(d + a)
        dp = q
    if m in dp:
        return "yes"
    return "no"
q = int(input())
for m in map(int, input().split()):
    print(solve(m))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-


if __name__ == '__main__':

    n = int(input())
    A = [int(a) for a in input().split(" ")]
    q = int(input())
    M = [int(m) for m in input().split(" ")]

    def solve(i, m):
        if m == 0:
            return True
        if i >= n:
            return False
        res = solve(i+1, m) or solve(i+1, m-A[i])
        return res

    for m in M:
        if solve(0, m)==True:
            print("yes")

        else:
            print("no")

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-


if __name__ == '__main__':

    n = int(input())
    A = [int(a) for a in input().split(" ")]
    q = int(input())
    M = [int(m) for m in input().split(" ")]

    def solve(i, m):
        if m == 0:
            return True
        if i >= n:
            return False
        res = solve(i+1, m) or solve(i+1, m-A[i])
        return res

    ans = []
    for m in M:
        if solve(0, m)==True:
            ans.append("yes")

        else:
            ans.append("no")

    print("\n".join(ans))

Submission status: Runtime Error

Solution code:
A_count = int(input());
A = [int(n) for n in input().split()];
M_count = int(input());
M = [int(n) for n in input().split];

def solve(start, target):
    if target == 0:
        return True;
    elif start >= n:
        return False:
    else:
        return solve(start + 1, target) || solve(start + 1, target - A[start]);

for m in M:
    if solve(0, m):
        print("yes");
    else:
        print("no");

Submission status: Runtime Error

Solution code:
A_count = int(input());
A = [int(n) for n in input().split()];
M_count = int(input());
M = [int(n) for n in input().split()];

def solve(start, target):
    if target == 0:
        return True;
    elif start >= A_count:
        return False;
    else:
        return solve(start + 1, target) or solve(start + 1, target - A[start]);

for m in M:
    if solve(0, m):
        print("yes");
    else:
        print("no");

Submission status: Runtime Error

Solution code:
A_count = int(input());
A = [int(n) for n in input().split()];
M_count = int(input());
M = [int(n) for n in input().split()];

memo = [[None for i in range(2000)] for j in range(A_count)];

def solve(start, target):
    if memo[start][target] is not None:
        return memo[start][target];
    if target == 0:
        return True;
    elif start >= A_count:
        return False;
    else:
        result = solve(start + 1, target) or solve(start + 1, target - A[start]);
        if result:
            memo[start][target] = True;
            return True;
        else:
            memo[start][target] = False;
            return False;

for m in M:
    if solve(0, m):
        print("yes");
    else:
        print("no");

Submission status: Runtime Error

Solution code:

def solve(i, m):
    if m == 0:
        return True
    elif i >= n:
        return False
    res = solve(i + 1, m) or solve(i + 1, m - arr[i])
    return res

n = int(input())
arr = list(map(int, input().split()))
q = input()
requests = list(map(int, input().split()))

for req in requests:
    if solve(0, req):
        print('yes')
    else:
        print('no')

Submission status: Runtime Error

Solution code:
def exSearch(i, m, hoge):
    if m == 0:
        return True
    if i >= len(hoge):
        return False
    res = exSearch(i+1, m, hoge) or exSearch(i+1, m-hoge[i], hoge)
    return res

_ = int(input())
hoge = [int(x) for x in input().split()]
_ = int(input())

for val in (int(x) for x in input().split()):
    if exSearch(0, val, hoge):
        print ('yes')
    else:
        print ('no')

Submission status: Runtime Error

Solution code:
def exSearch(i, m, hoge):
    if m == 0:
        return True
    if i >= len(hoge):
        return False
    res = exSearch(i+1, m, hoge) or exSearch(i+1, m-hoge[i], hoge)
    return res

if __name__ == '__main__':
    _ = int(input())
    hoge = [int(x) for x in input().split()]
    _ = int(input())

    for val in (int(x) for x in input().split()):
        if exSearch(0, val, hoge):
            print ('yes')
        else:
            print ('no')

Submission status: Runtime Error

Solution code:
_ = int(input())
hoge = [int(x) for x in input().split()]
_ = int(input())

def exSearch(i, m):
    if m == 0:
        return True
    if i >= len(hoge):
        return False
    res = exSearch(i+1, m) or exSearch(i+1, m-hoge[i])
    return res

if __name__ == '__main__':
    
    for val in (int(x) for x in input().split()):
        if exSearch(0, val):
            print ('yes')
        else:
            print ('no')

Submission status: Runtime Error

Solution code:
num_n = int(input())
n = map(int, input().split())
num_m = int(input())
m = map(int, input().split())
     
def solve(i, m):
   if m == 0:
      return True
   if i >= num_n or m > sum(n):
      return False
   res = solve(i + 1, m) or solve(i + 1,m - n[i])
   return res
      
for k in range(num_m):
   if solve(0, m[k]):
      print ("yes")
   else:
      print ("no")

Submission status: Runtime Error

Solution code:
def solve(a,m):
    S = []
    ans = []
    tmp = 0
    for i in range(1,2**len(a)):
        S.append(str(format(i,'b').zfill(len(a))))
    for s in S:
        for j in range(len(a)):
            if s[j] == '1':
                tmp += a[j]
        if tmp == m:return 'yes'
        tmp = 0
    else:return 'no'

n = int(input())
a =  [int(i) for i in input().split()]
q = int(input())
M = [int(i) for i in input().split()]

for m in M:
    print(solve(a,m))

Submission status: Runtime Error

Solution code:
def solve2(i,m,a,n):
    if m == 0:
        return True
    if i >= n:
        return False
    res = solve2(i+1,m,a,n) or solve2(i+1,m-a[i],a,n)
    return res

n = int(input())
a =  [int(i) for i in input().split()]
q = int(input())
M = [int(i) for i in input().split()]

for m in M:
  if solve2(0,m,a,n):
     print('yes')
  else:
     print('no')

Submission status: Runtime Error

Solution code:
 @functools.lru_cache()
def solve2(i,m,a,n):
    if m == 0:
        return True
    if i >= n:
        return False
    res = solve2(i+1,m,a,n) or solve2(i+1,m-a[i],a,n)
    return res

n = int(input())
a =  [int(i) for i in input().split()]
q = int(input())
M = [int(i) for i in input().split()]

for m in M:
  if solve2(0,m,a,n):
     print('yes')
  else:
     print('no')

Submission status: Runtime Error

Solution code:
@functools.lru_cache()
def solve2(i,m,a,n):
    if m == 0:
        return True
    if i >= n:
        return False
    res = solve2(i+1,m,a,n) or solve2(i+1,m-a[i],a,n)
    return res

n = int(input())
a =  [int(i) for i in input().split()]
q = int(input())
M = [int(i) for i in input().split()]

for m in M:
  if solve2(0,m,a,n):
     print('yes')
  else:
     print('no')

Submission status: Runtime Error

Solution code:
import functools
@functools.lru_cache()
def solve2(i,m,):
    if m == 0:
        return True
    if i >= n:
        return False
    res = solve2(i+1,m) or solve2(i+1,m-a[i])
    return res
global n
global a
n = int(input())
a =  [int(i) for i in input().split()]
q = int(input())
M = [int(i) for i in input().split()]

for m in M:
  if solve2(0,m):
     print('yes')
  else:
     print('no')

Submission status: Runtime Error

Solution code:
n=int(input())
A=list(map(int,input().split()))
r=set()
def f(s,k):
    if k>=0:
        global r
        r|=set(s)
        f(s+A[k-1],k-1)
        f(s,k-1)
f(0,n)
input()
for e in map(int,input().split()):print(['no','yes'][e in r])

Submission status: Runtime Error

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-

def solve(p, t, A):
    if A[p] == t:
        return True
    if len(A)-1 == p:
        return False
    if solve(p+1, t-A[p], A):
        return True
    if solve(p+1, t, A):
        return True
    return False

import sys

n = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))
num_q = int(sys.stdin.readline())
query = list(map(int, sys.stdin.readline().split()))

for m in query:
    if solve(0, m, A):
        print('yes')
    else:
        print('no')

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
m = list(map(int, input().split()))

def f(i, m):
    if m == 0:
        return True
    elif i >= n:
        return False
    elif f(i+1, m):
        return True
    elif f(i+1, m-a[i]):
        return True

for k in m:
    print('yes' if f(0, k) else 'no')

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
m = list(map(int, input().split()))

def f(i, m):
    if m == 0:
        return True
    elif m < 0:
        return False
    elif i >= n:
        return False
    elif f(i+1, m):
        return True
    elif f(i+1, m-a[i]):
        return True

for k in m:
    print('yes' if f(0, k) else 'no')

Submission status: Runtime Error

Solution code:
def calc(a, m, x):
    if a == []:
        return False
    y = a[0]
    if x == m:
        return True
    elif (x + y) == m:
        return True
    else:
        return calc(a[1:], m, x) or calc(a[1:], m, x + y) or calc(a[1:], m, y)

n = int(input())
a = list(map(int, input().split()))
q = int(input())
m = list(map(int, input().split()))
for mi in m:
    if calc(a[:], mi, 0):
        print("yes")
    else:
        print("no")

Submission status: Runtime Error

Solution code:
def calc(a, m, x):
    if a == []:
        return False
    y = a[0]
    if x == m:
        return True
    elif (x + y) == m:
        return True
    else:
        return calc(a[1:], m, x) or calc(a[1:], m, x + y)

n = int(input())
a = list(map(int, input().split()))
q = int(input())
m = list(map(int, input().split()))
for mi in m:
    if calc(a[:], mi, 0):
        print("yes")
    else:
        print("no")

Submission status: Runtime Error

Solution code:
def calc(a, m, x):
    if a == []:
        return False
    y = a[0]
    if x == m:
        return True
    elif (x + y) == m:
        return True
    else:
        return calc(a[1:], m, x + y) or calc(a[1:], m, x)

n = int(input())
a = list(map(int, input().split()))
q = int(input())
m = list(map(int, input().split()))
for mi in m:
    if calc(a, mi, 0):
        print("yes")
    else:
        print("no")

Submission status: Runtime Error

Solution code:
def calc(a, m, x):
    if a == []:
        return False
    y = a[0]
    if (x + y) == m:
        return True
    else:
        return calc(a[1:], m, x) or calc(a[1:], m, x + y)

n = int(input())
a = list(map(int, input().split()))
q = int(input())
m = list(map(int, input().split()))
for mi in m:
    if calc(a, mi, 0):
        print("yes")
    else:
        print("no")

Submission status: Runtime Error

Solution code:
n=int(input())
A=input().split(' ')
q=input()
M=input().split(' ')

def solve(i,m):
	if m==0:
		return(True)
	if i>=n:
		return(False)
	res=solve(i+1,m) or solve(i+1,m-int(A[i]))
	return(res)
	

for j in M:
	if solve(0,int(j)):
		print('yes')
	else:
		print('no')

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
m = list(map(int, input().split()))

def f(i, m):
    if m == 0:
        return True
    elif m < 0:
        return False
    elif i >= n:
        return False
    elif f(i+1, m):
        return True
    elif f(i+1, m-a[i]):
        return True

for k in m:
    print('yes' if f(0, k) else 'no')

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
m = list(map(int, input().split()))

def f(i, m):
    if m == 0:
        return True
    elif m < 0:
        return False
    elif i >= n:
        return False
    elif f(i+1, m):
        return True
    elif f(i+1, m-a[i]):
        return True
    else:
        return False

for k in m:
    print('yes' if f(0, k) else 'no')

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
m = list(map(int, input().split()))

memory = [[-1 for i in range(q)]for j in range(n)]

def f(i, m):
    if memory[i][m] != -1:
        return memory[i][m]
    if m == 0:
        return 1
    elif m < 0:
        return 0
    elif i >= n:
        return 0
    elif f(i+1, m):
        memory[i+1][m] = 1
        return 1
    elif f(i+1, m-a[i]):
        memory[i+1][m-a[i]] = 1
        return 1
    else:
        return 0

for k in m:
    print('yes' if f(0, k) else 'no')

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int, input().split()))
q = int(input())
m = list(map(int, input().split()))

T = pow(2,n)-1

for k in range(q):
    z = 0
    for i in range(T):
        B = list(format(i,"0"+str(n)+"b"))
        sum = 0
        for j in range(n):
            sum += A[j]*int(B[j])
        if sum == m[k]:
            z =1
            break
    if z==1:
        print("yes")
    else:
        print("no")

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
m = list(map(int, input().split()))
memory = [[-1 for i in range(2000)]for j in range(n)]

def f(i, k, sum):
    if memory[i][sum] != -1:
        return memory[i][sum]
    if sum == k:
        memory[i][sum] = 1
        return 1
    elif sum > k:
        memory[i][sum] = 0
        return 0
    elif i >= n:
        return 0
    else:
        res0 = f(i+1, k, sum)
        res1 = f(i+1, k, sum + a[i])
        
        if res0 or res1:
            memory[i][sum] = 1
            return 1
        else:
            memory[i][m] = 0
            return 0

for k in m:
    print('yes' if f(0, k, 0) else 'no')

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
m = list(map(int, input().split()))

def f(i, m):
    if m == 0:
        return 1
    elif i >= n:
        return 0
    elif m - a[i] >= 0:
        res0 = f(i+1, m)
        res1 = f(i+1, m-a[i])
        if res0 or res1:
            return 1
        else:
            return 0
    else:
        res = f(i+1, m)
        return res

for k in m:
    print('yes' if f(0, k) else 'no')

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int, input().split()))
q = int(input())
m = list(map(int, input().split()))


def agari(i):
    if X[i]==1:
        X[i]=0
        agari(i+1)
    else:
        X[i]=1

for j in range(q):
    k = 0
    flag = 0
    X = [0] * 10
    while k<(pow(2,n)-1):
        y = 0
        for l in range(n):
            y += A[l]*X[l]
        if y == m[j]:
            flag = 1
            break
        agari(0)
        k+=1
    if flag == 1:
        print("yes")
    else:
        print("no")

Submission status: Runtime Error

Solution code:
class Tree:
    class Node:
        def __init__(self, n): 
            self.left = None
            self.right = None
            self.n = n 

        def find(self, total):
            if total == 0:
                return True
            elif total < 0:
                return False

            if self.left is None:
                left = (total == self.n)
            else:
                left = self.left.find(total-self.n)

            if self.right is None:
                right = False
            else:
                right = self.right.find(total)
    
            return left or right

        def __str__(self):
            cs = []
            if self.left is not None:
                cs.append(str(self.left))
            if self.right is not None:
                cs.append(str(self.right))

            return "{} -> ({})".format(self.n, ",".join(cs))


    def __init__(self, ns):
        self.top = self.Node(0)
        self.build_node(self.top, sorted(ns))

    def has_subset(self, total):
        return self.top.find(total)

    def build_node(self, node, ns):
        if len(ns) == 0:
            return
    
        node.left = self.Node(ns[-1])
        node.right = self.Node(ns[-1])
        self.build_node(node.left, ns[:-1])
        self.build_node(node.right, ns[:-1])


def run():
    _ = int(input())  # flake8: noqa
    ns = [int(i) for i in input().split()]

    tree = Tree(ns)
    _ = int(input())  # flake8: noqa
    
    for q in (int(j) for j in input().split()):
        if tree.has_subset(q):
            print("yes")
        else:
            print("no")


if __name__ == '__main__':
    run()