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
M
i
.
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 (
M
i
) are given.
Output
For each question
M
i
, print
yes
or
no
.
Constraints
n ≤ 20
q ≤ 200
1 ≤ elements in A ≤ 2000
1 ≤ M
i
≤ 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].

Submitted solutions:

code_snippet: # include <stdio.h>

static int N, A[50];

static int solve(int i, int m)
{
    if (m == 0)
        return 1;
    if (i >= N)
        return 0;

    /* A[i]を使用するか、しないか */
    int res = (solve(i + 1, m) || solve(i + 1, m - A[i]));
    return res;
}

int main(void)
{
    int q, M, i;

    scanf("%d", &N);
    for (i = 0; i < N; i++)
        scanf("%d", &A[i]);

    scanf("%d", &q);
    for (i = 0; i < q; i++)
    {
        scanf("%d", &M);
        if (solve(0, M))
            printf("yes\n");
        else
            printf("no\n");
    }
    return 0;
}
accuracy: 1
time_ms: 350
memory_kb: 2908
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;

    vector<int> A(n);
    for (int i = 0; i < n; i++) cin >> A[i];
    
    unordered_set<int> possible;
    int total = 1 << n;
    
    for (int bit = 0; bit < total; bit++) {
        int sum = 0;
        for (int i = 0; i < n; i++) {
            if (bit & (1 << i)) sum += A[i];
        }
        possible.insert(sum);
    }

    int q;
    cin >> q;
    while (q--) {
        int m;
        cin >> m;
        if (possible.count(m)) {
            cout << "yes\n";
        } else {
            cout << "no\n";
        }
    }

    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3480
final_rank: 2

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define int long long
int n;
int a[30];
bool rec(int d,int t){
  if(t==0) return true;
  if(d==n||t<0) return false;
  return rec(d+1,t)||rec(d+1,t-a[d]);
}
signed main(){
  cin>>n;
  for(int i=0;i<n;i++) cin>>a[i];
  int q;
  cin>>q;
  int m[q];
  for(int i=0;i<q;i++) cin>>m[i];
  for(int i=0;i<q;i++) cout<<(rec(0,m[i])?"yes":"no")<<endl;
  return 0;
}
accuracy: 1
time_ms: 420
memory_kb: 3360
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;

int n;
vector<int> A;

bool solve(int i, int M) {
    if (M == 0) return true;

    if (i > n) return false;

    bool res = solve(i + 1, M) || solve(i + 1, M - A[i]);  
    return res;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n;
    A.resize(n);
    for (int i = 0; i < n; i++) cin >> A[i];

    int q; cin >> q;
    while (q--) {
        int m; cin >> m;
        if (solve(0, m)) cout << "yes\n";
        else cout << "no\n";
    }
}
accuracy: 1
time_ms: 680
memory_kb: 3460
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    if(!(cin >> n)) return 0;
    vector<long long> A(n);
    for(int i = 0; i < n; ++i) cin >> A[i];
    int q; cin >> q;
    vector<long long> M(q);
    for(int i = 0; i < q; ++i) cin >> M[i];

    unordered_set<long long> sums;
    sums.reserve(1 << min(n, 20));
    long long total = 1LL << n;
    for(long long mask = 0; mask < total; ++mask){
        long long s = 0;
        for(int j = 0; j < n; ++j){
            if((mask >> j) & 1LL) s += A[j];
        }
        sums.insert(s);
    }

    for(int i = 0; i < q; ++i){
        cout << (sums.count(M[i]) ? "yes" : "no") << '\n';
    }
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 11276
final_rank: 18

code_snippet: #include<iostream>

using namespace std;

bool solve(int i, int X, int A[], int n){
    if(X==0){
        return true;
    }
    else if(X<0){
        return false;
    }
    if(i>=n){
        return false;
    }
    bool res = solve(i+1, X, A, n) || solve(i+1, X-A[i], A, n);
    return res;
}

int main(){
    int n;
    cin >> n;
    int A[n];
    for(int i=0;i<n;i++){
        cin >> A[i];
    }
    
    int q;
    cin >> q;
    int M[q];
    for(int j=0;j<q;j++){
        cin >> M[j];
    }
    
    for(int k=0;k<q;k++){
        bool y = solve(0, M[k], A, n);
        if(y==true){
            cout << "yes" << endl;
        }
        else cout << "no" << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 340
memory_kb: 3364
final_rank: 3

code_snippet: #include<iostream>

using namespace std;

bool solve(int i, int X, int A[], int n){
    if(X==0){
        return true;
    }
    else if(X<0){
        return false;
    }
    if(i>=n){
        return false;
    }
    bool res = solve(i+1, X, A, n) || solve(i+1, X-A[i], A, n);
    return res;
}

int main(){
    int n;
    cin >> n;
    int A[n];
    for(int i=0;i<n;i++){
        cin >> A[i];
    }
    
    int q;
    cin >> q;
    int M[q];
    for(int j=0;j<q;j++){
        cin >> M[j];
    }
    
    for(int k=0;k<q;k++){
        bool y = solve(0, M[k], A, n);
        if(y==true){
            cout << "yes" << endl;
        }
        else cout << "no" << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 340
memory_kb: 3432
final_rank: 6

code_snippet: #include <iostream>
#include <vector>

// グローバル変数として扱うことで、再帰関数に毎回渡す手間を省きます
int n;
std::vector<int> A;

// 再帰関数: `index`番目の要素以降を使って `target` を作れるか判定する
bool solve(int index, int target) {
    // ベースケース1: `target` が0になったら、指定の和が作れたということ
    if (target == 0) {
        return true;
    }
    // ベースケース2: `index` が `n` (数列の末尾) に到達し、かつ `target` が0でない場合
    // これ以上要素がないので `target` を作れない
    if (index == n) {
        return false;
    }

    // 再帰ステップ: 以下の2つのケースを試す
    // 1. `A[index]` を選ばずに、次の要素 `A[index + 1]` 以降で `target` を作る
    // 2. `A[index]` を選び、残りの要素 `A[index + 1]` 以降で `target - A[index]` を作る
    return solve(index + 1, target) || solve(index + 1, target - A[index]);
}

int main() {
    std::cin >> n;
    A.resize(n); 
    for (int i = 0; i < n; ++i) {
        std::cin >> A[i];
    }

    int q;
    std::cin >> q;
    for (int i = 0; i < q; ++i) {
        int m;
        std::cin >> m;
        if (solve(0, m)) { 
            std::cout << "yes" << std::endl;
        } else {
            std::cout << "no" << std::endl;
        }
    }

    return 0;
}
accuracy: 1
time_ms: 400
memory_kb: 3444
final_rank: 11

code_snippet: #include <iostream>
using namespace std;

bool solve(long long i, long long M, long long A[], long long n){
    if(M==0) return true;
    else if(M<0) return false;
    if(i >= n) return false;
    bool res = solve(i+1, M, A, n) || solve(i+1, M-A[i], A, n);
    return res;
}

int main(){
    long long n;
    cin >> n;
    long long A[n];
    for(long long i=0; i<n; ++i){
        cin >> A[i];
    }
    long long q;
    cin >> q;
    for(long long i=0; i<q; ++i){
        int m;
        cin >> m;
        if(solve(0, m, A, n)) cout << "yes\n";
        else cout << "no\n";
    }
    
    return 0;
}
accuracy: 1
time_ms: 230
memory_kb: 3584
final_rank: 4

code_snippet: #include <iostream>
#include <vector>
using namespace std;

bool solve(int i,int M,int n,vector<int>& A){
    if (M==0){
        return true;
    }
    else if (M<0){
        return false;
    }
    if (i>=n){
        return false;
    }
    int res = solve(i+1,M,n,A) || solve(i+1,M-A[i],n,A);
    return res;
}

int main(){
    int n,q;
    cin >> n;
    vector<int> A(n);
    for (int i=0;i<n;i++){
        cin >> A[i];
    }
    cin >> q;
    vector<int> m(q);
    for (int i=0;i<q;i++){
        cin >> m[i];
    }
    for (int i=0;i<q;i++){
        if (solve(0,m[i],n,A)){
            cout << "yes" << endl;
        }
        else {
            cout << "no" << endl;
        }
        
    }

}
accuracy: 1
time_ms: 400
memory_kb: 3376
final_rank: 9

code_snippet: #include <iostream>
#include <vector>
using namespace std;

int n;
vector<int> A;

bool solve(int i, int m){
   if (m == 0)return true;
   if (i >= n)return false;
   bool res = solve(i + 1, m) || solve(i + 1, m - A[i]);
   return res;
}

int main(){
    int q;
    vector<int> M;
    
    cin >> n;
    A.resize(n);
    for (int i = 0; i < n; i++ ){cin >> A[i];}
    cin >> q;
    M.resize(q);
    for (int i = 0; i < q; i++ ){cin >> M[i];}

    for (int i=0; i < q; i++){
        if (solve(0,M[i])) cout << "yes" << endl;
        else cout << "no" << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 360
memory_kb: 3444
final_rank: 8

code_snippet: #include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <cstring> 

using namespace std;


int solve(int I,int B[2000],int M, int N){
    int ans;
    if (M==0){
        return 1;
    } else if (M<0){
        return 0;
    }
    if (I > N){
        return 0;
    }
    ans = solve(I+1,B,M,N) or solve(I+1,B,M-B[I],N);
    return ans;
}


int main(){
    int n,q,i,sum;
    int A[2000];
    int m[2000];
    
    cin >> n;
    for (i=0;i<n;i++){
        cin >> A[i];
    }
    
    cin >> q;
    for (i=0;i<q;i++){
        cin >> m[i];
    }
    
    
    
    
    for (i=0;i<q;i++){
        
        if (solve(0,A,m[i],n)==1){
            cout << "yes" << endl;
        }else{
            cout << "no" << endl;
        }
    }
    return 0;   
}
accuracy: 1
time_ms: 800
memory_kb: 3444
final_rank: 16

code_snippet: #include<iostream>
using namespace std;

int A[20];
bool solve(int i,int m,const int A[], int n){
    if (m==0){
        return true;
    }else if(m<0){
        return false;
    }
    if(i >= n){
        return false;
    }
    bool res = solve(i+1,m,A,n) || solve(i+1,m-A[i],A,n);
    return res;
}
int main() {
    int n,q;
    cin >> n; 
    int A[n];
    for(int i=0;i<n;i++){
        cin >> A[i];
    }   
    cin >> q;
    int m_i[q];
    for(int i=0;i<q;i++){
        cin >> m_i[i];
    }
    for(int i=0;i<q;i++){
        if(solve(0,m_i[i],A,n)){
            cout << "yes" <<endl;
        }else{
            cout << "no" << endl;
        }
    }
    return 0;
}
accuracy: 1
time_ms: 340
memory_kb: 3440
final_rank: 7

code_snippet: #include <iostream>
#include <vector>
using namespace std;

int slove(int i, int M, vector<int> A)
{
    if (M == 0)
    {
        return true;
    }
    if (M < 0)
    {
        return false;
    }
    if (i >= A.size())
    {
        return false;
    }
    int res = slove(i + 1, M, A) || slove(i + 1, M - A[i], A);
    return res;
}
int main()
{
    int n, q;

    cin >> n;
    vector<int> A(n);
    for (int i = 0; i < n; i++)
    {
        cin >> A[i];
    }
    cin >> q;
    vector<int> M(q);
    for (int i = 0; i < q; i++)
    {
        cin >> M[i];
    }
    for (int i = 0; i < q; i++)
    {
        if (slove(0, M[i], A))
        {
            cout << "yes" << endl;
        }
        else
        {
            cout << "no" << endl;
        }
    }
}
accuracy: 1
time_ms: 4580
memory_kb: 3448
final_rank: 17

code_snippet: #include <iostream>
#include <vector>
using namespace std;

int slove(int i, int M, vector<int> A)
{
    if (M == 0)
    {
        return true;
    }
    if (M < 0)
    {
        return false;
    }
    if (i > A.size())
    {
        return false;
    }
    int res = slove(i + 1, M, A) || slove(i + 1, M - A[i], A);
    return res;
}
int main()
{
    int n, q;

    cin >> n;
    vector<int> A(n);
    for (int i = 0; i < n; i++)
    {
        cin >> A[i];
    }
    cin >> q;
    vector<int> M(q);
    for (int i = 0; i < q; i++)
    {
        cin >> M[i];
    }
    for (int i = 0; i < q; i++)
    {
        if (slove(0, M[i], A))
        {
            cout << "yes" << endl;
        }
        else
        {
            cout << "no" << endl;
        }
    }
}
accuracy: 0.8
time_ms: 1100
memory_kb: 3448
final_rank: 20

code_snippet: #include <iostream>
#include <vector>
using namespace std;

int n;
vector<int> A;

bool solve(int i, int m){
    if(m == 0){
        return true;
    } else if(m < 0){
        return false;
    }
    
    if(i >= n){
        return false;
    }
    
    bool res = solve(i + 1, m) || solve(i + 1, m - A[i]);
    return res;
}

int main(){
    cin >> n;
    A.resize(n);
    for (int i=0; i<n; i++){
        cin >> A[i];
    }
    
    int q;
    cin >> q;
    vector<int> m(q);
    for(int i=0; i<q; i++){
        cin >> m[i]; 
    }
    
    
    for(int i=0; i<q; i++){
        if(solve(0, m[i]) == true){
            cout << "yes" << endl;
        } else {
            cout << "no" << endl;
        }
    }
    return 0;
}
accuracy: 1
time_ms: 410
memory_kb: 3444
final_rank: 13

code_snippet: using namespace std;
#include <iostream>

int n;
int A[2000];

int solve(int i, int m){
    if(m == 0){
        return true;
    }
    if(i >= n){
        return false;
    }
    int res = solve(i + 1,m) || solve(i+1, m-A[i]);
    return res;
}


int main (){
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> A[i];
    }
    int q;
    cin >> q;
    int M;
    for(int i = 0; i < q; i++){
        cin >> M;
        if(solve(0,M)){
            cout << "yes" << endl;
        }
        else{
            cout << "no" << endl;
        }
    }
    return 0;
}
accuracy: 1
time_ms: 360
memory_kb: 3368
final_rank: 5

code_snippet: #include<iostream>
#include<vector>
using namespace std;
int solve(int i,int M,int n,const vector<int>& A){
    if (M == 0){
        return 1;
    }else if(M < 0){
        return 0;
    }
    if (i >= n){
        return 0;
    }
    int res,res2;
    res = solve(i+1,M,n,A);
    res2 = solve(i+1,M-A[i],n,A);
    if (res == 0 && res2 == 0){
        return 0;
    }else{
        return 1;
    } 
}

int main(){
    int n,q,input_1,input_2;
    vector<int>A,m_i;
    cin >> n;
    for(int i = 0;i < n; ++i){
        cin >> input_1;
        A.push_back(input_1);
    }
    cin >> q;
    for(int j = 0;j < q; ++j){
        cin >> input_2;
        m_i.push_back(input_2);
    }
    for(int k = 0;k < q; ++k){
        int key = m_i[k];
        if (solve(0,key,n,A) == 1){
            cout << "yes" << endl;
        }else{
            cout << "no" << endl;
        }
    }

}
accuracy: 1
time_ms: 630
memory_kb: 3440
final_rank: 14

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <bits/stdc++.h>
using namespace std;

int main(){
    int n;
    cin >> n;

    int A[n];
    for(int i = 0; i < n; i++){
        cin >> A[i];
    }

    int q;
    cin >> q;

    int m[q];
    for(int i = 0; i < q; i++){
        cin >> m[i];
    }

    

    for(int j = 0; j < q; j++){
        int s = 0;
        for (int bits = 0; bits < (1 << n); bits++) {
            int sum = 0;
            for (int i = 0; i < n; i++) {
                if (bits & (1 << i)) {
                    sum += A[i];
                }
            }
            if (sum == m[j]) {
                cout << "yes" << endl;
                s = sum;
                break;
            }
        }
        if (s != m[j]){
            cout << "no" << endl;
        }
    }
    return 0;
}
accuracy: 1
time_ms: 4940
memory_kb: 3440
final_rank: 19

code_snippet: #include <iostream>
#include <vector>
using namespace std;

int n;
vector<int> A;

bool solve(int i, int M){
    if (M == 0){
        return true;
    }
    else if (M < 0){
        return false;        
    }
    
    if (i >= n){
        return false;
    }
    bool res = solve(i+1, M) || solve(i+1, M - A[i]);
    return res;
}

int main(){
    cin >> n;
    A.resize(n);
    for (int i = 0; i<n ; i++){
        cin >> A[i];
    }
    int q;
    cin >> q;
    for (int i= 0; i<q ;i++){
        int m;
        cin >> m;
        if (solve(0,m)){
            cout << "yes" << endl; 
        } else {
            cout << "no" << endl;
        }
    }
    return 0;
}
accuracy: 1
time_ms: 410
memory_kb: 3440
final_rank: 12