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

#include <bits/stdc++.h>
#include <tuple>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
#define square(x) (x) * (x)
#define cube(x) (x) * (x) * (x)

const int INF = 1e9;

using namespace std;

bool solve(vector<int> a, int target) {
  int last = a[a.size() - 1];
  if (last == target)
    return true;
  a.pop_back();
  if (a.empty())
    return false;
  return (solve(a, target - last) || solve(a, target));
}

int main() {
  int n, q, x;
  vector<int> a;
  scanf("%d", &n);
  REP(i, n) {
    scanf("%d", &x);
    a.push_back(x);
  }
  scanf("%d", &q);
  REP(i, q) {
    scanf("%d", &x);
    if (solve(a, x))
      printf("yes\\n");
    else
      printf("no\\n");
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <tuple>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
#define square(x) (x) * (x)
#define cube(x) (x) * (x) * (x)

const int INF = 1e9;

using namespace std;

bool solve(vector<int> a, int target) {
  int last = a[a.size() - 1];
  if (last == target)
    return true;
  a.pop_back();
  if (a.empty())
    return false;
  return (solve(a, target) || solve(a, target - last));
}

int main() {
  int n, q, x;
  vector<int> a;
  scanf("%d", &n);
  REP(i, n) {
    scanf("%d", &x);
    a.push_back(x);
  }
  scanf("%d", &q);
  REP(i, q) {
    scanf("%d", &x);
    if (solve(a, x))
      printf("yes\\n");
    else
      printf("no\\n");
  }
  return 0;
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int n, A[30];

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;
  int M[210];

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

Error: TLE

Fixed submission:

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

int n, A[30];

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;
  int M[210];

  cin >> n;
  for (int i = 0; i < n; n++) {
    cin >> A[i];
  }
  cin >> q;
  for (int j = 0; j < q; j++) {
    cin >> M[j];
  }
  for (int j = 0; j < q; j++) {
    if (solve(0, M[j]))
      cout << "yes" << endl;
    else {
      cout << "no" << endl;
    }
  }
  return 0;
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int data[20], n;

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

  return false;
}

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

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int data[20], n;

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

  return false;
}

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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <list>
#include <vector>

using namespace std;

int A[25];
int n;

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

int main() {

  int 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;
    if (add(m, 0))
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <list>
#include <vector>

using namespace std;

int A[25];
int q;

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

int main() {

  int n, 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 (add(m, 0))
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <string>
#include <typeinfo>
#include <vector>
#define _USE_MATH_DEFINES
#include <cmath>
#include <queue>
#include <stack>

using namespace std;

int n, q;
vector<int> A(2000);

int solve(int i, int v) {
  if (v == 0) {
    return 1;
  }
  if (i >= n) {
    return 0;
  }
  int res;
  if (A[i] <= v) {
    res = solve(i + 1, v) || solve(i + 1, v - A[i]);
  } else {
    res = solve(i + 1, v);
  }
  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 (solve(0, M)) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <string>
#include <typeinfo>
#include <vector>
#define _USE_MATH_DEFINES
#include <cmath>
#include <queue>
#include <stack>

using namespace std;

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

int main() {

  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 (solve(0, M, A, n)) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }

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

/**
filename:5_A-Exhaustive_Search.c
created on 21.9.2015 by charis
edited on 21.9.2015 by charis
*/

#include <cstdio>
/*
int n;
int S[10000];
int r;

void solve(int i, int A[], int m){
        if(i == n) {
                int s = 0;
                for(int k=0; k<n; k++) {
                        if( S[k] == 1) s += A[k] ;
                        if (s == m) r=1 ;
                }
                if (s != m) r!= 0;
        }
        else {
                solve(i + 1, A, m) ;
                S[i] = 1 ;
                solve(i + 1, A, m) ;
                S[i] = 0 ;
        }
}
          time over
*/
int n;

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

  res = solve(i + 1, A, m, res) || solve(i + 1, A, m, res + A[i]);

  return res;
}

int main() {
  int q, m;
  scanf("%d", &n);
  int A[n];
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);

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

  return 0;
}

Error: TLE

Fixed submission:

/**
filename:5_A-Exhaustive_Search.c
created on 21.9.2015 by charis
edited on 21.9.2015 by charis
*/

#include <cstdio>

int n;
int S[10000];
int r;

void solve(int i, int A[], int m) {
  if (i == n) {
    int s = 0;
    for (int k = 0; k < n; k++) {
      if (S[k] == 1)
        s += A[k];
      if (s == m)
        r = 1;
    }
    if (s != m)
      r != 0;
  } else {
    solve(i + 1, A, m);
    S[i] = 1;
    solve(i + 1, A, m);
    S[i] = 0;
  }
}

int main() {
  int q, m;
  scanf("%d", &n);
  int A[n];
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);

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

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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); i++)
typedef vector<int> VI;

int main() {
  int n;
  cin >> n;
  VI a(n);
  rep(i, n) cin >> a[i];

  int q;
  cin >> q;

  const int M_MAX = 2001;
  VI canMake(M_MAX);
  rep(st, 1 << n) {
    int val = 0;
    rep(i, n) if (st & (1 << i)) val += a[i];
    if (val < M_MAX)
      canMake[val] = true;
  }

  rep(qi, q) {
    int m;
    cin >> m;
    if (canMake[m])
      cout << "yes\\n";
    else
      cout << "no\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); i++)
typedef vector<int> VI;

int main() {
  int n;
  cin >> n;
  VI a(n);
  rep(i, n) cin >> a[i];

  int q;
  cin >> q;

  rep(qi, q) {
    int m;
    cin >> m;
    bool canMake = false;
    rep(st, 1 << n) {
      int val = 0;
      rep(i, n) if (st & (1 << i)) val += a[i];
      if (val == m) {
        canMake = true;
        break;
      }
    }
    if (canMake)
      cout << "yes\\n";
    else
      cout << "no\\n";
  }
  return 0;
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

// void b_search(bool &flag, vector<int> a, int n, int m, int sum, int i){
//	if (sum == m) flag = true;
//	if (i == n || sum == m) return;
//	if (sum + a[i] <= m) b_search(flag, a, n, m, sum + a[i], i + 1);
//	b_search(flag, a, n, m, sum, i + 1);
//
// }
//
// void solve(){
//	bool flag;
//	int n, q, mi;
//	cin >> n;
//	vector<int> A(n);
//	for (int i = 0; i < n; i++) cin >> A[i];
//	sort(A.begin(), A.end());
//	cin >> q;
//	for (int i = 0; i < q; i++){
//		cin >> mi;
//		flag = false;
//		b_search(flag, A, n, mi, 0, 0);
//		if (flag) cout << "yes" << endl;
//		else cout << "no" << endl;
//	}
// }

void solve() {
  bool flag;
  int n, q, ai, mi, max = 0;
  bool dp[2000];
  fill(dp, dp + 2000, false);
  dp[0] = true;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> ai;
    for (int j = max; j >= 0; j--) {
      if (dp[j])
        dp[j + ai] = true;
    }
    max += ai;
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> mi;
    if (dp[mi])
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
}

signed main() { solve(); }

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

void b_search(bool &flag, vector<int> a, int n, int m, int sum, int i) {
  if (sum == m)
    flag = true;
  if (i == n || sum == m)
    return;
  if (sum + a[i] <= m)
    b_search(flag, a, n, m, sum + a[i], i + 1);
  b_search(flag, a, n, m, sum, i + 1);
}

void solve() {
  bool flag;
  int n, q, mi;
  cin >> n;
  vector<int> A(n);
  for (int i = 0; i < n; i++)
    cin >> A[i];
  sort(A.begin(), A.end());
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> mi;
    flag = false;
    b_search(flag, A, n, mi, 0, 0);
    if (flag)
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
}

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

#include <cstdint>
#include <vector>

using namespace std;

static bool aux(vector<uint32_t> &vec, uint32_t index, int32_t val) {
  if (val == 0)
    return true;

  if (vec.size() == index || (val < 0)) {
    return false;
  }

  int32_t rem = val - vec[index];

  return aux(vec, index + 1, rem) || aux(vec, index + 1, val);
}

bool exausitve_search_search(vector<uint32_t> vec, int32_t val) {
  return aux(vec, 0, val);
}

#if !defined(TEST)
#include <iostream>

static vector<uint32_t> array;

int32_t main() {
  uint32_t num;
  cin >> num;

  for (uint32_t i = 0; i < num; i++) {
    uint32_t val;
    cin >> val;
    array.push_back(val);
  }

  cin >> num;

  for (uint32_t i = 0; i < num; i++) {
    uint32_t val;
    cin >> val;

    if (exausitve_search_search(array, val)) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }

  return 0;
}

#endif

Error: TLE

Fixed submission:

#include <cstdint>
#include <vector>

using namespace std;

static bool aux(vector<uint32_t> vec, uint32_t index, int32_t val) {
  if (val == 0)
    return true;

  if (vec.size() == index || (val < 0)) {
    return false;
  }

  int32_t rem = val - vec[index];

  return aux(vec, index + 1, rem) || aux(vec, index + 1, val);
}

bool exausitve_search_search(vector<uint32_t> vec, int32_t val) {
  return aux(vec, 0, val);
}

#if !defined(TEST)
#include <iostream>

static vector<uint32_t> array;

int32_t main() {
  uint32_t num;
  cin >> num;

  for (uint32_t i = 0; i < num; i++) {
    uint32_t val;
    cin >> val;
    array.push_back(val);
  }

  cin >> num;

  for (uint32_t i = 0; i < num; i++) {
    uint32_t val;
    cin >> val;

    if (exausitve_search_search(array, val)) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }

  return 0;
}

#endif
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

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

bool x[5000000];

int main() {
  memset(x, false, sizeof(x));
  int n, q, y[20], z[5000], power[21], a[20], sum;
  power[0] = 1;
  for (int i = 1; i <= 20; i++) {
    power[i] = power[i - 1] * 2;
  }
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> y[i];
  }
  for (int i = 0; i < power[n]; i++) {
    sum = 0;
    for (int j = 0; j < n; j++) {
      a[j] = (i / power[j]) % 2;
      if (a[j] == 1) {
        sum += y[j];
      }
    }
    x[sum] = true;
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> z[i];
    if (x[z[i]] == true) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

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

bool x[50000];

int main() {
  memset(x, false, sizeof(x));
  int n, q, y[20], z[500], power[20], a[20], sum;
  power[0] = 1;
  for (int i = 1; i < 20; i++) {
    power[i] = power[i - 1] * 4;
  }
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> y[i];
  }
  for (int i = 0; i < power[n]; i++) {
    sum = 0;
    for (int j = 0; j < n; j++) {
      a[j] = (i / power[j]) % 2;
      if (a[j] == 1) {
        sum += y[j];
      }
    }
    x[sum] = true;
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> z[i];
    if (x[z[i]] == true) {
      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;

int main() {
  cin.tie(nullptr);
  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> Q(q);
  map<int, bool> mp; // {value, existed}
  for (int i = 0; i < q; i++) {
    cin >> Q[i];
    mp[Q[i]] = false;
  }

  for (int i = 0; i < (1 << n); i++) {
    int cost = 0;
    for (int j = 0; j < n; j++) {
      if ((i >> j) & 1) {
        cost += A[j];
      }
    }
    if (mp.count(cost)) {
      mp[cost] = true;
    }
  }

  for (int i = 0; i < q; i++) {
    cout << (mp[Q[i]] ? "yes" : "no") << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int n, q;
  cin >> n;
  int A[n];
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    int tmp;
    cin >> tmp;
    bool f = false;
    for (int j = 0; j < (1 << n); j++) {
      int total = 0;
      for (int k = 0; k < n; k++) {
        if ((j >> k) & 1)
          total += A[k];
      }
      if (total == tmp) {
        f = true;
        break;
      }
    }
    if (f)
      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 n;
vector<int> A(50);

bool calc(int i, int m) {
  if (m == 0)
    return true;
  if (i > n)
    return false;

  int ans = calc(i + 1, m) || calc(i + 1, m - A[i]);
  return ans;
}

int main() {
  cin >> n;

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

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

    if (calc(0, x))
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

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

int n;
vector<int> A(25);

bool calc(int i, int m) {
  if (m == 0)
    return true;
  if (i > m)
    return false;

  int ans = calc(i + 1, m) || calc(i + 1, m - A[i]);
  return ans;
}

int main() {
  cin >> n;

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

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

    if (calc(0, x))
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }

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

#include <cstdio>

bool solve(int A[], int size, int index, int remain) {
  bool a, b;
  if (remain == 0) {
    return true;
  } else if (size <= index || remain < 0) {
    return false;
  } else {
    a = solve(A, size, index + 1, remain);
    b = solve(A, size, index + 1, remain - A[index]);
  }
  return a || b;
}
int main() {
  int n, q, A[2000], M[2000];

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

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

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>

bool solve(int A[], int size, int index, int remain) {
  bool a, b;
  if (remain == 0) {
    return true;
  } else if (size <= index || remain < 0) {
    return false;
  } else {
    a = solve(A, size, index + 1, remain);
    b = solve(A, size, index + 1, remain - A[index]);
  }
  return a || b;
}
int main() {
  int n, q, A[2000], M[2000];

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

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

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

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

int main() {
  int n, q, m, num = 0, sum = 0;
  vector<int> A, Q;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> m;
    A.push_back(m);
    sum += m;
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> m;
    Q.push_back(m);

    if (m > num)
      num = m;
  }

  int M = max(num, sum);
  vector<vector<bool>> B;

  B.resize(A.size() + 1);
  for (int i = 0; i < A.size() + 1; i++)
    B[i].resize(M + 1);

  B[0][0] = true;
  for (int j = 1; j < M + 1; j++)
    B[0][j] = false;

  for (int i = 1; i < A.size() + 1; i++) {
    for (int j = 0; j < M + 1; j++) {
      if (j < A[i - 1])
        if (B[i - 1][j])
          B[i][j] = true;
        else
          B[i][j] = false;
      else if (B[i - 1][j] || B[i - 1][j - A[i - 1]])
        B[i][j] = true;
      else
        B[i][j] = false;
    }
  }

  for (int i = 0; i < q; i++) {
    if (B[A.size()][Q[i]])
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

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

  if (i >= A.size())
    return false;

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

int main() {
  int n, q, num, m;
  vector<int> A;

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

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

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

#include <stdio.h>

int a[200], b;
int a_num, b_num, j = 0;

int solve(int, int);
int main() {

  int b_num, i, j = 0;

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

  scanf("%d", &b_num);
  for (i = 0; i < b_num; i++) {
    scanf("%d", &b);
    j = 0;
    if (solve(j, b) == 1) {
      printf("yes\\n");
    }
    if (solve(j, b) == 0) {
      printf("no\\n");
    }
  }

  return 0;
}

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


Error: TLE

Fixed submission:

#include <stdio.h>

int a[200], b;

int solve(int, int);
int main() {

  int a_num, b_num, i, j;

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

  scanf("%d", &b_num);
  for (i = 0; i < b_num; i++) {
    scanf("%d", &b);

    if (solve(0, b) == 1) {
      printf("yes\\n");
    } else if (solve(i, b) == 0) {
      printf("no\\n");
    }
  }

  return 0;
}

int solve(int i, int m) {
  int res;
  if (m == 0)
    return 1;
  else if (i >= m)
    return 0;

  res = solve(i + 1, m) || solve(i + 1, m - a[i]);
  return res;
}
Problem p02271 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int m, n, q, A[20];
int ans(int x, int y) {
  if (x == 0)
    return 0;
  if (y >= n)
    return 1;
  if (ans(x, y + 1) == 0 || ans(x - A[y], y + 1) == 0)
    return 0;
  else
    return 1;
}

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;
    if (ans(m, 0) == 0)
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int m, n, q, A[20];
int ans(int x, int y) {
  if (x == 0)
    return 0;
  if (ans(x, y + 1) == 0 || ans(x - A[y], y + 1) == 0)
    return 0;
  else
    return 1;
}

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;
    if (ans(m, 0) == 0)
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
  return 0;
}