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

#include <iostream>
#include <vector>

bool serach(std::vector<int>, int);
bool serach(std::vector<int>, int, int, int);

int main(int argc, char const *argv[]) {
  int n, q;
  int a, m;
  std::vector<int> A;
  std::cin >> n;
  for (int i = 0; i < n; ++i) {
    std::cin >> a;
    A.push_back(a);
  }

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

  return 0;
}

bool serach(std::vector<int> A, int m) {
  for (int i = 0; i < A.size(); ++i) {
    if (serach(A, 0, m, i))
      return true;
  }
  return false;
}

bool serach(std::vector<int> A, int sum, int m, int I) {
  sum += A[I];
  if (sum == m) {
    return true;
  } else if (sum > m) {
    return false;
  } else {
    for (int i = I + 1; i < A.size(); ++i) {
      if (serach(A, sum, m, i))
        return true;
    }
    return false;
  }
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

bool serach(std::vector<int>, int);
bool serach(std::vector<int>, int, int, int);

int main(int argc, char const *argv[]) {
  int n, q;
  int a, m;
  std::vector<int> A;
  std::cin >> n;
  for (int i = 0; i < n; ++i) {
    std::cin >> a;
    A.push_back(a);
  }

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

  return 0;
}

bool serach(std::vector<int> A, int m) {
  for (int i = 0; i < A.size(); ++i) {
    if (serach(A, 0, m, i))
      return true;
  }
  return false;
}

bool serach(std::vector<int> A, int sum, int m, int I) {
  sum += A[I];
  if (sum == m) {
    return true;
  } else if (sum > m) {
    return false;
  } else {
    A.erase(A.begin() + I);
    for (int i = 0; i < A.size(); ++i) {
      if (serach(A, sum, m, i))
        return true;
    }
    return false;
  }
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <set>
#include <vector>

using namespace std;

int main() {
  size_t n;
  scanf("%zu", &n);

  vector<int> A(n);
  for (size_t i = 0; i < n; ++i)
    scanf("%d", &A[i]);

  set<int> sum;

  for (size_t i = 0; i < (1u << n); ++i) {
    int s = 0;
    for (size_t j = 0; j < n; ++j) {
      if (1 << j & i)
        s += A[j];
    }
    sum.insert(s);
  }

  size_t q;
  scanf("%zu", &q);

  for (size_t i = 0; i < q; ++i) {
    int m;
    scanf("%d", &m);

    printf("%s\\n", sum.count(m) ? "yes" : "no");
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

bool can_make(const vector<int> A, int m, vector<vector<int>> dp,
              size_t i = 0) {
  if (i == A.size())
    return !m;
  if (!m)
    return (dp[m][i] = 1);
  if (m < 0)
    return false;
  if (~dp[m][i])
    return dp[m][i];

  if (m >= A[i])
    if (can_make(A, m - A[i], dp, i + 1))
      return (dp[m][i] = 1);

  return (dp[m][i] = can_make(A, m, dp, i + 1));
}

int main() {
  size_t n;
  scanf("%zu", &n);

  vector<int> A(n);
  for (size_t i = 0; i < n; ++i)
    scanf("%d", &A[i]);

  size_t q;
  scanf("%zu", &q);

  for (size_t i = 0; i < q; ++i) {
    int m;
    scanf("%d", &m);

    vector<vector<int>> dp(m + 1, vector<int>(A.size(), -1));
    printf("%s\\n", can_make(A, m, dp) ? "yes" : "no");
  }

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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define rep(i, n) FOR(i, 0, n)
#define DEBUG(x) cout << #x << ": " << x << endl
#define vint vector<int>
#define vdouble vector<double>
#define vstring vector<string>
using namespace std;

#include <map>
#include <queue>
#include <set>

typedef long long ll;
typedef unsigned long long ull;

const int MAX_N = 1000000;
int F[MAX_N];
int NS;
int NT;
int S[MAX_N];
int T[MAX_N];

bool flag;
int target;

void print(int F[], int N) {
  rep(i, N) { cout << F[i] << " "; }
  cout << endl;
}

void Judge(int A[], int F[], int N) {
  int ret = 0;
  rep(i, N) { ret += A[i] * F[i]; }
  if (ret == target)
    flag = true;
}

void rec(int A[], int F[], int N, int i) {
  if (i == N) {
    Judge(A, F, N);
    return;
  }

  F[i] = 0;
  rec(A, F, N, i + 1);

  F[i] = 1;
  rec(A, F, N, i + 1);
};

int main() {

  cin >> NS;
  rep(i, NS) { cin >> S[i]; }
  cin >> NT;
  rep(i, NT) { cin >> T[i]; }

  rep(i, NT) {
    flag = false;
    target = T[i];
    rec(S, F, NS, 0);
    cout << (flag ? "yes" : "no") << endl;
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define rep(i, n) FOR(i, 0, n)
#define DEBUG(x) cout << #x << ": " << x << endl
#define vint vector<int>
#define vdouble vector<double>
#define vstring vector<string>
using namespace std;

#include <map>
#include <queue>
#include <set>

typedef long long ll;
typedef unsigned long long ull;

const int MAX_N = 1000000;
int F[MAX_N];
int NS;
int NT;
int S[MAX_N];
int T[MAX_N];

bool flag;
int target;

void print(int F[], int N) {
  rep(i, N) { cout << F[i] << " "; }
  cout << endl;
}

void Judge(int A[], int F[], int N) {
  int ret = 0;
  rep(i, N) { ret += A[i] * F[i]; }
  if (ret == target)
    flag = true;
}

void rec(int A[], int F[], int N, int i) {
  if (i == N) {
    Judge(A, F, N);
    return;
  }

  F[i] = 0;
  rec(A, F, N, i + 1);

  F[i] = 1;
  rec(A, F, N, i + 1);
};

int main() {

  cin >> NS;
  rep(i, NS) { cin >> S[i]; }
  cin >> NT;
  rep(i, NT) { cin >> T[i]; }

  rep(i, NT) {
    flag = false;
    target = T[i];
    rec(S, F, NS, i);
    cout << (flag ? "yes" : "no") << endl;
  }
}
Problem p02271 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

bool solve(int p, int t);
void sortA(void);

int A[3000];
int n;

int main() {

  int q;

  cin >> n;

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

  cin >> q;
  int tmp;
  for (int j = 0; j < q; j++) {

    cin >> tmp;
    int i, lim = n - 1;
    for (i = 0; i < n; i++) {
      if (lim == n - 1 && tmp < A[i]) {
        lim = (i - 1 >= 0 ? i - 1 : 0);
      }
    }
    if (solve(lim, tmp)) {
      cout << "yes\\n";
    } else {
      cout << "no\\n";
    }
  }

  return 0;
}
void sortA(void) {
  int i, j;
  int tmp;
  for (i = 0; i < n; i++) {
    for (j = i + 1; j < n; j++) {
      if (A[i] > A[j]) {
        tmp = A[i];
        A[i] = A[j];
        A[j] = tmp;
      }
    }
  }
}
bool solve(int i, int t) {
  if (t == A[i]) {
    return true;
  } else if (t <= 0) {
    return false;
  } else if (i == 0) {
    return false;
  }

  if (solve(i - 1, t - A[i])) {
    return true;
  } else if (solve(i - 1, t)) {
    return true;
  } else {
    return false;
  }
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

bool solve(int p, int t);
void sortA(void);

int A[3000];
int n;

int main() {

  int q;

  cin >> n;

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

  cin >> q;
  int tmp;
  for (int j = 0; j < q; j++) {

    cin >> tmp;
    // CALL Solve Method
    int i, lim = n - 1;
    for (i = 0; i < n; i++) {
      if (lim == n - 1 && tmp < A[i]) {
        lim = i - 1;
      }
    }
    if (solve(lim, tmp)) {
      cout << "yes\\n";
    } else {
      cout << "no\\n";
    }
  }

  return 0;
}
void sortA(void) {
  int i, j;
  int tmp;
  for (i = 0; i < n; i++) {
    for (j = i + 1; j < n; j++) {
      if (A[i] > A[j]) {
        tmp = A[i];
        A[i] = A[j];
        A[j] = tmp;
      }
    }
  }
}
bool solve(int i, int t) {
  if (t == A[i]) {
    return true;
  } else if (t <= 0) {
    return false;
  } else if (i == 0) {
    return false;
  }

  if (solve(i - 1, t - A[i])) {
    return true;
  } else if (solve(i - 1, t)) {
    return true;
  } else {
    return false;
  }
}
Problem p02271 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
int a[20];
int n;
int dfs(int m, int sum, int i) {
  if (i >= n)
    return 0;
  if (sum + a[i] == m)
    return 1;

  return dfs(m, sum + a[i], i + 1) + dfs(m, sum, i + 1);
}

void A() {
  scanf("%d", &n);

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

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

int main() {
  A();
  return 0;
}

Error: -11

Fixed submission:

#include <cstdio>
int a[20];
bool dfs(int m, int sum, int i) {
  if (sum + a[i] > m)
    return false;
  if (sum + a[i] == m)
    return true;
  return dfs(m, sum + a[i], i + 1) || dfs(m, sum, i + 1);
}

void A() {
  int n;
  scanf /*_s*/ ("%d", &n);

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

  int q;
  scanf /*_s*/ ("%d", &q);
  for (int i = 0; i < q; i++) {
    int m = 0;
    scanf /*_s*/ ("%d", &m);
    if (dfs(m, 0, 0)) {
      printf("yes");
    } else {
      printf("no");
    }
  }
}

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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e12;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1

int n, q;
int a[100100], m[100100];
set<int> si;

int main() {
  cin >> n;
  rep(i, n) cin >> a[i];
  rep(j, (1 << n)) {
    int s = 0;
    rep(k, n) {
      if (j & (1 << k))
        s += a[k];
    }
    si.insert(s);
  }
  cin >> q;
  rep(i, q) {
    cin >> m[i];
    if (si.find(m[i]) != si.end())
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e12;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1

int n, q;
int a[100100], m[100100];

int main() {
  cin >> n;
  rep(i, n) cin >> a[i];
  cin >> q;
  rep(i, q) {
    cin >> m[i];
    rep(j, (1 << n)) {
      ll s = 0;
      rep(k, n) {
        if (j & (1 << k))
          s += a[k];
      }
      if (s == m[i]) {
        cout << "yes" << endl;
        break;
      }
      if (j == (1 << n) - 1)
        cout << "no" << endl;
    }
  }
}
Problem p02271 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>

int n, A[50];

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

int main() {
  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;
}

Error: -11

Fixed submission:

#include <stdio.h>

int n, A[50];

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

int main() {
  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;
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long unsigned int ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

std::vector<int> A;
int n;

void print_vector(vector<int> v) {
  for (auto i : v) {
    std::cout << i << " ";
  }
  std::cout << '\\n';
}

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

int main() {
  cin >> n;
  for (size_t i = 0; i < n; i++) {
    int a;
    cin >> a;
    A.push_back(a);
  }

  int q;
  cin >> q;
  for (size_t i = 0; i < q; i++) {
    int m;
    scanf("%d", &m);

    if (check(0, m)) {
      std::cout << "yes" << '\\n';
    } else {
      std::cout << "no" << '\\n';
    }
  }

  // print_vector(A);

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long unsigned int ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

void print_vector(vector<int> v) {
  for (auto i : v) {
    std::cout << i << " ";
  }
  std::cout << '\\n';
}

bool check(std::vector<int> A, int i, int m) {
  if (m == 0) {
    return true;
  }
  if (i >= A.size()) {
    return false;
  }
  bool res = check(A, i + 1, m) || check(A, i + 1, m - A[i]);
  return res;
}

int main() {
  int n;
  cin >> n;
  std::vector<int> A;
  for (size_t i = 0; i < n; i++) {
    int a;
    cin >> a;
    A.push_back(a);
  }

  int q;
  cin >> q;
  for (size_t i = 0; i < q; i++) {
    int m;
    scanf("%d", &m);

    if (check(A, 0, m)) {
      std::cout << "yes" << '\\n';
    } else {
      std::cout << "no" << '\\n';
    }
  }

  // print_vector(A);

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

#include <iostream>
#include <iterator>
#include <map>
#include <unordered_map>
using namespace std;

typedef multimap<int, int> mm;
typedef mm::reverse_iterator ri;
unordered_map<int, bool> memo;

bool solve(ri p, int t, mm &A) {

  long key = distance(A.rbegin(), p) + 1 + ((t + 1) << 16);
  auto i = memo.find(key);
  if (i != memo.end())
    return (*i).second;

  bool res;
  int x = (*p).first;
  if (p == A.rend())
    res = false;
  else if (x == t)
    res = true;
  else if ((*p).second < t)
    res = false;
  else if (x <= t)
    res = solve(next(p, 1), t - x, A) ? true : solve(next(p, 1), t, A);
  else
    res = solve(make_reverse_iterator(A.upper_bound(t)), t, A);

  memo[key] = res;
  return res;
}

int main() {
  int n, q, m, sum = 0;

  cin >> n;
  mm A;
  while (n--) {
    cin >> m;
    A.insert({m, 0});
  }

  for (auto &x : A) {
    sum += x.first;
    x.second = sum;
  }

  cin >> q;
  while (q--) {
    cin >> m;
    cout << (solve(A.rbegin(), m, A) ? "yes" : "no") << endl;
  }
}


Error: 0
22


Fixed submission:

#include <iostream>
#include <iterator>
#include <map>
#include <unordered_map>
using namespace std;

typedef multimap<int, int> mm;
typedef mm::reverse_iterator ri;
unordered_map<int, bool> memo;

int counter = 0;
bool solve(ri p, int t, mm &A) {
  counter++;

  long key = distance(A.rbegin(), p) + 1 + ((t + 1) << 16);
  auto i = memo.find(key);
  if (i != memo.end())
    return (*i).second;

  bool res;
  int x = (*p).first;
  if (p == A.rend())
    res = false;
  else if (x == t)
    res = true;
  else if ((*p).second < t)
    res = false;
  else if (x <= t)
    res = solve(next(p, 1), t - x, A) ? true : solve(next(p, 1), t, A);
  else
    res = solve(make_reverse_iterator(A.upper_bound(t)), t, A);

  memo[key] = res;
  return res;
}

int main() {
  int n, q, m, sum = 0;

  cin >> n;
  mm A;
  while (n--) {
    cin >> m;
    A.insert({m, 0});
  }

  for (auto &x : A) {
    sum += x.first;
    x.second = sum;
  }

  cin >> q;
  while (q--) {
    cin >> m;
    cout << (solve(A.rbegin(), m, A) ? "yes" : "no") << endl;
  }

  cerr << counter << endl;
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

/**
 * ?????°????????£????????????????????????????????????????°??????????????????????
 */

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

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

void read_problem(vector<int> &a, vector<int> &ms) {
  int n;
  scanf("%d\\n", &n);

  a.resize(n);
  REP(i, n) { scanf("%d", &a[i]); }

  int q;
  scanf("%d\\n", &q);

  ms.resize(q);
  REP(i, q) { scanf("%d", &ms[i]); }
}

bool recurse(const vector<int> &a, int m, int sum, int i, bool use) {
  if (i == a.size())
    return false;

  if (use)
    sum += a[i];

  if (sum == m)
    return true;
  else if (sum > m)
    return false;

  return recurse(a, m, sum, i + 1, false) || recurse(a, m, sum, i + 1, true);
}

bool attempt(const vector<int> &a, int m) {
  return recurse(a, m, 0, 0, false) || recurse(a, m, 0, 0, true);
}

int main() {
  vector<int> a;
  vector<int> ms;
  read_problem(a, ms);

  int q = ms.size();
  deque<bool> result(q);
  REP(i, q) { result[i] = attempt(a, ms[i]); }

  for (bool b : result) {
    puts(b ? "yes" : "no");
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

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

void read_problem(vector<int> &a, vector<int> &ms) {
  int n;
  scanf("%d\\n", &n);

  a.resize(n);
  REP(i, n) { scanf("%d", &a[i]); }

  int q;
  scanf("%d\\n", &q);

  ms.resize(q);
  REP(i, q) { scanf("%d", &ms[i]); }
}

bool attempt(const vector<int> &a, int m) {
  int n = a.size();

  for (unsigned int mask = 0; mask < (1U << n) - 1; ++mask) {
    int sum = 0;
    for (int i = 0; i < n; ++i) {
      if (mask & (1 << i))
        sum += a[i];
    }
    if (sum == m)
      return true;
  }

  return false;
}

int main() {
  vector<int> a;
  vector<int> ms;
  read_problem(a, ms);

  int q = ms.size();
  deque<bool> result(q);
  REP(i, q) { result[i] = attempt(a, ms[i]); }

  for (bool b : result) {
    puts(b ? "yes" : "no");
  }

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

#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>   // require fabs
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit atof atoi
#include <cstring> // require memset
#include <ctime>   // require srand
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int a[22];
int m[205];

bool dfs(int ind, int curr, int n) {
  if (curr == 0)
    return true;
  if (curr < 0 || ind >= n)
    return false;
  if (dfs(ind + 1, curr - a[ind], n))
    return true;
  if (dfs(ind + 1, curr, n))
    return true;

  return false;
}

int main() {
  memset(a, 0, sizeof(a));
  memset(m, 0, sizeof(m));
  ios_base::sync_with_stdio(0);

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

  rep(k, q) {
    bool ok = dfs(0, m[k], n);
    cout << (ok ? "yes" : "no") << endl;
  } // end rep

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>   // require fabs
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit atof atoi
#include <cstring> // require memset
#include <ctime>   // require srand
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int a[22];
int m[205];

int main() {
  memset(a, 0, sizeof(a));
  memset(m, 0, sizeof(m));

  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  rep(i, n) cin >> a[i];
  int q;
  cin >> q;
  rep(j, q) cin >> m[j];

  rep(k, q) {
    bool ok = false;
    for (int i = 0; i < (1 << n); i++) {
      int curr = 0;
      rep(j, n) {
        if (i & (1 << j))
          curr += a[j];
      } // end rep
      if (curr == m[k])
        ok = true;
    } // end for
    cout << (ok ? "yes" : "no") << endl;
  } // end rep

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

#include <stdio.h>

using namespace std;

int n, A[50];

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

int main() {
  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;
}


Error: -11

Fixed submission:


#include <stdio.h>

using namespace std;

int n, A[50];

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

int main() {
  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;
}