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

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

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

int main() {
  int i, n, A[20], q, m;

  scanf("%d", &n);
  for (i = 0; i <= n - 1; i++)
    scanf("%d", &A[i]);
  scanf("%d", &q);
  for (i = 0; i <= q - 1; i++) {
    scanf("%d", &m);
    if (solve(0, m, n, A) == 1)
      printf("yes\\n");
    else
      printf("no\\n");
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int solve(int i, int m, int n, int A[]) {
  int d[20][2000];

  if (m == 0)
    return 2;
  if (i >= n || m < 0)
    return 1;
  if (d[i][m] != 0)
    return d[i][m];
  solve(i + 1, m, n, A);
  solve(i + 1, m - A[i], n, A);
  if (solve(i + 1, m, n, A) == 2 || solve(i + 1, m - A[i], n, A) == 2)
    return d[i][m] = 2;
  else
    return d[i][m] = 1;
}

int main() {
  int i, n, A[20], q, m;

  scanf("%d", &n);
  for (i = 0; i <= n - 1; i++)
    scanf("%d", &A[i]);
  scanf("%d", &q);
  for (i = 0; i <= q - 1; i++) {
    scanf("%d", &m);
    if (solve(0, m, n, A) == 2)
      printf("yes\\n");
    else
      printf("no\\n");
  }
  return 0;
}
Problem p02271 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int n, a[21], num;

bool saiki(int i, int sum) {
  if (sum == num)
    return 1;
  if (i == n)
    return 0;
  if (saiki(i + 1, a[i] + sum))
    return 1;
  if (saiki(i + 1, sum))
    return 1;
  return 0;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int q;
  cin >> q;
  while (q--) {
    cin >> num;
    if (saiki(0, 0))
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;
int n, a[21], num;

bool saiki(int i, int sum) {
  if (sum == num)
    return 1;
  if (saiki(i + 1, a[i] + sum))
    return 1;
  if (saiki(i + 1, sum))
    return 1;
  return 0;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int q;
  cin >> q;
  while (q--) {
    cin >> num;
    if (saiki(0, 0))
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

bool check(int ans, std::vector<int>::iterator iter,
           std::vector<int>::iterator enditer) {
  if (ans == 0) {
    return true;
  } else if (ans < 0) {
    return false;
  }

  if (iter == enditer) {
    return false;
  }

  bool result = check(ans, std::next(iter, 1), enditer) ||
                check(ans -= (*iter), std::next(iter, 1), enditer);
  return result;
}

int main() {
  int N, Q, temp;
  std::vector<int> A;
  std::cin >> N;

  for (int i = 0; i < N; i++) {
    std::cin >> temp;
    A.push_back(temp);
  }

  std::cin >> Q;

  auto b = A.begin();
  auto e = A.end();

  for (int i = 0; i < Q; i++) {
    std::cin >> temp;
    if (check(temp, b, e)) {
      std::cout << "yes" << std::endl;
    } else {
      std::cout << "no" << std::endl;
    }
  }
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

bool check(std::vector<int> V, int ans, std::vector<int>::iterator iter,
           std::vector<int>::iterator enditer) {
  if (ans == 0) {
    return true;
  } else if (ans < 0) {
    return false;
  }

  if (iter == enditer) {
    return false;
  }

  bool result = check(V, ans, std::next(iter, 1), enditer) ||
                check(V, ans -= (*iter), std::next(iter, 1), enditer);
  return result;
}

int main() {
  int N, Q, temp;
  std::vector<int> A;
  std::cin >> N;

  for (int i = 0; i < N; i++) {
    std::cin >> temp;
    A.push_back(temp);
  }

  std::cin >> Q;

  auto b = A.begin();
  auto e = A.end();

  for (int i = 0; i < Q; i++) {
    std::cin >> temp;
    if (check(A, temp, b, e)) {
      std::cout << "yes" << std::endl;
    } else {
      std::cout << "no" << std::endl;
    }
  }
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <stdio.h>
using namespace std;

bool judge(const int a[], int n, int m, int i, int sum);

int main(void) {
  int n, a[20];
  cin >> 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 (judge(a, n, m, 0, 0))
      cout << "yes";
    else
      cout << "no";
    cout << endl;
  }

  return 0;
}

bool judge(const int a[], int n, int m, int i, int sum) {
  if (i >= n)
    return false;
  int sum1 = sum;
  int sum2 = sum;

  if (judge(a, n, m, i + 1, sum1))
    return true;

  sum2 += a[i];
  if (sum2 == m)
    return true;
  if (sum2 < m && judge(a, n, m, i + 1, sum2))
    return true;
  return false;
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <stdio.h>
using namespace std;

bool judge(const int a[], int n, int m);

int main(void) {
  int n, a[20];
  cin >> 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 (judge(a, n, m))
      cout << "yes";
    else
      cout << "no";
    cout << endl;
  }

  return 0;
}

bool judge(const int a[], int n, int m) {
  int p = 1 << n;
  for (int i = 0; i < p; i++) {
    int sum = 0;
    for (int j = 0; j < n; j++) {
      if (i & (1 << j))
        sum += a[j];
    }
    if (sum == m)
      return true;
  }
  return false;
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int a[20];
int n;
bool work(int r, int t) {
  if (t < 0)
    return false;
  if (t == 0)
    return true;
  if (r == 1)
    return a[0] == t;
  return work(r - 1, t) || work(r - 1, t - a[r - 1]);
}
int main() {
  int q, i;
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> a[i];
  cin >> q;
  while (q--) {
    cin >> i;
    if (work(n, i))
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
using namespace std;
int n, a[20];
bool work(int m) {
  int i, j, sum, t, l = pow(2, n);
  for (i = 0; i < l; i++) {
    sum = 0;
    for (j = 0; j < n; j++) {
      t = i >> j;
      if ((t & 0x1) == 1) {
        sum += a[j];
        if (sum > m)
          break;
      }
    }
    if (sum == m)
      return true;
  }
  return false;
}
int main() {
  int i, q;
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", &a[i]);
  scanf("%d", &q);
  while (q--) {
    scanf("%d", &i);
    if (work(i))
      printf("yes\\n");
    else
      printf("no\\n");
  }
  return 0;
}
Problem p02271 (C++) — buggy submission, verdict: Runtime Error

#include <bitset>
#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

int main() {
  int n;
  cin >> n;
  bool num[40000] = {false};
  num[0] = true;
  int a, max = 0;
  for (int i = 0; i < n; i++) {
    cin >> a;
    for (int d = max; d >= 0; d--) {
      if (num[d])
        num[d + a] = true;
    }
    max += a;
  }
  int s, z;
  cin >> s;
  for (int i = 0; i < s; i++) {
    cin >> z;
    cout << (num[z] ? "yes" : "no") << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bitset>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<bool> num = {false};
  num[0] = true;
  int a, max = 0;
  for (int i = 0; i < n; i++) {
    cin >> a;
    for (int d = max; d >= 0; d--) {
      if (num[d])
        num[d + a] = true;
    }
    max += a;
  }
  int s, z;
  cin >> s;
  for (int i = 0; i < s; i++) {
    cin >> z;
    cout << (num[z] ? "yes" : "no") << endl;
  }
  return 0;
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
#define FOR(i, b, e) for (int i = b; i < e; i++)
#define REP(i, n) FOR(i, 0, n)
using namespace std;

bool solve(vector<int> A, int p, int t) {
  if (p > A.size() || t < 0 || accumulate(A.begin() + p, A.end(), 0) < t)
    return false;
  else if (t == 0)
    return true;
  else
    return (solve(A, p + 1, t) || solve(A, p + 1, t - A[p]));
}

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

  vector<int> A;
  int n, q, x;

  cin >> n;
  REP(i, n) {
    cin >> x;
    A.push_back(x);
  }

  cin >> q;
  REP(i, q) {
    cin >> x;
    if (solve(A, 0, x)) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define FOR(i, b, e) for (int i = b; i < e; i++)
#define REP(i, n) FOR(i, 0, n)
using namespace std;

bool solve(vector<int> A, int p, int t) {
  if (p > A.size() || t < 0)
    return false;
  else if (t == 0)
    return true;
  else
    return (solve(A, p + 1, t) || solve(A, p + 1, t - A[p]));
}

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

  vector<int> A;
  int n, q, x;

  cin >> n;
  REP(i, n) {
    cin >> x;
    A.push_back(x);
  }

  cin >> q;
  REP(i, q) {
    cin >> x;
    if (solve(A, 0, x)) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
vector<int> A;

bool solve(int now, int sum) {
  if (!sum)
    return true;
  else if (now == A.size() || sum < 0)
    return false;
  return solve(now + 1, sum - A[now]) || solve(now + 1, sum);
}

int main() {
  int num1, num2, test;
  cin >> num1;
  A.resize(num1);
  for (int i = 0; i < num1; i++) {
    cin >> A[i];
  }
  cin >> num2;
  for (int i = 0; i < num2; i++) {
    cin >> test;
    if (solve(0, test) == 1)
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;
int num1, num2, test;
vector<int> A;

bool solve(int now, int sum) {
  if (!sum)
    return true;
  else if (now == num2 || sum < 0)
    return false;
  return solve(now + 1, sum - A[now]) || solve(now + 1, sum);
}

int main() {
  cin >> num1;
  A.resize(num1);
  for (int i = 0; i < num1; i++) {
    cin >> A[i];
  }
  cin >> num2;
  for (int i = 0; i < num2; i++) {
    cin >> test;
    if (solve(0, test) == 1)
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;

bool sum_binsearch(int target, int index, vector<int> &A) {
  if (target == A[index]) {
    return true;
  } else if (target < A[index] || index >= (int)A.size()) {
    return false;
  } else {
    return (sum_binsearch(target, index + 1, A) ||
            sum_binsearch(target - A[index], index + 1, A));
  }
}

int main() {
  int i;
  int m, n, q;
  vector<int> A;
  scanf("%d", &n);
  A.resize(n);
  for (i = 0; i < n; ++i) {
    scanf("%d", &A[i]);
  }
  sort(A.begin(), A.end()); // ?????????
  scanf("%d", &q);
  for (i = 0; i < q; ++i) {
    scanf("%d", &m);
    if (sum_binsearch(m, 0, A)) {
      printf("yes\\n");
    } else {
      printf("no\\n");
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;

bool sum_search(int lim, int m, vector<int> &A, vector<int> &u_idx) {
  if (m <= 0 || lim < 0)
    return false;
  for (int i = 0; i < (int)A.size(); ++i) {
    /*
                    printf("called sum_search(%d,A,u_idx)\\n",m);
                    for(int j=0; j<(int)A.size(); ++j) {
                            printf("used_index[%d]=%d\\n",j,u_idx[j]);
                    }
    */
    if (u_idx[i] != 1 && m >= A[i]) {
      if (m == A[i]) {
        return true;
      } else {
        u_idx[i] = 1;
        ++u_idx.back();
        if (sum_search(lim - 1, m - A[i], A, u_idx)) {
          /*					printf("m=%d, i=%d\\n",m,i);*/
          return true;
        } else if (u_idx.back() == (int)u_idx.size()) {
          return false;
        } else {
          u_idx[i] = 0;
          --u_idx.back();
        }
      }
    }
  }
  return false;
}

int main() {
  int i, j;
  int n, q;
  int limit;
  vector<int> A, sum;
  vector<vector<int>> used_index;
  int m;
  scanf("%d", &n);
  A.resize(n);
  sum.resize(n);
  for (i = 0; i < n; ++i) {
    scanf("%d", &j);
    A[i] = j;
  }
  sort(A.begin(), A.end());
  for (sum[0] = A[0], i = 1; i < n; ++i) {
    sum[i] = sum[i - 1] + A[i];
  }
  scanf("%d", &q);
  used_index.resize(q);
  for (i = 0; i < q; ++i) {
    used_index[i].resize(n + 1);
    scanf("%d", &m);
    j = 0;
    while (sum[j] < m && j < q) {
      ++j;
    }
    if (j == q) {
      printf("no\\n");
      continue;
    }
    limit = j;
    //		printf("m=%d,limit=%d\\n",m,limit);
    if (sum_search(limit, m, A, used_index[i])) {
      printf("yes\\n");
    } else {
      printf("no\\n");
    }
  }
  return 0;
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <iostream>
#include <string>
#include <vector>

using namespace std;

int n, q, A[25];
char t[25][2010];

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

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

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

  return 0;
}

Error: TLE

Fixed submission:



#include <iostream>
#include <string>
#include <vector>

using namespace std;

int n, q, A[25], m[210];
bool S[25];

bool checkCombination(const int *m) {
  int x = 0;
  for (int i = 0; i < n; i++) {
    x += S[i] == 1 ? A[i] : 0;
  }
  if (x == *m)
    return true;
  return false;
}
bool setFlag(int i, const int *m) {
  if (i == n) {
    return checkCombination(m);
  }

  S[i] = 0;
  bool res = setFlag(i + 1, m);
  if (res)
    return true;

  S[i] = 1;
  res = setFlag(i + 1, m);
  return res;
}
void makeCombination(const int *m) {
  for (int i = 0; i < n; i++) {
    S[i] = 0;
  }
  string result = setFlag(0, m) ? "yes" : "no";
  cout << result << endl;
}

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

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

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

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

#include <iostream>
#include <vector>

using namespace std;

bool isSumExist(vector<int> &A, int pos, int digit) {
  if (digit == 0) {
    return (true);
  }
  if (pos >= A.size() || digit < 0) {
    return (false);
  }
  return (isSumExist(A, pos + 1, digit - A[pos]) ||
          isSumExist(A, pos + 1, digit));
}

void solve() {
  int n;
  cin >> n;
  vector<int> A(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 (isSumExist(A, 0, m)) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
}

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

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

bool isSumExist(vector<int> A, int pos, int digit) {
  if (digit == 0) {
    return (true);
  }
  if (pos >= A.size() || digit < 0) {
    return (false);
  }
  return (isSumExist(A, pos + 1, digit - A[pos]) ||
          isSumExist(A, pos + 1, digit));
}

void solve() {
  int n;
  cin >> n;
  vector<int> A(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 (isSumExist(A, 0, m)) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
}

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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

#define PB push_back

bool dp[21][2001];

int main(int argc, char *argv[]) {
  int n, q, x, i, j, val, tmp;
  queue<int> m;
  int w[21];
  memset(dp, 0, sizeof(dp));
  dp[0][0] = true;

  cin >> n;
  for (i = 1; i <= n; i++)
    cin >> w[i];

  cin >> q;
  for (i = 0; i < q; i++) {
    cin >> x;
    m.push(x);
  }

  while (!m.empty()) {
    val = m.front();
    m.pop();

    for (i = 1; i <= n; i++) {
      tmp = w[i];
      for (j = 0; j <= val; j++) {

        if (j >= tmp) {
          if (dp[i - 1][j] || dp[i - 1][j - tmp])
            dp[i][j] = true;
        } else if (dp[i - 1][j])
          dp[i][j] = true;
      }
    }

    if (dp[n][val])
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

#define PB push_back

bool func(int, int, int);

vector<int> via;
queue<int> viq;
int n;

int main(int argc, char *argv[]) {
  int q, x, tmp, sum;

  // input
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x;
    via.PB(x);
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> x;
    viq.push(x);
  }

  // calc
  while (!viq.empty()) {
    tmp = viq.front();
    viq.pop();
    sum = 0;

    if (func(tmp, sum, 0))
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}

bool func(int tmp, int sum, int ind) {
  if (ind == n && sum != tmp)
    return false;
  if (sum == tmp)
    return true;

  bool flag = false;

  for (int i = ind; i < n; i++) {
    if (!flag) {
      flag = func(tmp, sum + via[i], i + 1);

      if (!flag)
        flag = func(tmp, sum, i + 1);
    } else
      break;
  }
  return flag;
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>

int element[20];
int size;

bool check(int nowPos, int val) {
  if (val == 0) {
    return true;
  }
  if (val < 0) {
    return false;
  }
  if (nowPos > size) {
    return false;
  }
  return check(nowPos + 1, val - element[nowPos]) || check(nowPos + 1, val);
}
int main(void) {
  int n, m, target;
  scanf("%d", &n);
  size = n;
  for (int i = 0; i < n; i++) {
    scanf("%d", &element[i]);
  }
  scanf("%d", &m);
  for (int i = 0; i < m; i++) {
    scanf("%d", &target);
    if (check(0, target)) {
      printf("yes\\n");
    } else {
      printf("no\\n");
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

int element[20];
int size;

bool check(int nowPos, int val) {
  if (val == 0) {
    return true;
  }
  if (val < 0) {
    return false;
  }
  if (nowPos > size) {
    return false;
  }
  return check(nowPos + 1, val - element[nowPos]) || check(nowPos + 1, val);
}
int main(void) {
  int n, m, target;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &element[i]);
  }
  scanf("%d", &m);
  size = m;
  for (int i = 0; i < m; i++) {
    scanf("%d", &target);
    if (check(0, target)) {
      printf("yes\\n");
    } else {
      printf("no\\n");
    }
  }
  return 0;
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>

using namespace std;

int A[200];
int n, q, m;

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

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

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;

int A[200];

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

int main() {
  int n, q, m;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> m;
    cout << (solve(0, m) ? "true" : "false") << endl;
  }
  return 0;
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <climits>
#include <iostream>
#include <list>   //???????????????????????????
#include <vector> //????????¢??????????????????
using namespace std;

bool solve(int i, int m, vector<int> &A) {
  if (m == 0) {
    return true;
  }

  int n = A.size();

  if (i >= n) {
    return false;
  }

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

int main(void) {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> A(n);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  int q;
  cin >> q;
  vector<int> mi(q);
  for (int i = 0; i < q; i++) {
    cin >> mi[i];
  }
  for (int j = 0; j < q; j++) {

    if (solve(0, mi[j], A)) {
      cout << "yes" << endl;
    } else
      cout << "no" << endl;
  }
}

Error: TLE

Fixed submission:

#include <climits>
#include <iostream>
#include <list>   //???????????????????????????
#include <vector> //????????¢??????????????????
using namespace std;

bool solve(int i, int m, vector<int> A) {
  if (m == 0) {
    return true;
  }

  int n = A.size();

  if (i >= n) {
    return false;
  }

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

int main(void) {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> A(n);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  int q;
  cin >> q;
  vector<int> mi(q);
  for (int i = 0; i < q; i++) {
    cin >> mi[i];
  }
  for (int j = 0; j < q; j++) {

    if (solve(0, mi[j], A)) {
      cout << "yes" << endl;
    } else
      cout << "no" << endl;
  }
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int n, m, h;
int data[21];

bool dfs(int v, int d) {
  if (v == h)
    return true;
  if (v > h || d == n)
    return false;
  bool ret = dfs(v + data[d], d + 1);
  if (!ret)
    ret = dfs(v, d + 1);
  return ret;
}

int main() {

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

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

    cin >> h;
    cout << (dfs(0, 0) ? "yes" : "no") << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int n, m, h;
int data[21];
bool solve(int n, int t) {

  if (n == h)
    return true;
  if (n > h || t == m)
    return false;
  bool ret = solve(data[t] + n, t + 1);
  if (!ret)
    ret = solve(n, t + 1);
  return ret;
}

int main() {

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

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

    cin >> h;
    cout << (solve(0, 0) ? "yes" : "no") << endl;
  }

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

#include <algorithm>
#include <cstdio>
using namespace std;
void dfs(int j, int sum);
bool memo[2001];
int a[2001];
int n;
int main() {

  int m, b;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);

  fill(memo, memo + 2001, 0);
  dfs(0, 0);

  scanf("%d", &m);
  for (int i = 0; i < m; i++) {
    scanf("%d", &b);
    if (memo[b] == 1)
      printf("yes\\n");
    else
      printf("no\\n");
  }
  return (0);
}

void dfs(int j, int sum) {
  if (memo[sum] != 1)
    memo[sum]++;
  if (j <= n) {
    dfs(j + 1, sum + a[j]);
    dfs(j + 1, sum);
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <set>
using namespace std;
int n, m, b;
set<int> se;
int a[21];
void dfs(int j, int bit, int sum);
int main() {
  cin >> n;

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

  dfs(0, 0, 0);

  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> b;
    if (se.find(b) == se.end())
      cout << "no" << endl;
    else
      cout << "yes" << endl;
  }

  return (0);
}

void dfs(int j, int bit, int sum) {

  for (int i = 0; i < n; i++) {
    if ((bit & (1 << i)) == 0) {
      if (se.find(sum) == se.end())
        se.insert(sum);
      dfs(j + 1, bit | (1 << i), sum + a[i]);
    }
  }
}