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:
class Tree:
    class Node:
        def __init__(self, n):
            self.left = None
            self.right = None
            self.n = n

        def find(self, total):
            if total == self.n:
                return True
            elif total < self.n:
                return (self.right is not None
                        and self.right.find(total))
            else:
                return ((self.left is not None
                        and self.left.find(total-self.n))
                        or
                        (self.right is not None
                         and self.right.find(total)))

        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.build_nodes(sorted(ns))

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

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

        return node


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()

Submission status: Runtime Error

Solution code:
# jは目的の値, iは要素のリストのキー
def solve(i, j):
    global a, A
    if j == 0:
        return True
    if i >= a:
        return False
    # 左はA[i]の要素を使用しない場合、右は使用する場合
    res = solve(i + 1, j) or solve(i + 1, j - A[i])
    return res


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

Submission status: Runtime Error

Solution code:
a = int(input())
A = list(map(int, input().split()))
N = int(input())
M = list(map(int, input().split()))


# jは目的の値, iは要素のリストのキー
def solve(i, j):
    global a, A
    if j == 0:
        return True
    if i >= a:
        return False
    # 左はA[i]の要素を使用しない場合、右は使用する場合
    res = solve(i + 1, j) or solve(i + 1, j - A[i])
    return res


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

Submission status: Runtime Error

Solution code:
a = input()
A = list(map(int, input().split()))
N = input()
M = list(map(int, input().split()))


# jは目的の値, iは要素のリストのキー
def solve(i, j):
    global a, A
    if j == 0:
        return True
    if i >= a:
        return False
    # 左はA[i]の要素を使用しない場合、右は使用する場合
    res = solve(i + 1, j) or solve(i + 1, j - A[i])
    return res


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

Submission status: Runtime Error

Solution code:
a = int(input())
A = list(map(int, input().split()))
N = input()
M = list(map(int, input().split()))


# jは目的の値, iは要素のリストのキー
def solve(i, j):
    global a, A
    if j == 0:
        return True
    if i >= a:
        return False
    # 左はA[i]の要素を使用しない場合、右は使用する場合
    res = solve(i + 1, j) or solve(i + 1, j - A[i])
    return res


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

Submission status: Runtime Error

Solution code:
import copy

combinations = []
def make_combination(n):
    S = [0 for i in range(n)]
    rec(0, n, S)

def rec(i, n, S):
    global combinations
    if i == n:
        combinations.append(copy.copy(S))
        return None
    rec(i+1, n, S)
    S[i] = 1
    rec(i+1, n, S)
    S[i] = 0

n = int(input())
A = [int(x) for x in input().split(' ')]
q = int(input())
M = [int(x) for x in input().split(' ')]

make_combination(n)
for m in M:
    flag = False
    for co in combinations:
        sum = 0
        for i in range(n):
            sum += A[i] * co[i]
            if sum >= m:
                break
        if sum == m:
            flag = True
            break

    if flag:
        print('yes')
    else:
        print('no')

Submission status: Runtime Error

Solution code:
import copy

combinations = []
def make_combination(n):
    S = [0 for i in range(n)]
    rec(0, n, S)

def rec(i, n, S):
    global combinations
    if i == n:
        combinations.append(copy.copy(S))
        return None
    rec(i+1, n, S)
    S[i] = 1
    rec(i+1, n, S)
    S[i] = 0

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

make_combination(n)
for m in M:
    flag = False
    for co in combinations:
        sum = 0
        for i in range(n):
            sum += A[i] * co[i]
            if sum >= m:
                break
        if sum == m:
            flag = True
            break

    if flag:
        print('yes')
    else:
        print('no')

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Wed May  2 21:28:06 2018
ALDS1_5a
@author: maezawa
"""
import itertools as itr

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

for i in m:
    yesorno = False
    b = []
    for j in a:
        if j <= i:
            b.append(j)
    #print(b)
    if len(b)>=1:
        for r in range(1, len(b)+1):
            for comb in itr.combinations(b,r):
                #print(i,comb)
                if sum(comb) == i:
                    yesorno = True
                    break
            if yesorno:
                break
    if yesorno:
        print('yes')
    else:
        print('no')

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Wed May  2 21:28:06 2018
ALDS1_5a
@author: maezawa
"""
import itertools as itr

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

for i in m:
    yesorno = False
    b = []
    for j in a:
        if j <= i:
            b.append(j)
    #print(b)
    lb = len(b)
    if lb >= 1:
        for r in range(1, 2**lb):
            bin_str = format(r, 'b')
            sumb = sum([b[k]*int(bin_str[k]) for k in range(lb)])    #print(i,comb)
            if sumb == i:
                yesorno = True
                break
            if yesorno:
                break
    if yesorno:
        print('yes')
    else:
        print('no')

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Wed May  2 21:28:06 2018
ALDS1_5a
@author: maezawa
"""
import itertools as itr

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

for i in m:
    yesorno = False
    b = []
    for j in a:
        if j <= i:
            b.append(j)
    #print(b)
    lb = len(b)
    if lb >= 1:
        for r in range(1, 2**lb):
            fmt = '0'+str(lb)+'b'
            bin_str = format(r, fmt)
            sumb = sum([b[k]*int(bin_str[k]) for k in range(lb)])    #print(i,comb)
            if sumb == i:
                yesorno = True
                break
            if yesorno:
                break
    if yesorno:
        print('yes')
    else:
        print('no')

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Wed May  2 21:28:06 2018
ALDS1_5a
@author: maezawa
"""
import itertools as itr

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

sum_array = []
for r in range(1,n+1):
    for comb in itr.combinations(a, r):
        aum_array.append[sum(comb)]

for i in m:
    yesorno = 'no'
    for j in sum_array:
        if i == j:
            yesorno == 'yes'
            break
    print('yes')

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Wed May  2 21:28:06 2018
ALDS1_5a
@author: maezawa
"""
import itertools as itr

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

sum_array = []
for r in range(1,n+1):
    for comb in itr.combinations(a, r):
        sum_array.append[sum(comb)]

for i in m:
    yesorno = 'no'
    for j in sum_array:
        if i == j:
            yesorno == 'yes'
            break
    print('yes')

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Thu May  3 11:39:47 2018

@author: maezawa
"""

# -*- coding: utf-8 -*-
"""
Created on Wed May  2 21:28:06 2018
ALDS1_5a_r2  
@author: maezawa
"""

n = int(input())
a = list(map(int, input().split()))
q = int(input())
m_array = list(map(int, input().split()))

def can_comp(i, m): #i番目以降の数列をつかってmを作れるか？
    if m == 0:
        return True
    elif i == n:
        return False
    else:
        return can_comp(i+1, m) or can_comp(i+1, m-a[i])

for m in m_array:
    if can_comp(0, m):
        print('yes')
    else:
        print('no')

Submission status: Runtime Error

Solution code:
global n
n = int(input())
a_array = [int(x) for x in input().split()]
q = int(input())
m_array = [int(x) for x in input().split()]

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

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

Submission status: Runtime Error

Solution code:

# -*- coding: utf-8 -*-
"""
Created on Wed May  2 21:28:06 2018
ALDS1_5a_r2  Time Limit Exceeded
@author: maezawa
"""

n = int(input())
a = list(map(int, input().split()))
q = int(input())
m_array = list(map(int, input().split()))
can_c_array = [[None for _ in range(m)] for _ in range(n)]

def can_comp(i, m): #i番目以降の数列をつかってmを作れるか？
    if m == 0:
        return True
    elif i == n:
        return False
    else:
        if can_c_array[i+1][m] == None:
            c1 = can_comp(i+1, m)
            can_c_array[i+1][m] = c1
        else:
            c1 = can_c_array[i+1][m] 
        if can_c_array[i+1][m-a[i]] == None:
            c2 = can_comp(i+1, m-a[i])
            can_c_array[i+1][m-a[i]] = c2
        else:
            c2 = can_c_array[i+1][m-a[i]]     
        return c1 or c2

for m in m_array:
    if can_comp(0, m):
        print('yes')
    else:
        print('no')

Submission status: Runtime Error

Solution code:

# -*- coding: utf-8 -*-
"""
Created on Wed May  2 21:28:06 2018
ALDS1_5a_r2  Time Limit Exceeded
@author: maezawa
"""

n = int(input())
a = list(map(int, input().split()))
q = int(input())
m_array = list(map(int, input().split()))
can_c_array = [[None for _ in range(q)] for _ in range(n)]

def can_comp(i, m): #i番目以降の数列をつかってmを作れるか？
    if m == 0:
        return True
    elif i == n:
        return False
    else:
        if can_c_array[i+1][m] == None:
            c1 = can_comp(i+1, m)
            can_c_array[i+1][m] = c1
        else:
            c1 = can_c_array[i+1][m] 
        if can_c_array[i+1][m-a[i]] == None:
            c2 = can_comp(i+1, m-a[i])
            can_c_array[i+1][m-a[i]] = c2
        else:
            c2 = can_c_array[i+1][m-a[i]]     
        return c1 or c2

for m in m_array:
    if can_comp(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(' ')))

def rec(i, sum, m):
    if i == n:
        if sum == m:
            return True
        else:
            return False

    if sum > m:
        return False

    rst = rec(i+1, sum, m) or rec(i+1, sum + A[i], m)
    return rst

for m in M:
    if rec(0, 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(' ')))

def rec(i, sum, m):
    if sum == m:
        return True
    elif sum > m:
        return False

    if i >= n:
        return False

    rst = rec(i+1, sum, m) or rec(i+1, sum + A[i], m)
    return rst

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

Submission status: Runtime Error

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

def rec(i, sum, m):
    if sum == m:
        return True
    elif sum > m:
        return False

    if i >= n:
        return False

    rst = rec(i+1, sum, m) or rec(i+1, sum + A[i], m)
    return rst

for m in M:
    if rec(0, 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(' ')))

def rec(i, m):
    if m == 0:
        return True
    if i >= n:
        return False

    rst = rec(i+1, m - A[i]) or rec(i+1, m)
    return rst

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

Submission status: Runtime Error

Solution code:

# -*- coding: utf-8 -*-
"""
Created on Wed May  2 21:28:06 2018
ALDS1_5a_r2  Time Limit Exceeded
@author: maezawa
"""

n = int(input())
a = list(map(int, input().split()))
q = int(input())
m_array = list(map(int, input().split()))
m_max = max(m_array)
can_c_array = [[None for _ in range(m_max+1)] for _ in range(n+1)]

def can_comp(i, m): #i番目以降の数列をつかってmを作れるか？
    #print(i,m)
    global n
    global a
    global can_c_array
    if m == 0:
        can_c_array[i][m] = True
        return True
    if i >= n:
        can_c_array[i][m] = False
        return False
    if can_c_array[i][m] != None:
        return can_c_array[i][m]
    elif can_comp(i+1,m):
        can_c_array[i][m] = True
    elif m-a[i] >= 0:
        if can_comp(i+1,m-a[i]):
            can_c_array[i][m] = True
    else:
        can_c_array[i][m] = False
    return can_c_array[i][m]

for m in m_array:
    if can_comp(0, m):
        print('yes')
    else:
        print('no')

Submission status: Runtime Error

Solution code:
n=int(input())
A=[int(x) for x in input().split()]

q=int(input())
M=[int(x) for x in input().split()]


def solve(m,AA):
    if len(AA)==0:
        return False
    if m in AA:
        return True
    else:
        for i in range(len(AA)):
            if solve(m-AA[i],AA[:i]+AA[i+1:]):
                return True
                break
        return False
    
    
for m in M:
    if solve(m,A):
        print('yes')
    else:
        print('no')

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000)  # set the maximum depth as 10000

n=int(input())
A=[int(x) for x in input().split()]

q=int(input())
M=[int(x) for x in input().split()]


def solve(i,m):
    global n
    global A
    
    if m==0:
        return True
    if i>=n:
        return False
    
    return solve(i+1,m) or solve(i+1,m-A[i])
    
    
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()))


def make_m(i, m):
    if m == 0:
        return True
    if i >= N or m < 0:
        return False

    return make_m(i+1, m) or make_m(i+1, m-A[i])


def main():
    q = int(input())
    m_lst = list(map(int, input().split()))

    for i in range(q):
        if make_m(0, m_lst[i]):
            print("yes")
        else:
            print("no")


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()))

# リストi番目以降の数字を足して、jが作れるかどうかを判定する関数を定義
def judge(i, j):
    if j == 0:
        return True
    elif i >= n:
        return False
    else:
        return judge(i + 1, j) or judge(i + 1, j - A[i])

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

Submission status: Runtime Error

Solution code:
import itertools as it
n = int(input())
A = list(map(int, input().split()))
q = int(input())
m = list(map(int, input().split()))
A.sort()

for h in range(q):
    cnt=0
    for i in range(n):
        for j in it.combinations(A,i):
            if sum(j)==m[h]:
                cnt = 1
    if cnt==1:
        print("yes")
    else:
        print("no")

Submission status: Runtime Error

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

for h in range(q):
    cnt=0
    for i in range(n):
        for j in it.combinations(A,i):
            if sum(j)==m[h]:
                cnt = 1
    if cnt==1:
        print("yes")
    else:
        print("no")

Submission status: Runtime Error

Solution code:
import itertools as itl

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

for mi in M:
    flg = False
    for i in range(len(A)):
        C = itl.combinations(A, i)
        for j in C:
            if sum(j) == mi:
                flg = True
                break
    if flg: print("yes")
    else: print("no")

Submission status: Runtime Error

Solution code:
import itertools as itl

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

for mi in M:
    flg = False
    for i in range(len(A)):
        C = itl.combinations_with_replacemant(A, i)
        for j in C:
            if sum(j) == mi:
                flg = True
                break
    if flg: print("yes")
    else: print("no")

Submission status: Runtime Error

Solution code:
import itertools as it
n = int(input())
A = list(map(int, input().split()))
q = int(input())
m = list(map(int, input().split()))
A.sort()

for h in range(q):
    cnt=0
    for i in range(n):
        for j in it.combinations(A,i):
            if sum(j)==m[h]:
                cnt = 1
                break
            
    if cnt==1:
        print("yes")
    else:
        print("no")

Submission status: Runtime Error

Solution code:
import itertools as it
n = int(input())
A = list(map(int, input().split()))
q = int(input())
m = list(map(int, input().split()))
A.sort()

for h in range(q):
    cnt=0
    for i in range(n):
        if cnt==1:
            break
        for j in it.combinations(A,i):
            if sum(j)==m[h]:
                cnt = 1
                break
            
    if cnt==1:
        print("yes")
    else:
        print("no")

Submission status: Runtime Error

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

for h in range(q):
    cnt=0
    for i in range(n):
        if cnt==1:
            break
        for j in it.combinations(A,i):
            if sum(j)==m[h]:
                cnt = 1
                break
            
    if cnt==1:
        print("yes")
    else:
        print("no")

Submission status: Runtime Error

Solution code:
import itertools as itl

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

for mi in M:
    flg = False
    for i in range(len(A)):
        C = itl.combinations(A, i)
        #print(C)
        for j in C:
            if sum(j) == mi:
                flg = True
                break
    if flg: print("yes")
    else: print("no")

Submission status: Runtime Error

Solution code:
import itertools as it
n = int(input())
A = list(map(int, input().split()))
q = int(input())
m = list(map(int, input().split()))
A.sort()

for h in range(q):
    cnt=0
    for i in range(n-1,0,-1):
        if cnt==1:
            break
        for j in it.combinations(A,i):
            if sum(j)==m[h]:
                cnt = 1
                break
    if cnt==1:
        print("yes")
    else:
        print("no")

Submission status: Runtime Error

Solution code:
import itertools as it
n = int(input())
A = list(map(int, input().split()))
q = int(input())
m = list(map(int, input().split()))
A.sort()

for h in range(q):
    cnt=0
    if m[q] > sum(A):
        break
    
    for i in range(n-1,0,-1):
        if cnt==1:
            break
        for j in it.combinations(A,i):
            if sum(j)==m[h]:
                cnt = 1
                break
    if cnt==1:
        print("yes")
    else:
        print("no")

Submission status: Runtime Error

Solution code:
import itertools as it
n = int(input())
A = list(map(int, input().split()))
q = int(input())
m = list(map(int, input().split()))
A.sort()

for h in range(q):
    cnt=0
    for i in range(n+1):
        for j in it.combinations(A,i):
            if sum(j)==m[h]:
                cnt = 1
    if cnt==1:
        print("yes")
    else:
        print("no")

Submission status: Runtime Error

Solution code:
import itertools
n=int(input())
A=[0 for i in range(n)]
A=input().split()
for i in range(n):
    A[i]=int(A[i])
q=int(input())
B=[0 for i in range(q)]
B=input().split()
C=[]
for i in range(q):
    B[i]=int(B[i])
l=[0,1]
p=[]
k=0
D=[]
for v in itertools.product(l,repeat=n):
    sum=0
    for i in range(n):
        if v[i]==1:
            sum=sum+A[i]
    D.append(sum)
    k+=1
for i in range(q):
    c=0
    for j in range(k):
        if B[i]==D[j] and c==0:
            C.append("yes")
            c+=1
    if c==0:
        C.append("no")
for i in range(q):
    print(C[i])

Submission status: Runtime Error

Solution code:
import itertools
n=int(input())
A=[0 for i in range(n)]
A=input().split()
for i in range(n):
    A[i]=int(A[i])
q=int(input())
B=[0 for i in range(q)]
B=input().split()
C=[]
for i in range(q):
    B[i]=int(B[i])
l=[0,1]
p=[]
k=0
D=[]
for v in itertools.product(l,repeat=n):
    sum=0
    for i in range(n):
        if v[i]==1:
            sum=sum+A[i]
    D.append(sum)
    k+=1
for i in range(q):
    c=0
    for j in range(k):
        if B[i]==D[j] and c==0:
            C.append("yes")
            c+=1
    if c==0:
        C.append("no")
for i in range(q):
    print(C[i])

Submission status: Runtime Error

Solution code:
import itertools
n=int(input())
A=[0 for i in range(n)]
A=input().split()
for i in range(n):
    A[i]=int(A[i])
q=int(input())
B=[0 for i in range(q)]
B=input().split()
C=[]
for i in range(q):
    B[i]=int(B[i])
l=[0,1]
p=[]
k=0
D=[]
for v in itertools.product(l,repeat=n):
    sum=0
    for i in range(n):
        if v[i]==1:
            sum=sum+A[i]
    D.append(sum)
    k+=1
for i in range(q):
    c=0
    j=0
    while c==0 and j<k:
        if B[i]==D[j] and c==0:
            C.append("yes")
            c+=1
        j+=1
    if c==0:
        C.append("no")
for i in range(q):
    print(C[i])

Submission status: Runtime Error

Solution code:
import itertools as itl

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

for mi in M:
    flg = False
    for i in range(n):
        C = itl.combinations(A, i+1)
        #print(C)
        for j in C:
            if sum(j) == mi:
                flg = True
                break
    if flg: print("yes")
    else: print("no")

Submission status: Runtime Error

Solution code:
import itertools as itl

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

for mi in M:
    flg = False
    for i in range(n):
        C = itl.combinations(A, i+1)
        #print(C)
        for j in C:
            if sum(j) == mi:
                flg = True
                break
        if flg: break
    if flg: print("yes")
    else: print("no")

Submission status: Runtime Error

Solution code:
import itertools as itl

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

for mi in M:
    flg = False
    for i in range(n):
        C = itl.combinations(A, i+1)
        for j in C:
            if sum(j) == mi:
                flg = True
                break
        if flg: break
    if flg: print("yes")
    else: print("no")

Submission status: Runtime Error

Solution code:
n=int(input())
A=[int(j) for j in input().split()]
m=int(input())
B=[int(j) for j in input().split()]
ans=[0 for i in range(n)]
C=[0 for i in range(n)]

for i in range(0,m+1):
    for j in range(i+1,m+1):
        if i==j:
            break
        ans=A[i]+A[j]
        C.append(ans)

for i in range(m):
    if B[i] in C:
        print("yes")
    else:
        print("no")

Submission status: Runtime Error

Solution code:
import itertools as itl

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

C = [False for i in range(q)]
for i in range(1, n+1):
    for j in itl.combinations(A, i):
        for mi in range(q):
            if sum(j) == M[mi]:
                C[mi] = True
                
for c in C:
    if c: print("yes")
    else: print("no")

Submission status: Runtime Error

Solution code:
#coading Shift_JIS
import itertools
n=int(input())
str=input().split()
A=[0 for i in range(n)]
for i in range(n):
	A[i]=int(str[i])
q=int(input())
m=[0 for i in range(q)]
str=input().split()
for i in range(q):
	m[i]=int(str[i])
	cn=n
while cn>0:
	l=list(itertools.combinations(A,cn))
	for j in l:
		sum=0
		for k in j:
			sum+=k
		for cnt in range(q):
			if sum==m[cnt]:
				m[cnt]=-1
	cn-=1
for i in range(q):
	if m[i]>0: print("no")
	else: print("yes")

Submission status: Runtime Error

Solution code:
#coading Shift_JIS
import numpy as np
n=int(input())
str=input().split()
A=np.zeros(n)
for i in range(n):
	A[i]=int(str[i])
q=int(input())
m=np.zeros(q)
str=input().split()

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

for i in range(q):
	m=int(str[i])
	if solve(0,m)==1: print("yes")
	else: print("no")

Submission status: Runtime Error

Solution code:
#coading Shift_JIS
n=int(input())
str=input().split()
A=[0 for i in range(n)]
for i in range(n):
	A[i]=int(str[i])
q=int(input())
m=[0 for i in range(q)]
str=input().split()

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

for i in range(q):
	m=int(str[i])
	if solve(0,m)==1: print("yes")
	else: print("no")

Submission status: Runtime Error

Solution code:

n=int(input())
list1=map(int,input().split())

q=int(input())
list2=map(int,input().split())

for i in range(len(list2)):
	list3=[]
	ok=0
	for j in range(len(list1)):
		if list2[i]<list1[j]:
			list3.append(list1[j])
	for k in range(len(list3)-1):
		if ok==1:
			break
		for l in range(1,len(list3)):
			if list3[k]+list3[l]==list2[i]:
				print("yes")
				ok=1
				break
	if ok==0:
		print("no")

Submission status: Runtime Error

Solution code:

import itertools

n=int(input())
list1=[int(x) for x in input().split()]

q=int(input())
list2=[int(x) for x in input().split()]

for i in range(len(list2)):
	ok=0
	for j in range(1,len(list1)):
		if ok==1:
			break
		list3=list(itertools.combinations(list1,j))
		for k in range(len(list3)):
			if ok==1:
				break
			if sum(list3[k])==list2[i]:
				print("yes")
				ok=1
				break
	if ok==0:
		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=[]*n
A=list(input().split())
for i in range(n):
    A[i]=int(A[i])
m=int(input())
B=list(input().split())
ans=[]
for i in range(m):
    B[i]=int(B[i])
    ans=solve(0,B[i])
    if ans==True:
        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=[]*n
A=list(input().split())
for i in range(n):
    A[i]=int(A[i])
m=int(input())
B=list(input().split())
ans=[]
for i in range(m):
    B[i]=int(B[i])
    ans=solve(0,B[i])
    if ans==True:
        print("yes")
    else:
        print("no")

Submission status: Runtime Error

Solution code:
import itertools as itl

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

for mi in M:
    flg = False
    for i in range(n):
        for j in itl.combinations(A, i+1):
            if sum(j) == mi:
                flg = True
                break
        if flg: break
    if flg: print("yes")
    else: print("no")

Submission status: Runtime Error

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

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

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

Submission status: Runtime Error

Solution code:
import itertools as itl

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

for mi in M:
    flg = False
    for i in range(n):
        for j in list(itl.combinations(A, i+1)):
            if sum(j) == mi:
                flg = True
                break
        if flg: break
    if flg: print("yes")
    else: print("no")

Submission status: Runtime Error

Solution code:
import itertools as itl

n = int(input())
A = list(map(int, input().split()))
q = int(input())
M = list(map(int, input().split()))
S = []

"""通る(ko)
for i in range(n):
    cmb = list(itl.combinations(A, i+1))
    for j in cmb:
        S.append(sum(j))

for m in M:
    flg = False
    for s in S:
        if s == m:
            flg = True
            break
    if flg:
        print("yes")
    else:
        print("no")

"""

for mi in M:
    flg = False
    for i in range(n):
        for j in list(itl.combinations(A, i+1)):
            if sum(j) == mi:
                flg = True
                break
        if flg: break
    if flg: 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(' ')))
S = [0 for i in range(n)]

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 M:
    if solve(0, j):
        print('yes')
    else:
        print('no')