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: Time Limit Exceeded

Solution code:
import sys


def search_combination(elements, target):
    if len(elements) == 0:
        if target == 0:
            return True
        else:
            return False

    if target < 0:
        return False

    return search_combination(elements[1:], target - elements[0]) or search_combination(elements[1:], target)

if __name__ == "__main__":
    lines = sys.stdin.readlines()
    elements = [int(x) for x in lines[1].strip().split(" ")]
    targets = [int(x) for x in lines[3].strip().split(" ")]

    for target in targets:
        result = "yes" if search_combination(elements, target) else "no"
        print(result)

Submission status: Time Limit Exceeded

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

global n
n = int(input())
st = input()
A = list(map(int,st.split()))
# dont use q(num of ans patarn) dumping!
input()
st = input()
ans = list(map(int,st.split()))

for x in ans:
    if(solve(0,x,A) == True):
        print('yes')
    else:
        print('no')

Submission status: Time Limit Exceeded

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

global n
n = int(input())
st = input()
A = list(map(int,st.split()))
# dont use q(num of ans patarn) dumping!
input()
st = input()
ans = list(map(int,st.split()))

for x in ans:
    if(solve(0,x,A) == True):
        print('yes')
    else:
        print('no')

Submission status: Time Limit Exceeded

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

global n
global A
n = int(input())
st = input()
A = list(map(int,st.split()))
input()     # don't use q(num of ans pattern) dumping!
st = input()
ans = list(map(int,st.split()))

for x in ans:
    if(solve(0,x) == True):
        print('yes')
    else:
        print('no')

Submission status: Time Limit Exceeded

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

global n
global A
n = int(input())
st = input()
A = list(map(int,st.split()))
A.sort(reverse = True)
input()     # don't use the num of ans pattern. dumping!
st = input()
ans = list(map(int,st.split()))

for x in ans:
    if(solve(0,x) == True):
        print('yes')
    else:
        print('no')

Submission status: Time Limit Exceeded

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

global n
global A
n = int(input())
st = input()
A = list(map(int,st.split()))
#A.sort(reverse = True)
input()     # don't use the num of ans pattern. dumping!
st = input()
ans = list(map(int,st.split()))

for x in ans:
    if(solve(0,x) == True):
        print('yes')
    else:
        print('no')

Submission status: Time Limit Exceeded

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

global n
global A
n = int(input())
A = [ int( val ) for val in input().rstrip().split( ' ' ) ]
#A.sort(reverse = True)
input()     # don't use the num of ans pattern. dumping!
ans = [ int( val ) for val in input().rstrip().split( ' ' ) ]
for x in ans:
    if(solve(0,x) == True):
        print('yes')
    else:
        print('no')

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8
#1_5_A
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 = list(map(int, input().split()))
q = int(input())
m = list(map(int, input().split()))

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

Submission status: Time Limit Exceeded

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


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


def main():
    n = int(input())
    a = [int(x) for x in input().split()]
    q = int(input())
    for m in (int(y) for y in input().split()):
        if solve(a, n, 0, m):
            print("yes")
        else:
            print("no")


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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
    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: Time Limit Exceeded

Solution code:
n = int(raw_input())
A = map(int, raw_input().strip().split(' '))
q = int(raw_input())
M = map(int, raw_input().strip().split(' '))

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

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

Submission status: Time Limit Exceeded

Solution code:
n = int(raw_input())
A = map(int, raw_input().strip().split(' '))
q = int(raw_input())
M = map(int, raw_input().strip().split(' '))

memo = []
for i in range(len(A)+1):
    memo.append([None]*2000)

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

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

Submission status: Time Limit Exceeded

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


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

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

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

Submission status: Time Limit Exceeded

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


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

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

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

Submission status: Time Limit Exceeded

Solution code:
def solve(A, i, m, n):
    """Return if it is possible to make value m with some elements in A.

    n is length of A.
    i is index.
    Using Divide-and-Conquer method.
    """

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


n = int(input())

A = list(map(int, input().split()))

x = input()

M = list(map(int, input().split()))

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

Submission status: Time Limit Exceeded

Solution code:
def solve(A, i, m, n):
    """Return if it is possible to make value m with some elements in A.

    n is length of A.
    i is index.
    Using Divide-and-Conquer method.
    """

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


n = int(input())

A = list(map(int, input().split()))

x = input()

M = list(map(int, input().split()))

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

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
N = int(input())
A = [int(i) for i in input().split()]
q = int(input())
m = [int(i) for i in input().split()]
MAX_LENGTH = len(A)
#i番目以降の値を使ってmを作ればTrueを返す
def fullSearch(i, m):
    #終了条件
    if m == 0:
        return True
    elif i >= MAX_LENGTH:
        return False
    else:
        #全探索かっこわるい
        return fullSearch(i + 1, m) or fullSearch(i + 1, m - A[i])
 
for i in m:
    print("yes" if fullSearch(0, i) else "no")

Submission status: Time Limit Exceeded

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


def check(m, bin):
    global a
    s = 0
    for b in a:
        if bin & 1:
            s += b
            if s > m:
                return False
        bin >>= 1
    return s == m


for m in ms:
    for i in range(1, 2 ** n - 1):
        if check(m, i):
            print('yes')
            break
    else:
        print('no')

Submission status: Time Limit Exceeded

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

cache = {}


def sum_a(tmp, b, i=0, mask=1):
    global a, cache
    if not b:
        return tmp
    elif b in cache:
        return tmp + cache[b]
    elif b & mask:
        tmp += a[i]
        new_b = b ^ mask
    else:
        new_b = b
    result = sum_a(tmp, new_b, i + 1, mask << 1)
    cache[b] = result
    return result


for m in ms:
    for b in range(1, 2 ** n):
        if sum_a(0, b) == m:
            print('yes')
            break
    else:
        print('no')

Submission status: Time Limit Exceeded

Solution code:
n = input()
 
A = map(int, raw_input().split())
  
q = input()
  
M = map(int, raw_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):
    print "yes"
  else:
    print "no"

Submission status: Time Limit Exceeded

Solution code:
n = input()
  
A = map(int, raw_input().split())
   
q = input()
   
M = map(int, raw_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):
    print "yes"
  else:
    print "no"

Submission status: Time Limit Exceeded

Solution code:
n = input()

A = map(int, raw_input().split())

q = input()

m = map(int, raw_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])
  if res == True:
    return True
  else:
    return False

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

Submission status: Time Limit Exceeded

Solution code:
n = input()

A = map(int, raw_input().split())

q = input()

m = map(int, raw_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])
  if res == True:
    return True
  else:
    return False

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

Submission status: Time Limit Exceeded

Solution code:
n = input()

A = map(int, raw_input().split())

q = input()

m = map(int, raw_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])
  if res == True:
    return True
  else:
    return False

for i in xrange(q):
  if solve(0, m[i]) == True:
    print "yes"
  else:
    print "no"

Submission status: Time Limit Exceeded

Solution code:
n = int( raw_input() )
A = map( int , raw_input().strip().split(' ') )
q = int( raw_input() )
mi = map( int , raw_input().strip().split(' ') )

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

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

Submission status: Time Limit Exceeded

Solution code:
n = int( raw_input() )
A = map( int , raw_input().strip().split(' ') )
q = int( raw_input() )
mi = map( int , raw_input().strip().split(' ') )

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

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

Submission status: Time Limit Exceeded

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

n = input()

A = map(int, raw_input().split())

q = input()

m = map(int, raw_input().split())


for i in xrange(q):
  if solve(0, m[i]) == True:
    print "yes"
  else:
    print "no"

Submission status: Time Limit Exceeded

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


n = input()
A = map(int, raw_input().split())
p = input()
m = map(int, raw_input().split())
for i in range(p):
  if solve(0, m[i], A, n):
    print 'yes'
  else:
    print 'no'

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

Solution code:
n = input()

A = map(int, raw_input().split())

q = input()

m = map(int, raw_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, m[i]) == True:
        print "yes"
    else:
        print "no"

Submission status: Time Limit Exceeded

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

def henkan(x):
    s = [0]*n
    i = 0
    while x != 0:
        s[i] = x%2
        x /= 2
        i += 1
    return s

for k in range(0, q):
    frag = 0
    for i in range(1, 2**n-1):
        sum = 0
        s = henkan(i)
        for j in range(0, len(s)):
            if s[j] == 1:
                sum += A[j]
        if sum == m[k]:
            print "yes"
            frag = 1
            break
    if frag == 0:
        print "no"

Submission status: Time Limit Exceeded

Solution code:
n = int(raw_input())
A = map(int, raw_input().split())
q = int(raw_input())
m = map(int, raw_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 j in range(0, q):
    if solve(0, m[j]):
        print "yes"
    else:
        print "no"

Submission status: Time Limit Exceeded

Solution code:
def check(m,g,A,n):
    
    for i in range(n):
        gg = g + A[n-i-1]
        if m == gg:
            return True
        elif n == 0 or m < gg:
            continue
        if check(m,gg,A,n-i-1) == True:
            return True
    return False
        

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

for mi in m:
    if check(mi,0,A,n):
        print("yes")
    else:
        print("no")

Submission status: Time Limit Exceeded

Solution code:
n = input()

A = map(int, raw_input().split())

q = input()

m = map(int, raw_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 xrange(len(m)):
    if solve(0, m[i]) == True:
        print "yes"
    else:
        print "no"

Submission status: Time Limit Exceeded

Solution code:
n = input()

A = map(int, raw_input().split())

q = input()

m = map(int, raw_input().split())

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

for i in xrange(len(m)):
    if solve(0, m[i]) == True:
        print "yes"
    else:
        print "no"

Submission status: Time Limit Exceeded

Solution code:
n = input()

A = map(int, raw_input().split())

q = input()

m = map(int, raw_input().split())

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

for i in xrange(q):
    if solve(0, m[i]) == True:
        print "yes"
    else:
        print "no"

Submission status: Time Limit Exceeded

Solution code:
n1 = input()
A = map(int, raw_input().split())

n2 = input()
q = map(int, raw_input().split())

list = set()

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

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

Submission status: Time Limit Exceeded

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


n = input()
A = map(int, raw_input().split())
p = input()
m = map(int, raw_input().split())
for i in range(p):
  if solve(0, m[i]):
    print 'yes'
  else:
    print 'no'

Submission status: Time Limit Exceeded

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 = input()
a = map(int,raw_input().split())
q = input()
p = map(int,raw_input().split())

for i in xrange(0,q):
    if solve(0,p[i]):
        print "yes"
    else:
        print "no"

Submission status: Time Limit Exceeded

Solution code:
n = input()
A = map(int, raw_input().split())
q = input()
M = map(int, raw_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):
      print 'yes'
    else:
      print 'no'

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
class ExhaustiveSearch:
    def solve(self, n, a, q, m):
        for mm in m:
            if self.es(0, mm):
                print("yes")
            else:
                print("no")
    def es(self, i, m): 
        if m == 0:
            return True
        if i >= n:
            return False
        res = self.es(i + 1, m) or self.es(i + 1, m - a[i])
        return res

n = int(input())
a = list(map(int, input().rstrip().split(" ")))
q = int(input())
m = list(map(int, input().rstrip().split(" ")))
x = ExhaustiveSearch()
x.solve(n, a, q, m)

Submission status: Time Limit Exceeded

Solution code:
class ExhaustiveSearch:
    def solve(self, n, a, q, m):
        for mm in m:
            if self.es(0, mm):
                print("yes")
            else:
                print("no")
    def es(self, i, m): 
        if m == 0:
            return True
        if i >= n:
            return False
        res = self.es(i + 1, m) or self.es(i + 1, m - a[i])
        return res

if __name__ == '__main__':
    n = int(input())
    a = [int(i) for i in input().rstrip().split(" ")]
    q = int(input())
    m = [int(i) for i in input().rstrip().split(" ")]
    x = ExhaustiveSearch()
    x.solve(n, a, q, m)

Submission status: Time Limit Exceeded

Solution code:
n = int(raw_input())
A = map(int, raw_input().split())
q = int(raw_input())
M = map(int, raw_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

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

main()

Submission status: Time Limit Exceeded

Solution code:
n = int(raw_input())
A = map(int, raw_input().split())
q = int(raw_input())
M = map(int, raw_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

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

main()

Submission status: Time Limit Exceeded

Solution code:
def exhaustive_search(arr, num):
    for bit in range(1 << len(arr)):
        total = 0
        for i in range(len(arr)):
            if 1 & (bit >> i) == 1:
                total += arr[i]
            if total == num:
                return "yes"
    return "no"

input()
A = [int(x) for x in input().split()]
input()
ms = [int(x) for x in input().split()]
for m in ms:
    print(exhaustive_search(A, m))

Submission status: Time Limit Exceeded

Solution code:
# coding: UTF-8
def solve(i, goal, A):
    # to decrese goal
    # then return boolean
    if goal == 0:
        return True
    elif i == n:
        return False
    else:
        return solve(i + 1, goal, A) or solve(i + 1, goal - A[i], A)

n = input() # parts total
A = map (int, raw_input().split()) # parts
q = input() # goal total
M = map (int, raw_input().split()) # goal

for j in range(q):
    if solve(0, M[j], A): # default
        print "yes"
    else:
        print "no"

Submission status: Time Limit Exceeded

Solution code:
# coding: UTF-8
def solve(i, goal):
    # to decrese goal
    # then return boolean
    if goal == 0:
        return True
    elif i == n:
        return False
    else:
        return solve(i + 1, goal) or solve(i + 1, goal - A[i])

n = input() # parts total
A = map (int, raw_input().split()) # parts
q = input() # goal total
M = map (int, raw_input().split()) # goal

for j in range(q):
    if solve(0, M[j]): # default
        print "yes"
    else:
        print "no"

Submission status: Time Limit Exceeded

Solution code:
# coding: UTF-8
def solve(i, goal):
    # to decrese goal
    # then return boolean
    if goal == 0:
        return True
    elif i == n:
        return False
    else:
        return solve(i + 1, goal) or solve(i + 1, goal - A[i])

n = input() # parts total
A = map (int, raw_input().split()) # parts
q = input() # goal total
M = map (int, raw_input().split()) # goal

for j in xrange(q):
    if solve(0, M[j]): # default
        print "yes"
    else:
        print "no"

Submission status: Time Limit Exceeded

Solution code:
def solve(i, goal):
    # to decrese goal
    # then return boolean
    if goal == 0:
        return True
    elif i == n:
        return False
    else:
        return solve(i + 1, goal) or solve(i + 1, goal - A[i])

n = input() # parts total
A = map (int, raw_input().split()) # parts
q = input() # goal total
M = map (int, raw_input().split()) # goal

for j in xrange(q):
    goal = M[j]
    if solve(0, goal): # default
        print "yes"
    else:
        print "no"

Submission status: Time Limit Exceeded

Solution code:
import sys

def exhaustiveSearch(A, m):
    for i in range(len(A)):
        if m == A[i]:
            return True
        elif m > A[i]:
            if exhaustiveSearch(A[:i] + A[i + 1:], m -  A[i]):
                return True
    return False

if __name__ == "__main__":
    lines = sys.stdin.readlines()
    A = map(int, lines[1].split())
    M = map(int, lines[3].split())

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

Submission status: Time Limit Exceeded

Solution code:
import sys

def exhaustiveSearch(A, m):
    if len(A) == 1 and A[0] != m:
        return False
    elif A[0] == m:
        return True
    return exhaustiveSearch(A[1:], m) or exhaustiveSearch(A[1:], m - A[0])

if __name__ == "__main__":
    lines = sys.stdin.readlines()
    A = map(int, lines[1].split())
    M = map(int, lines[3].split())

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

Submission status: Time Limit Exceeded

Solution code:
import sys

def exhaustiveSearch(A, m):
    if len(A) == 1 and A[0] != m or m < 0:
        return False
    elif A[0] == m:
        return True
    return exhaustiveSearch(A[1:], m) or exhaustiveSearch(A[1:], m - A[0])

if __name__ == "__main__":
    lines = sys.stdin.readlines()
    A = map(int, lines[1].split())
    M = map(int, lines[3].split())

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

Submission status: Time Limit Exceeded

Solution code:
import sys

def exhaustiveSearch(A, m, i):
    if A[i] == m:
        return True
    elif i == len(A) - 1:
        return False

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

if __name__ == "__main__":
    lines = sys.stdin.readlines()
    A = map(int, lines[1].split())
    M = map(int, lines[3].split())

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

Submission status: Time Limit Exceeded

Solution code:
import sys

def exhaustiveSearch(A, m, i):
    if A[i] == m:
        return True
    elif i == len(A) - 1 or m < 0:
        return False

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

if __name__ == "__main__":
    lines = sys.stdin.readlines()
    A = map(int, lines[1].split())
    M = map(int, lines[3].split())

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

Submission status: Time Limit Exceeded

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

A = sorted(A)

def sum_A(A, n, pos, sum, m, res):
    if res[0] == "yes":
        return 0
    if sum + A[pos] == m:
        res[0] = "yes"
        return 0
    if pos + 1 > n - 1:
        return 0
    sum_A(A, n, pos + 1, sum + A[pos], m, res)
    sum_A(A, n, pos + 1, sum, m, res)

for i, m_i in enumerate(m):
    sum = 0
    res = ["no"]
    sum_A(A, n, 0, sum, m_i, res)
    print res[0]

Submission status: Time Limit Exceeded

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

A = sorted(A)
#m = sorted(m)

def sum_A(A, n, pos, sum, m, res):
    if res[0] == "yes":
        return 0
    if sum + A[pos] > m:
        return 0
    if sum + A[pos] == m:
        res[0] = "yes"
        return 0
    if pos + 1 > n - 1:
        return 0
    sum_A(A, n, pos + 1, sum + A[pos], m, res)
    sum_A(A, n, pos + 1, sum, m, res)

for i, m_i in enumerate(m):
    sum = 0
    res = ["no"]
    sum_A(A, n, 0, sum, m_i, res)
    print res[0]

Submission status: Time Limit Exceeded

Solution code:
import time

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

if __name__ == '__main__':
    n = int(raw_input())
    A = map(int, raw_input().split())
    q = int(raw_input())
    m = map(int, raw_input().split())

    start = time.time()
    for m_i in m:
        print "yes" if solve(0, m_i) else "no"
    end = time.time()
    #print end - start

Submission status: Time Limit Exceeded

Solution code:
import time
from array import array

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

if __name__ == '__main__':
    n = int(raw_input())
    A = array('I', map(int, raw_input().split()))
    q = int(raw_input())
    m = array('I', map(int, raw_input().split()))

    start = time.time()
    for m_i in m:
        print "yes" if solve(0, m_i) else "no"
    end = time.time()
    #print end - start

Submission status: Time Limit Exceeded

Solution code:
import time

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

if __name__ == '__main__':
    n = int(raw_input())
    A = map(int, raw_input().split())
    q = int(raw_input())
    m = map(int, raw_input().split())

    #start = time.time()
    for m_i in m:
        print "yes" if solve(0, m_i) else "no"
    #end = time.time()
    #print end - start

Submission status: Time Limit Exceeded

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

if __name__ == '__main__':
    n = int(raw_input())
    A = map(int, raw_input().split())
    q = int(raw_input())
    m = map(int, raw_input().split())

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