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:
#coding:utf-8
import numpy as np

n = int(input())
A = list(map(int,input().split()))
q = int(input())
mList = list(map(int,input().split()))

dp = [[False for i in range(2000)] for j in range(20)]
dp = np.array(dp)

def solve(i,m):
    if dp[i,m]:
        return dp[i,m]

    if m == 0:
        dp[i,m] = True
    elif i >= n or m < 0:
        dp[i,m] = False
    elif solve(i+1,m):
        dp[i,m] = True
    elif solve(i+1,m-A[i]):
        dp[i,m] = True
    else:
        dp[i,m] = False

    return dp[i,m]

for m in mList:
    a = solve(0,m)
    if a:
        print("yes")
    else:
        print("no")

Submission status: Runtime Error

Solution code:
#coding:utf-8

n = int(input())
A = list(map(int,input().split()))
q = int(input())
mList = list(map(int,input().split()))

dp = [[False for i in range(2000)] for j in range(20)]

def solve(i,m):
    if dp[i][m]:
        return dp[i][m]

    if m == 0:
        dp[i][m] = True
    elif i >= n or m < 0:
        dp[i][m] = False
    elif solve(i+1,m):
        dp[i][m] = True
    elif solve(i+1,m-A[i]):
        dp[i][m] = True
    else:
        dp[i][m] = False

    return dp[i][m]

for m in mList:
    a = solve(0,m)
    if a:
        print("yes")
    else:
        print("no")

Submission status: Runtime Error

Solution code:
#coding:utf-8
import numpy as np

n = int(input())
A = list(map(int,input().split()))
q = int(input())
mList = list(map(int,input().split()))

dp = [[False for i in range(2001)] for j in range(21)]
dp = np.array(dp)

def solve(i,m):
    if dp[i,m]:
        return dp[i,m]
    if m < 0:
        return

    if m == 0:
        dp[i,m] = True
    elif i >= n:
        dp[i,m] = False
    elif solve(i+1,m):
        dp[i,m] = True
    elif solve(i+1,m-A[i]):
        dp[i,m] = True
    else:
        dp[i,m] = False

    return dp[i,m]

for m in mList:
    a = solve(0,m)
    if a:
        print("yes")
    else:
        print("no")

Submission status: Runtime Error

Solution code:
#coding:utf-8

n = int(input())
A = list(map(int,input().split()))
q = int(input())
mList = list(map(int,input().split()))

dp = [[False for i in range(2001)] for j in range(21)]

def solve(i,m):
    if dp[i][m]:
        return dp[i][m]
    if m < 0:
        return

    if m == 0:
        dp[i][m] = True
    elif i >= n:
        dp[i][m] = False
    elif solve(i+1,m):
        dp[i][m] = True
    elif solve(i+1,m-A[i]):
        dp[i][m] = True
    else:
        dp[i][m] = False

    return dp[i][m]

for m in mList:
    a = solve(0,m)
    if a:
        print("yes")
    else:
        print("no")

Submission status: Runtime Error

Solution code:
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

n = int(input())
A = [int(e) for e in input().split()]
q = int(input())
M = [int(e) for e in input().split()]
for m in M:
    result = solve(0, m)
    if result == True:
        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 main():
    for number in m:
        if solve(0, number):
            print('yes')
        else :
            print('no')

def solve(i, m):
    if m == 0:
        return 1

    if i >= n:
        return 0

    res = solve(i + 1, m) | solve(i + 1, m - a[i])
    return res

if __name__ == '__main__':
    main()

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 main():
    for number in m:
        if solve(n - 1, 0, number):
            print('yes')
        else :
            print('no')

def solve(i, m, number):
    if m == number:
        return 1

    if i < 0:
        return 0

    res = solve(i - 1, m, number) | solve(i - 1, m + a[i], number)
    return res

if __name__ == '__main__':
    main()

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 main():
    for number in m:
        if solve(0, number):
            print('yes')
        else :
            print('no')

def solve(i, m):
    if m == 0:
        return 1

    if i >= n:
        return 0

    res = solve(i + 1, m) or solve(i + 1, m - a[i])
    return res

if __name__ == '__main__':
    main()

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 main():
    for number in m:
        if solve(0, number):
            print('yes')
        else :
            print('no')

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

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from numba.decorators import jit

Submission status: Runtime Error

Solution code:
aN = int(input())
A = list(map(int, input().split()))
mN = int(input())
M = list(map(int, input().split()))

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

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

Submission status: Runtime Error

Solution code:

def solve(arr, target):
	return solve_rec(0, arr, target)

def solve_rec(i, arr, target):	
	if target == 0:
		return True
	if i >= len(arr):
		return False
	res1 = solve_rec(i + 1, arr, target)
	res2 = solve_rec(i + 1, arr, target - arr[i])
	if res1 == True or res2 == True:
		return True
	return False


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

for i in range(q):
	res = solve(arr, targets[i])
	if res == True:
		print('yes')
	else:
		print('no')

Submission status: Runtime Error

Solution code:

def solve(arr, target):
	return solve_rec(0, arr, target)

def solve_rec(i, arr, target):	
	if target == 0:
		return True
	if i >= len(arr):
		return False
	res1 = solve_rec(i + 1, arr, target)
	res2 = solve_rec(i + 1, arr, target - arr[i]
	return res1 or res2

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

for i in range(q):
	res = solve(arr, targets[i])
	if res == True:
		print('yes')
	else:
		print('no')

Submission status: Runtime Error

Solution code:

def solve(arr, target):
	return solve_rec(0, arr, target)

def solve_rec(i, arr, target):	
	if target == 0:
		return True
	if i >= len(arr):
		return False
	res1 = solve_rec(i + 1, arr, target)
	res2 = solve_rec(i + 1, arr, target - arr[i])
	return res1 or res2

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

for i in range(q):
	res = solve(arr, targets[i])
	if res == True:
		print('yes')
	else:
		print('no')

Submission status: Runtime Error

Solution code:

def solve(arr, target):
	return solve_rec(0, arr, target)

def solve_rec(i, arr, target):
	if target == 0:
		return True
	if i >= len(arr):
		return False
	res1 = solve_rec(i + 1, arr, target)
	res2 = solve_rec(i + 1, arr, target - arr[i])
	return res1 or res2

n = int(input())
arr = list(map(int, input().split()))
q = int(input())
targets = list(map(int, input().split()))
for i in range(q):
	res = solve(arr, targets[i])
	if res == True:
		print('yes')
	else:
		print('no')

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin


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


stdin.readline()
a = [int(s) for s in stdin.readline().split()]
stdin.readline()
ms = [int(s) for s in stdin.readline().split()]

for m in ms:
    if solve(a, len(a), m, 0):
        print('yes')
    else:
        print('no')

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin


def solve(a, al, m, i):
    if not m:
        return True
    if m > 0 and i < al:
        if solve(a, al, m-a[i], i+1) or solve(a, al, m, i+1):
            return True
    return False


stdin.readline()
a = [int(s) for s in stdin.readline().split()]
a.sort(reverse=True)
stdin.readline()
ms = [int(s) for s in stdin.readline().split()]

for m in ms:
    if solve(a, len(a), m, 0):
        print('yes')
    else:
        print('no')

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from __future__ import print_function,division
from itertools import combinations
import time
import sys
import io
import re
import math
start = time.clock()
i = 0
def enum_sum_numbers(sets, s_range, r):
    for cmb in combinations(sets,r):
        yield sum(cmb)
    if r <=s_range:
        for s in enum_sum_numbers(sets,s_range, r+1):
            yield s

sys.stdin.readline()
a=[int(s) for s in sys.stdin.readline().split()]
sys.stdin.readline()
ms=[int(s) for s in sys.stdin.readline().split()]
sets={s for s in enum_sum_numbers(a,len(a)),1}
for m in ms:
    print('yes' if m in sets else 'no')

Submission status: WA: Presentation Error

Solution code:
class Solver():
    def __init__(self, A: [int]):
        self.A = A
        self.max_depth = len(A)

    def solve(self, tgt):
        self.tgt = tgt
        if self._rec_solve(0, 0):
            return 'yes'
        else:
            return 'no'

    def _rec_solve(self, _sum, index):
        if _sum == self.tgt:
            return True
        elif index >= self.max_depth or _sum > self.tgt:
            return False
        else:
            return self._rec_solve(_sum + self.A[index], index + 1) or self._rec_solve(_sum, index + 1)
            


if __name__ == '__main__':
    n = int(input())
    A = [int(i) for i in input().rstrip().split()]
    q = int(input())
    m_ary = [int(i) for i in input().rstrip().split()]
    s = Solver(A)
    for i in m_ary[:-1]:
        print(s.solve(i))
    print(s.solve(m_ary[-1]), end='')