alexjercan/bugnet huggingface dataset:
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>

#define N 2000

int a[N], n, sum, q, m, i;

int divide(int i, int m) {
  if (sum < m)
    return 0;
  if (m == 0)
    return 1;
  if (i >= n)
    return 0;
  return divide(i + 1, m) || divide(i + 1, m - a[i]);
}

int main() {

  sum = 0;
  scanf("%d", &n);

  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    sum += a[i];
  }
  scanf("%d", &q);

  for (i = 0; i < q; i++) {
    scanf("%d", &m);
    if (divide(0, m)) {
      printf("yes\\n");
    } else {
      printf("no\\n");
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

#define N 2000

int a[N], n, sum, m, i;

int divide(int i, int m) {
  if (sum < m)
    return 0;
  if (m == 0)
    return 1;
  if (i >= n)
    return 0;
  return divide(i + 1, m) || divide(i + 1, m - a[i]);
}

int main() {

  sum = 0;
  scanf("%d", &n);

  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    sum += a[i];
  }
  scanf("%d", &n);

  for (i = 0; i < n; i++) {
    scanf("%d", &m);
    if (divide(0, m)) {
      printf("yes\\n");
    } else {
      printf("no\\n");
    }
  }
  return 0;
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_5_A&lang=jp
*/

#include <cmath>
#include <iostream>
using namespace std;

int n, q;
int A[50];

int check(int i, int m) {
  if (m == 0)
    return 1;
  if (i >= n)
    return 0;
  int res = check(i + 1, m) || check(i + 1, m - A[i]);
  return res;
}
int main() {
  int m;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> m;
    if (check(0, m))
      cout << "yes"
           << "\\n";
    else
      cout << "no"
           << "\\n";
  }
}

Error: TLE

Fixed submission:

/*
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_5_A&lang=jp
*/
/*p144 ??¨??¢?´¢*/
#include <cmath>
#include <iostream>
using namespace std;

int n, q;
/*2?????°??§1?????¨????????????????????°????????????*/
bool check(int A[], int m) {
  for (int i = 1; i < pow(2, n) - 1; i++) {
    int sum = 0, k = i;
    for (int j = n - 1; j >= 0; j--) {
      if (k & 1)
        sum += A[j];
      if (sum == m)
        return true;
      if (sum > m)
        break;
      k >>= 1; //???????????????
    }
  }
  return false;
}
int main() {
  int m;
  cin >> n;
  int A[n];
  for (int i = 0; i < n; i++)
    cin >> A[i];
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> m;
    if (check(A, m))
      cout << "yes"
           << "\\n";
    else
      cout << "no"
           << "\\n";
  }
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

#define N 100

// grobal
int data[N], num1, num2;

// flag
bool flag = false;

// function solve
bool solve(int i, int m) {

  // if i >num1 then false
  if (i > num1)
    return false;
  // if m<0 then false
  if (m < 0)
    return false;

  // if m=0 then true
  if (m == 0) {
    flag = true;
    return true;
  }
  // execute
  solve(i + 1, m);
  solve(i + 1, m - data[i]);
}

int main() {

  int i, m;

  // load number
  cin >> num1;

  // load data
  for (i = 0; i < num1; i++) {
    cin >> data[i];
  }
  // load number
  cin >> num2;
  // load  data and execute
  for (i = 0; i < num2; i++) {
    cin >> m;
    // initial flag
    flag = false;
    // execute solve
    solve(0, m);

    // print yes or no
    if (flag == true) {
      cout << "yes" << endl;
    } else
      cout << "no" << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

#define N 100

int data[N], num1, num2;

bool flag = false;

bool solve(int i, int m) {

  if (i > num1)
    return false;
  else if (m < 0)
    return false;
  if (m == 0 || m - data[i] == 0) {
    flag = true;
    return true;
  }

  else {
    solve(i + 1, m);
    solve(i + 1, m - data[i]);
  }
  return true;
}

int main() {

  int i, m;

  cin >> num1;

  for (i = 0; i < num1; i++) {
    cin >> data[i];
  }

  cin >> num2;

  for (i = 0; i < num2; i++) {
    cin >> m;
    flag = false;

    solve(0, m);
    if (flag == true) {
      cout << "yes" << endl;
    } else
      cout << "no" << endl;
  }

  return 0;
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

#define N 100

int data[N], num1, num2;

bool flag = false;

bool solve(int i, int m) {

  if (i > num1)
    return false;
  else if (m < 0)
    return false;
  if (m == 0 || m - data[i] == 0) {
    flag = true;
    return true;
  } else {
    solve(i + 1, m);
    solve(i + 1, m - data[i]);
  }
}

int main() {

  int i, m;

  cin >> num1;

  for (i = 0; i < num1; i++) {
    cin >> data[i];
  }

  cin >> num2;

  for (i = 0; i < num2; i++) {
    cin >> m;
    flag = false;

    solve(-1, m);
    if (flag == true) {
      cout << "yes" << endl;
    } else
      cout << "no" << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;
#define N 100

int data[N], num1, num2;

bool flag = false;

bool solve(int i, int m) {

  if (i > num1)
    return false;
  else if (m < 0)
    return false;
  if (m == 0) {
    flag = true;
    return true;
  } else {
    solve(i + 1, m);
    solve(i + 1, m - data[i]);
  }
}

int main() {

  int i, m;

  cin >> num1;

  for (i = 0; i < num1; i++) {
    cin >> data[i];
  }

  cin >> num2;

  for (i = 0; i < num2; i++) {
    cin >> m;
    flag = false;

    solve(-1, m);
    if (flag == true) {
      cout << "yes" << endl;
    } else
      cout << "no" << endl;
  }

  return 0;
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdlib>
#include <iostream>

using namespace std;
#define MAX 1000
#define N 100

int data[N], num1, num2;

bool flag = false;

bool solve(int i, int m) {

  if (i > num1)
    return false;
  else if (m < 0)
    return false;
  if (m == 0 || m - data[i] == 0) {
    flag = true;
    return true;
  } else {
    solve(i + 1, m);
    solve(i + 1, m - data[i]);
  }
}

int main() {

  int i, m;

  cin >> num1;

  for (i = 0; i < num1; i++) {
    cin >> data[i];
  }

  cin >> num2;

  for (i = 0; i < num2; i++) {
    cin >> m;
    flag = false;

    solve(-1, m);
    if (flag == true) {
      cout << "yes" << endl;
    } else
      cout << "no" << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

#define N 100

int data[N], num1, num2;
bool flag = false;

bool solve(int i, int m) {

  if (i > num1)
    return false;
  else if (m < 0)
    return false;
  if (m == 0) {
    flag = true;
    return true;
  }

  //  if(m-data[i]<0){
  solve(i + 1, m);
  //  }
  //  else{
  if (m - data[i] >= 0)
    solve(i + 1, m - data[i]);
  // }
  return false;
}

int main() {

  int i, m;

  cin >> num1;

  for (i = 0; i < num1; i++) {
    cin >> data[i];
  }

  cin >> num2;

  for (i = 0; i < num2; i++) {
    cin >> m;
    flag = false;

    solve(-1, m);
    if (flag == true) {
      cout << "yes" << endl;
    } else
      cout << "no" << endl;
  }

  return 0;
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int sum(vector<int> v) {
  int sum = 0;
  for (int i = 0; i < v.size(); i++) {
    sum += v[i];
  }
  return sum;
}

bool gtsum(vector<int> v, int x) {
  if (sum(v) < x)
    return true;
  else
    return false;
}
bool hantei(vector<int> v, int x) {
  if (x < 0)
    return false;
  if (gtsum(v, x))
    return false;
  if (v.empty()) {
    if (x == 0)
      return true;
    else
      return false;
  } else {
    vector<int>::iterator it = v.begin();
    int first = *it;
    v.erase(it);
    vector<int> rest = v;
    return hantei(rest, x - first) || hantei(rest, x);
  }
}

int main() {
  int n;
  cin >> n;
  vector<int> v;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    v.push_back(a);
  }
  int m;
  cin >> m;
  for (int i = 0; i < m; i++) {
    int x;
    cin >> x;
    if (hantei(v, x))
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
}

Error: TLE

Fixed submission:

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

bool hantei(vector<int> v, int x) {
  if (x < 0)
    return false;
  if (v.empty()) {
    if (x == 0)
      return true;
    else
      return false;
  } else {
    vector<int>::iterator it = v.begin();
    int first = *it;
    v.erase(it);
    vector<int> rest = v;
    return hantei(rest, x - first) || hantei(rest, x);
  }
}

int main() {
  int n;
  cin >> n;
  vector<int> v;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    v.push_back(a);
  }
  int m;
  cin >> m;
  for (int i = 0; i < m; i++) {
    int x;
    cin >> x;
    if (hantei(v, x))
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
}
Problem p02271 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main(void) {

  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;

  vector<int> res;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    for (int j = 0, size = res.size(); j < size; ++j) {
      res.push_back(a + res[j]);
    }
    res.push_back(a);
  }

  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int t;
    cin >> t;
    vector<int>::iterator it = find(res.begin(), res.end(), t);
    cout << (it != res.end() ? "yes" : "no") << "\\n";
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

void sumcomb(int pos, int sum, int *a, int n, vector<int> &res) {
  if (pos == n) {
    res.push_back(sum);
  }
  for (int i = pos; i < n; ++i) {
    sumcomb(i + 1, sum + a[i], a, n, res);
    sumcomb(i + 1, sum, a, n, res);
  }
}

// ????????????
int main(void) {

  ios::sync_with_stdio(false);
  cin.tie(0);

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

  vector<int> res;
  sumcomb(0, 0, A, n, res);

  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int m;
    cin >> m;
    vector<int>::iterator it = find(res.begin(), res.end(), m);
    cout << (it != res.end() ? "yes" : "no") << "\\n";
  }

  return 0;
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <numeric>
using namespace std;

long dict[1048576] = {};

void makeDict(long A[], long n) {
  for (long i = 0; i < pow(2, n); i++) {
    dict[i] += (i % 2) * A[0];
    for (long j = 1; j < n; j++) {
      dict[i] +=
          (fmod(i, pow(2, j + 1)) - fmod(i, pow(2, j))) / pow(2, j) * A[j];
    }
  }
}

int main() {
  long n, q, A[20], m[200];
  cin >> n;
  for (long i = 0; i < n; i++)
    cin >> A[i];
  makeDict(A, n);
  int p = pow(2, n);
  sort(dict, dict + p);
  cin >> q;
  for (long i = 0; i < q; i++) {
    cin >> m[i];
    if (binary_search(dict, dict + p, m[i]))
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;

long digit[20];

void makeDigit(long key, long n) {
  digit[0] = key % 2;
  for (long i = 1; i < n; i++)
    digit[i] = (fmod(key, pow(2, i + 1)) - fmod(key, pow(2, i))) / pow(2, i);
}

int main() {
  // long n, q, A[20], m[200];
  long n, q, A[20], m;
  cin >> n;
  for (long i = 0; i < n; i++)
    cin >> A[i];
  cin >> q;
  // for (long i = 0; i < q; i++) cin >> m[i];
  // scanf("%ld", &n);
  // for (long i = 0; i < n; i++) scanf("%ld", &A[i]);
  // scanf("%ld", &q);
  // for (long i = 0; i < q; i++) scanf("%ld", &m[i]);
  for (long i = 0; i < q; i++) {
    cin >> m;
    long flag = 0;
    for (long j = 0; j < pow(2, n); j++) {
      makeDigit(j, n);
      long sum = 0;
      for (long k = 0; k < n; k++)
        sum += A[k] * digit[k];
      if (sum == m) {
        flag = 1;
        break;
      }
    }
    if (flag)
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"

using namespace std;

#define int long long
#define ll long long
typedef pair<int, int> P;
#define mod 1000000007
#define INF (1LL << 60)

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl

int table[50000];

signed main() {

  int n, q, a[20], m[200];
  cin >> n;
  rep(i, n) cin >> a[i];
  cin >> q;
  rep(i, q) cin >> m[i];

  rep(k, 1 << n) {
    int t = 0;
    rep(bit, n) {
      if ((1 << bit) & k)
        t += a[bit];
    }
    table[t]++;
  }

  rep(i, q) {
    if (table[m[i]])
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

#define int long long
#define ll long long
typedef pair<int, int> P;
#define mod 1000000007
#define INF (1LL << 60)

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl

signed main() {

  int n, q, a[20], m[200];
  cin >> n;
  rep(i, n) cin >> a[i];
  cin >> q;
  rep(i, q) cin >> m[i];

  rep(query, q) {
    bool found = false;
    rep(k, 1 << n) {
      int t = 0;
      rep(bit, n) {
        if ((1 << bit) & k)
          t += a[bit];
      }
      if (t == m[query]) {
        found = true;
        cout << "yes" << endl;
        break;
      }
    }
    if (!found)
      cout << "no" << endl;
  }
  return 0;
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define REP(i, n) for (int i = 0; i < n; i++)

int n;
vector<int> a(50);

int solve(int i, int m) {
  if (m == 0)
    return 1;
  if (i >= n || m < 0)
    return 0;

  int res = solve(i + 1, m) || solve(i + 1, m - a[i]);
  return res;
}

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

  cin >> n;
  REP(i, n) cin >> a[i];
  a.resize(n);
  int q;
  cin >> q;
  REP(i, q) {
    int m;
    cin >> m;
    cout << (solve(0, m) ? "yes" : "no") << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define REP(i, n) for (int i = 0; i < n; i++)
#define VSORT(v) sort(v.begin(), v.end())

int n;
vector<int> a(30);

int solve(int i, int m) {
  if (m == 0)
    return 1;
  if (i >= a.size() || m < 0)
    return 0;
  int res;
  if (m >= a[i]) {
    res = solve(i + 1, m) || solve(i + 1, m - a[i]);
  } else {
    res = solve(i + 1, m);
  }
  return res;
}

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

  cin >> n;
  REP(i, n) cin >> a[i];
  VSORT(a);

  int q;
  cin >> q;
  REP(i, q) {
    int m;
    cin >> m;
    cout << (solve(0, m) ? "yes" : "no") << endl;
  }
  return 0;
}
Problem p02271 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
using namespace std;

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

  int n, q;
  bool a[2001] = {0};
  int x;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x;
    for (int j = 2000; j >= 0; j--) {
      if (a[j]) {
        a[j + x] = true;
      }
    }
    a[x] = true;
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> x;
    if (a[x]) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }

  return 0;
}

Error: -6
*** stack smashing detected ***: terminated


Fixed submission:

#include <iostream>
#include <string>
using namespace std;

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

  int n, q;
  bool a[2001] = {0};
  int x;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x;
    for (int j = 2001; j >= 0; j--) {
      if (a[j]) {
        a[j + x] = true;
      }
    }
    a[x] = true;
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> x;
    if (a[x]) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }

  return 0;
}
Problem p02271 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

int n, A[20], s[20], b[10000000], bc = 0, q, m;

bool rec(int i) {
  if (i == n) {
    int sum = 0;
    for (int j = 0; j < n; ++j) {
      // cout << s[j] << ",";
      if (s[j])
        sum += A[j];
    }
    b[bc] = sum;
    ++bc;
    // cout << endl;
    return 0;
  }

  rec(i + 1);
  s[i] = 1;
  rec(i + 1);
  s[i] = 0;
}

void combi() {
  for (int i = 0; i < n; ++i)
    ;
  rec(0);
}

bool judge(int in) {
  for (int i = 0; i < bc; ++i)
    if (in == b[i])
      return true;
  return false;
}

int main() {

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

  combi();

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

  return 0;
}


Error: 0

Fixed submission:

#include <iostream>

using namespace std;

int n, A[20], s[20], b[1000000], bc = 0, q, m;

bool rec(int i) {
  if (i == n) {
    int sum = 0;
    for (int j = 0; j < n; ++j) {
      // cout << s[j] << ",";
      if (s[j])
        sum += A[j];
    }
    b[bc] = sum;
    ++bc;
    // cout << endl;
    return 0;
  }

  rec(i + 1);
  s[i] = 1;
  rec(i + 1);
  s[i] = 0;
}

void combi() {
  for (int i = 0; i < n; ++i)
    ;
  rec(0);
}

bool judge(int in) {
  for (int i = 0; i < bc; ++i)
    if (in == b[i])
      return true;
  return false;
}

int main() {

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

  combi();

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

  return 0;
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int n, A[50];

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() {
  int q, M, i;

  cin >> n;

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

  cin >> q;

  for (int i = 0; i < q; i++) {
    cin >> M;

    if (solve(0, M))
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;

const int maxn = 10000;

int book[maxn], n, m, k;
int A[maxn], B[maxn];
bool flag = false;

void fn(int r, int t);

int main() {
  cin >> n;

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

  cin >> m;

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

  for (int i = 0; i < m; i++) {
    flag = false;

    fn(0, B[i]);

    if (flag)
      cout << "yes\\n";
    else
      cout << "no\\n";
  }

  return 0;
}

void fn(int r, int t) {
  if (r == t) {
    flag = true;
    return;
  }

  if (r > t)
    return;

  for (int i = 0; i < n; i++) {
    if (book[i] == 0) {
      book[i] = 1;
      fn(r + A[i], t);
      book[i] = 0;
    }
  }
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

#define MAX_A_NUM 2000
#define MAX_M_NUM 2000

int flags[MAX_A_NUM];

bool check_rec(int n, int a[], int m, int p) {
  if (p == n) {
    int x = 0;
    for (int i = 0; i < n; i++) {
      if (flags[i] == 1) {
        x += a[i];
      }
    }
    if (x == m) {
      return true;
    }
    return false;
  }

  if (check_rec(n, a, m, p + 1))
    return true;
  flags[p] = 1;
  if (check_rec(n, a, m, p + 1))
    return true;
  flags[p] = 0;

  return false;
}

bool check(int n, int a[], int m) {
  for (int i = 0; i < n; i++) {
    flags[i] = 0;
  }

  return check_rec(n, a, m, 0);
}

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

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

#define MAX_A_NUM 2000
#define MAX_M_NUM 2000

bool _check(int n, int a[], int m, char used[], int current_value) {
  char original[MAX_A_NUM];
  for (int i = 0; i < n; i++) {
    original[i] = used[i];
  }

  for (int i = 0; i < n; i++) {
    for (int i = 0; i < n; i++) {
      used[i] = original[i];
    }

    if (used[i] == 1) {
      continue;
    }

    used[i] = 1;
    int new_value = current_value + a[i];
    if (new_value == m) {
      return true;
    } else if (new_value > m) {
      continue;
    }
    if (_check(n, a, m, used, new_value)) {
      return true;
    }
  }
  return false;
}

bool check(int n, int a[], int m) {
  char used[MAX_A_NUM];
  for (int i = 0; i < n; i++) {
    used[i] = 0;
  }

  return _check(n, a, m, used, 0);
}

int main() {
  int n, q, m;
  int a[MAX_A_NUM];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> m;
    if (check(n, a, m)) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
}
Problem p02271 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define REP(i, n) rep(i, 0, n)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
// #define int long long
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;
// const int INF = 1e12;

int n;
vector<int> a;
int memo[21][400001];

bool solve(int i, int m) {
  if (memo[i][m] != -1)
    return memo[i][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;
  return memo[i][m] = solve(i + 1, m) || solve(i + 1, m - a[i]);
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  a.resize(n);
  rep(i, 0, n) cin >> a[i];
  rep(i, 0, 21) rep(j, 0, 400001) memo[i][j] = -1;
  int q;
  cin >> q;
  rep(i, 0, q) {
    int m;
    cin >> m;
    if (solve(0, m))
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define REP(i, n) rep(i, 0, n)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1e12;

int n;
vector<int> a;
int memo[21][400001];

bool solve(int i, int m) {
  if (memo[i][m] != -1)
    return memo[i][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;
  return memo[i][m] = solve(i + 1, m) || solve(i + 1, m - a[i]);
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  a.resize(n);
  rep(i, 0, n) cin >> a[i];
  rep(i, 0, 21) rep(j, 0, 400001) memo[i][j] = -1;
  int q;
  cin >> q;
  rep(i, 0, q) {
    int m;
    cin >> m;
    if (solve(0, m))
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
}