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].

Solution in Rust (judge verdict: Accepted)

use std::collections::VecDeque;
use std::env;
//use std::fmt::Debug;
use std::io;
use std::io::prelude::*;
//use std::time::{Duration, Instant};

//===================================================
// MACROs need to be defined above the use place ... ?
macro_rules! dprintln {
    ($($x:expr),*) => {{
        if $crate::is_debug_mode() {
            let f = || {println!( $($x),*)};
            f()
        }
    }}
}

/// Backward For-Loop Helper
///  from -> to (exclude `to`)
///
/// # Examples
/// see tests::macro_backward()
///
#[macro_export]
macro_rules! backward_ho {
    ($from:expr, $to:expr) => {{
        (($to + 1)..($from + 1)).rev()
    }};
}
//===================================================

fn main() {
    // Initialize
    set_debug_mode(false);
    for arg in env::args() {
        if arg == "--debug" {
            set_debug_mode(true);
        }
    }
    dprintln!("[DebugMode] {}", "On");

    // Execute
    dprintln!("=================");
    dprintln!("= READ INPUT     ");
    let stdin = io::stdin();
    let input = BufReadInput::new(stdin.lock());
    dprintln!("[Input] \n{:?}", input);
    dprintln!("=================");
    dprintln!("= INTERPRET INPUT");
    let q = input.into_quiz();
    dprintln!("[Quiz] \n{:?}", q);
    dprintln!("=================");
    dprintln!("= SOLVE QUIZE    ");
    let a = q.solve();
    dprintln!("[Answer] \n{:?}", a);
    dprintln!("=================");
    dprintln!("= PRINT ANSWER   ");
    a.print();
    dprintln!("=================");
}

#[derive(Debug)]
struct Quiz {
    n: usize,    // N <= 20
    a: Vec<u32>, // 1 <= a[i] <= 2,000, length=N
    q: usize,    // q <= 200
    m: Vec<u32>, // 1 <= m[i] <= 2,000, length=q
}

#[derive(Debug)]
struct Answer {
    v: Vec<bool>, // true -> 'yes'
}

trait IntoQuiz {
    fn into_quiz(self) -> Quiz;
}

impl<I: Input> IntoQuiz for I {
    fn into_quiz(mut self) -> Quiz {
        let n: usize = self.parse_next().unwrap();
        let a: Vec<u32> = self.parse_next_vec(n).unwrap();
        let q: usize = self.parse_next().unwrap();
        let m: Vec<u32> = self.parse_next_vec(q).unwrap();
        Quiz { n, a, q, m }
    }
}

impl Quiz {
    fn solve(self) -> Answer {
        let mut a = self.a;
        let m = self.m;

        a.sort();

        let mut v = Vec::new();

        for em in m {
            let b = Quiz::can_make(em, &a);
            v.push(b);
        }
        
        Answer{ v }
    }

    fn can_make(target: u32, sorted_a: &Vec<u32>) -> bool {
        dprintln!("can_make {} by {:?} ?", target, sorted_a);
        let mut a = sorted_a.clone();
        if let Some(max_a) = a.pop() {
            let remain_a = a;

            if max_a == target {
                dprintln!(" {} = {}", max_a, target);
                true
            } else if max_a > target {
                dprintln!(" {} > {}", max_a, target);
                Quiz::can_make(target, &remain_a)
            } else {
                dprintln!(" {} < {}", max_a, target);
                Quiz::can_make(target-max_a, &remain_a)
                || Quiz::can_make(target, &remain_a)
            }
        } else {
            dprintln!(" a is nil");
            false
        }
    }
}

impl Answer {
    fn print(self) {
        for b in self.v {
            if b {
                println!("yes");
            } else {
                println!("no");
            }
        }
    }
}

// =====================================================
// =
// =====================================================

//==================================================
// Stdin Reader
#[derive(Debug)]
pub enum Token {
    Word(String),
    LineBreak,
}

impl Token {
    pub fn is_word(&self) -> bool {
        match *self {
            Token::Word(ref _x) => true,
            _ => false,
        }
    }
}

#[derive(Debug)]
pub struct BufReadInput<R: BufRead> {
    input: R,
    tokens: VecDeque<Token>,
}

pub trait Input {
    fn read_next_word(&mut self) -> Option<Token>;

    fn parse_next<T>(&mut self) -> Option<T>
    where
        T: std::str::FromStr,
    {
        if let Some(Token::Word(str)) = self.read_next_word() {
            match str.parse() {
                Ok(x) => Some(x),
                _ => None,
            }
        } else {
            None
        }
    }

    fn parse_next_vec<T>(&mut self, size: usize) -> Option<Vec<T>>
    where
        T: std::str::FromStr,
    {
        let mut v = Vec::new();

        for _i in 0..size {
            if let Some(t) = self.parse_next() {
                v.push(t);
            } else {
                return None;
            }
        }

        Some(v)
    }

    fn parse_next_vec2<T>(&mut self, size2: usize, size1: usize) -> Option<Vec<Vec<T>>>
    where
        T: std::str::FromStr,
    {
        let mut v = Vec::new();

        for _i in 0..size2 {
            if let Some(t) = self.parse_next_vec(size1) {
                v.push(t);
            } else {
                return None;
            }
        }

        Some(v)
    }

    fn parse_all_remaining_into_vec<T>(&mut self) -> Vec<T>
    where
        T: std::str::FromStr,
    {
        let mut v = Vec::new();

        loop {
            match self.parse_next() {
                Some(x) => v.push(x),
                None => return v,
            }
        }
    }
}
impl<R: BufRead> BufReadInput<R> {
    pub fn new(input: R) -> Self {
        let tokens = VecDeque::new();

        BufReadInput { input, tokens }
    }
}

impl<R: BufRead> Input for BufReadInput<R> {
    fn read_next_word(&mut self) -> Option<Token> {
        loop {
            match self.tokens.pop_front() {
                None => {
                    // Read Input
                    let mut line = String::new();
                    let n = self.input.read_line(&mut line).expect("Read Error.");
                    if n == 0 {
                        return None;
                    }
                    let mut line_tokens = tokenaize(line);
                    self.tokens.append(&mut line_tokens);
                }
                Some(Token::LineBreak) => (),
                x => return x,
            }
        }
    }

}

//TODO: Make CaseIterator
// ex) 
//  let mut input: Input ...
//  let mut iter: CaseIterator<Case=Command> = input.rest_into_cases(|i|   
//    let cmd:String = i.parse_next().expect("Parse error cmd");
//    let num:u32    = i.parse_next().expect("Parse error num");
//
//    Command::new(cmd, num)
//  );
//  let case1 = iter.next();
//  let case2 = iter.next();
//  ...


fn tokenaize(str: String) -> VecDeque<Token> {
    let mut v = VecDeque::new();

    for w in str.split_whitespace() {
        v.push_back(Token::Word(w.to_string()));
    }
    v.push_back(Token::LineBreak);
    v
}

//==================================================
// Utility Functions
pub fn concat_vec_to_string<T>(v: &Vec<T>) -> String
where
    T: std::fmt::Display,
{
    let mut string = String::with_capacity(v.len() * 2);

    if let Some((h, t)) = v.split_first() {
        string = h.to_string();

        for obj in t {
            string.push(' ');
            string.push_str(&obj.to_string());
        }
    }

    return string;
}

//==================================================
// Debug Switch
static mut S_DEBUG_MODE: bool = false;
fn is_debug_mode() -> bool {
    unsafe { S_DEBUG_MODE }
}

fn set_debug_mode(mode: bool) {
    unsafe {
        S_DEBUG_MODE = mode;
    }
}

//==================================================
// Usage
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn macro_backward() {
        let mut v = Vec::new();
        for i in backward_ho!(5, 1) {
            v.push(i);
        }
        assert_eq!(vec![5, 4, 3, 2], v);
    }
}