Buggy python submission (verdict: Runtime Error):
import numpy as np

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

def deci_to_bin_list(num, n):
    out = np.zeros(n, dtype='int')
    for i in range(n-1, -1, -1):
        denom = 2**i
        out[i] = num // denom
        num = num % denom
    return out

def sums_to_num(A, num):
    n = len(A)
    for cnt in range(2**n - 1):
        selected = deci_to_bin_list(cnt, n)
        if np.sum(A*selected) == num:
            return 'yes'
    return 'no'

for num in m:
    print(sums_to_num(A, num))    


Fixed submission:
# used DP

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


dp = [[None]*(max(M)+1) for i in range(n)]

# we refer to the list dp only when i = 0, 1, ... , n-1 and m >= 0.
def solve(i, m):    
    if i >= n:
        return m == 0
    
    elif m < 0:
        return False
    
    elif dp[i][m] is not None:
        return dp[i][m]
    
    else:
        if m == 0:
            dp[i][m] = True
        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 num in M:
    print('yes' if solve(0, num) else 'no')

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
A = list(map(int, input().split() ))
q = int(input())
m = list(map(int, input().split()))

def deci_to_bin_list(num, n):
    out = [0]*n
    for i in range(n-1, -1, -1):
        denom = 2**i
        out[i] = num // denom
        num = num % denom
    return out

def sum_selected(A, selected):
    sum = 0
    for a,b in zip(A, selected):
        sum += a*b
    return sum

def sums_to_num(A, num):
    n = len(A)
    for cnt in range(2**n - 1):
        selected = deci_to_bin_list(cnt, n)
        if sum_selected(A, selected) == num:
            return 'yes'
    return 'no'

for num in m:
    print(sums_to_num(A, num))    


Fixed submission:
# used DP

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


dp = [[None]*(max(M)+1) for i in range(n)]

# we refer to the list dp only when i = 0, 1, ... , n-1 and m >= 0.
def solve(i, m):    
    if i >= n:
        return m == 0
    
    elif m < 0:
        return False
    
    elif dp[i][m] is not None:
        return dp[i][m]
    
    else:
        if m == 0:
            dp[i][m] = True
        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 num in M:
    print('yes' if solve(0, num) else 'no')

Buggy python submission (verdict: Time Limit Exceeded):
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
    elif i >= n:
        return False
    else:
        return solve(i+1, m) or solve(i+1, m - A[i])
    

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



Fixed submission:
# used DP

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


dp = [[None]*(max(M)+1) for i in range(n)]

# we refer to the list dp only when i = 0, 1, ... , n-1 and m >= 0.
def solve(i, m):    
    if i >= n:
        return m == 0
    
    elif m < 0:
        return False
    
    elif dp[i][m] is not None:
        return dp[i][m]
    
    else:
        if m == 0:
            dp[i][m] = True
        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 num in M:
    print('yes' if solve(0, num) else 'no')