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

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

using namespace std;

int main() {
  int n, k, i = 0, j = 0;
  cin >> n >> k;
  vector<int> w(n, 0);
  vector<int> track(k, 0);
  vector<int> t_init(track);
  int P;
  int sum_w = 0;
  for (int i = 0; i < n; i++) {
    cin >> w[i];
    sum_w += w[i];
  }
  P = sum_w / k - 1;
  while (i != w.size()) {
    i = 0;
    j = 0;
    P++;
    track = t_init;
    while (i != w.size()) {
      if (track[j] + w[i] > P) {
        if (++j == k)
          break;
      } else {
        track[j] += w[i++];
      }
    }
  }
  cout << P << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

using namespace std;

int main() {
  int n, k, i = 0, j = 0;
  cin >> n >> k;
  vector<int> w(n, 0);
  vector<int> track(k, 0);
  vector<int> t_init(track);
  int P;
  int max_w = 0;
  for (int i = 0; i < n; i++) {
    cin >> w[i];
    max_w = max(max_w, w[i]);
  }
  P = max_w - 1;
  while (i != w.size()) {
    i = 0;
    j = 0;
    P++;
    track = t_init;
    while (i != w.size()) {
      if (track[j] + w[i] > P) {
        if (++j == k)
          break;
      }
      track[j] += w[i++];
    }
  }
  cout << P << endl;
  return 0;
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

bool can_make(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;
}

Error: TLE

Fixed submission:

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

using namespace std;

bool can_make(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: Time Limit Exceeded

/*
?????属????????????solve(p, t)???t==0????????贈?????????????????????
??即?則???????????????属??則???????????則????????卒??測??即??????????????????

???????????属??????????????????????????贈???????????????????????????
yes
no??則???????????則,???t==0????????贈?????贈????????賊??????????????則?????????????????属?????????
*/
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

// true ?????属??贈???????????即 yes
bool solve(int p, int t, int n, int *A) {
  if (t == 0)
    return true; //==0????????卒??贈???????其??????????
  if (p >= n)
    return false; // A???????卒???即??????????????????????????????(??賊???p?????則)
  if (t < 0)
    return false; //?其????????????則?其?????????即???????????則?????????

  // + ???????????? or
  return solve(p + 1, t - A[p], n, A) + solve(p + 1, t, n, A);
}

int main() {
  int i, j;
  int A[21];
  int buf;
  int n, q, M;

  cin >> n;
  for (i = 0; i < n; i++) {
    // scanf("%5d", &(A[i]));
    cin >> A[i];
  }

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

  return 0;
}


Error: TLE

Fixed submission:

/*
?????属????????????solve(p, t)???t==0????????贈?????????????????????
??即?則???????????????属??則???????????則????????卒??測??即??????????????????

???????????属??????????????????????????贈???????????????????????????
yes
no??則???????????則,???t==0????????贈?????贈????????賊??????????????則?????????????????属?????????
*/
#include <iostream>
#include <vector>

using namespace std;

// true ?????属??贈???????????即 yes
bool solve(int p, int t, int n, vector<int> A) {
  if (t == 0)
    return true; //==0????????卒??贈???????其??????????
  if (p >= n)
    return false; // A???????卒???即??????????????????????????????(??賊???p?????則)
  if (t < 0)
    return false; //?其????????????則?其?????????即???????????則?????????

  // + ???????????? or
  return solve(p + 1, t - A.at(p), n, A) + solve(p + 1, t, n, A);
}

int main() {
  int i, j;
  vector<int> A;
  int buf;
  int n, q, M;

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

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

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

#include <iostream>
#include <set>
#include <vector>
using namespace std;
vector<int> A;
set<int> a;
void slove(int n, int sum) {
  if (n < 0) {
    a.insert(sum);
    return;
  }
  slove(n - 1, sum);
  slove(n - 1, sum + A[n]);
}
int main(void) {
  ios_base::sync_with_stdio(false);
  int n, q, m;
  cin >> n;
  A.resize(n);
  for (int i = 0; i < n; i++)
    cin >> A[i];
  slove(n - 1, 0);
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> m;
    if (a.find(m) == a.end())
      cout << "no" << endl;
    else
      cout << "yes" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
vector<int> A;
string flag(string a, string b) {
  return (a == "yes" || b == "yes") ? "yes" : "no";
}
string slove(int n, int sum) {
  if (sum == 0)
    return "yes";
  if (sum < 0 || n < 0)
    return "no";
  return flag(slove(n - 1, sum), slove(n - 1, sum - A[n]));
}
int main(void) {
  ios_base::sync_with_stdio(false);
  int n, q, m;
  cin >> n;
  A.resize(n);
  for (int i = 0; i < n; i++)
    cin >> A[i];
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> m;
    cout << flag(slove(n - 2, m), slove(n - 2, m - A[n - 1])) << endl;
  }
  return 0;
}
Problem p02271 (C++) — buggy submission, verdict: Runtime Error

// 枝刈り無し
#include <algorithm>
#include <climits>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

int n, q;
vector<int> A;

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;
  A.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> A[i];
  }
  cin >> q;
  for (int j = 0; j < q; ++j) {
    int m;
    cin >> m;
    bool ans = solve(0, m);
    if (ans)
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }

  return 0;
}


Error: -11

Fixed submission:

// 枝刈り無し
#include <algorithm>
#include <climits>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

int n, q;
vector<int> A;

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

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

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

#include <iostream>
using namespace std;
bool solve(int i, int m, int n, int a[]) {
  if (m == 0)
    return true;
  if (i >= n)
    return false;
  return solve(i + 1, m, n, a) || solve(i + 1, m - a[i], n, a);
}

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

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
using namespace std;
bool visited[25];
bool dfs(int a[], int m, int n, int sum, int start, int cnt, int t) {
  if (cnt == n) {
    if (sum == m)
      return true;
    else
      return false;
  }
  if (sum == m)
    return true;
  if (sum > m)
    return false;
  for (int j = t; j < n; j++) {
    if (!visited[j]) {
      if (sum + a[j] > m)
        continue;
      visited[j] = 1;
      if (dfs(a, m, n, sum + a[j], j, cnt + 1, t))
        return true;
      visited[j] = 0;
    }
  }
  return false;
}

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

  for (int i = 0; i < q; i++) {
    scanf("%d", &m);
    int j;
    for (j = 0; j < n; j++) {
      if (a[j] > m)
        continue;
      memset(visited, 0, sizeof(visited));
      for (int t = 0; t <= j; t++)
        visited[t] = 1;
      if (dfs(a, m, n, a[j], j, 1, j + 1))
        break;
    }
    if (j == n)
      printf("no\\n");
    else
      printf("yes\\n");
  }
  return 0;
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
// #define Kongxiangzhouye
#define sd(x) scanf("%d", &x)
#define ss(x) scanf("%s", x)
#define sc(x) scanf("%c", &x)
#define sf(x) scanf("%f", &x)
#define slf(x) scanf("%lf", &x)
#define slld(x) scanf("%lld", &x)
#define me(x, b) memset(x, b, sizeof(x))
#define pd(d) printf("%d\\n", d);
#define plld(d) printf("%lld\\n", d)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAX_N = 100000;
const int INF = 0x3f3f3f3f;

int b[30];
int n;
// int t=0;
int solve(int i, int m) {
  //	t++;
  if (m < 0)
    return 0;
  if (m == 0)
    return 1;
  if (i >= n)
    return 0;
  int res = solve(i + 1, m) || solve(i + 1, m - b[i]);
  return res;
}
int main() {
#ifdef Kongxiangzhouye
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  ios::sync_with_stdio(false);
  int m;
  while (cin >> n) {
    //		t=0;
    for (int i = 0; i < n; i++) {
      cin >> b[i];
    }
    cin >> m;
    int te;
    for (int i = 0; i < m; i++) {
      cin >> te;
      if (solve(0, te))
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
    //	cout<<t<<endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
#define maxx 2000
using namespace std;

int n, m;
int a[maxx], q[maxx], judge[maxx];

void init() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  scanf("%d", &m);
  for (int i = 1; i <= m; i++)
    scanf("%d", &q[i]);
}

void dfs(int num, int t, int now) {
  if (num == 0)
    return;
  if (now == t) {
    judge[0] = true;
    return;
  }
  if (now > t)
    return;
  for (int i = 1; i <= n && judge[0] == false; i++)
    if (!judge[i]) {
      judge[i] = true;
      dfs(num - 1, t, now + a[i]);
      judge[i] = false;
    }
}

void deal(int x) {
  memset(judge, 0, sizeof(judge));
  for (int i = 1; i <= n; i++)
    dfs(i, x, 0);
  if (judge[0])
    printf("yes\\n");
  else
    printf("no\\n");
}

int main() {
  // freopen("std.in","r",stdin);
  // freopen("std.out","w",stdout);

  init();
  for (int i = 1; i <= m; i++)
    deal(q[i]);
  return 0;
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int n, q;
int A[21];

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() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];
  cin >> q;
  int a;
  for (int i = 0; i < q; i++) {
    cin >> a;
    if (solve(0, a))
      cout << "yes\\n";
    else
      cout << "no\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define sd(n) scanf("%d", &n)
#define pd(n) printf("%d\\n", n)
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define INF 0x3f3f3f3f
#define NIL (-1)

using namespace std;
typedef long long LL;

const int maxn = 25;

int a[maxn], n, q;
bool vis[3000];
bool b[maxn];
int flag, v;

void dfs(int x, int val) {
  if (flag)
    return;
  if (val == v) {
    flag = 1;
    return;
  }

  for (int i = 0; i < n; i++) {
    if (!b[i]) {
      b[i] = true;
      dfs(i, val + a[i]);
      b[i] = false;
    }
  }
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  cin >> q;
  while (q--) {
    cin >> v;
    flag = 0;
    memset(b, false, sizeof(b));
    if (vis[v])
      cout << "yes" << endl;
    else {
      for (int i = 0; i < n; i++) {
        if (b[i])
          continue;
        b[i] = true;
        dfs(i, 0);
        b[i] = false;
      }
      if (flag)
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
  }
  return 0;
}
Problem p02271 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#define MAX 20

int A[MAX], n;

bool 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 q, m;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &A[i]);
  }
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d", &m);
    if (check(0, m)) {
      printf("yes\\n");
    } else {
      printf("no\\n");
    }
  }
}

Error: -11

Fixed submission:

#include <stdio.h>
#define MAX 20

int A[MAX], n;

bool 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 q, m;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &A[i]);
  }
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d", m);
    if (check(0, m)) {
      printf("yes\\n");
    } else {
      printf("no\\n");
    }
  }
}
Problem p02271 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

#define N_MAX 100
#define M_MAX 100000
#define NIL -1

using namespace std;

int n;
int a[N_MAX], dp[N_MAX][M_MAX];

// can make m after a[i] or not
bool solve(int i, int m) {
  if (dp[i][m] != NIL)
    return dp[i][m];
  if (m == 0) {
    dp[i][m] = 1;
  } else if (i >= n) {
    dp[i][m] = 0;
  } else if (solve(i + 1, m)) {
    dp[i][m] = 1;
  } else if (solve(i + 1, m - a[i])) {
    dp[i][m] = 1;
  } else {
    dp[i][m] = 0;
  }
  return dp[i][m];
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= M_MAX; j++) {
      dp[i][j] = NIL;
    }
  }
  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 (solve(0, x))
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>

#define N_MAX 20
#define M_MAX 2001
#define NIL -1

using namespace std;

int n;
int a[N_MAX], dp[N_MAX][M_MAX];

// can make m after a[i] or not
bool solve(int i, int m) {
  if (dp[i][m] != NIL)
    return dp[i][m];
  if (m == 0) {
    dp[i][m] = 1;
  } else if (i >= n) {
    dp[i][m] = 0;
  } else if (solve(i + 1, m)) {
    dp[i][m] = 1;
  } else if (solve(i + 1, m - a[i])) {
    dp[i][m] = 1;
  } else {
    dp[i][m] = 0;
  }
  return dp[i][m];
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= M_MAX; j++) {
      dp[i][j] = NIL;
    }
  }
  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 (solve(0, x))
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

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

bool Judge(int m, vector<int> A) {
  int n = A.size();
  if (m == 0) {
    return true;
  }
  if (n == 1) {
    if (m == A[0]) {
      return true;
    }
  } else {
    int l = m - A.back();
    A.pop_back();
    if (Judge(m, A) || Judge(l, A)) {
      return true;
    }
  }
  return false;
}

int main() {
  int n, q; // n:??´??°???????????? q:???????????°
  cin >> n;
  vector<int> A, M;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    A.push_back(a);
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    int m;
    cin >> m;
    M.push_back(m);
  }
  for (int i = 0; i < q; i++) {
    if (Judge(M[i], A)) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

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

vector<int> Erase(vector<int> A, int i) {
  int n = A.size();
  vector<int> B;
  for (int j = 0; j < i; j++) {
    B.push_back(A[j]);
  }
  for (int j = i + 1; j < n; j++) {
    B.push_back(A[j]);
  }
  return B;
}

bool Judge(int m, vector<int> A) {
  int n = A.size();
  if (m == 0) {
    return true;
  }
  if (n == 1) {
    if (m == A[0]) {
      return true;
    }
  } else {
    int l = m - A[0];
    A = Erase(A, 0);
    if (Judge(m, A) || Judge(l, A)) {
      return true;
    }
  }
  return false;
}

int main() {
  int n, q; // n:??´??°???????????? q:???????????°
  cin >> n;
  vector<int> A, M;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    A.push_back(a);
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    int m;
    cin >> m;
    M.push_back(m);
  }
  for (int i = 0; i < q; i++) {
    if (Judge(M[i], A)) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
  return 0;
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdint>
#include <iostream>

using namespace std;

bool f(int A[], int n, int m) {
  for (int32_t bits = (1 << n) - 1; bits >= 0; bits--) {
    int sum = 0;
    for (int i = 0; i < n; i++) {
      if (bits & (1 << i)) {
        sum += A[i];
        if (sum == m) {
          return true;
        }
      }
    }
  }
  return false;
}

int main() {
  int n, q;

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

  // cin >> q;
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    int m;
    // cin >> m;
    scanf("%d", &m);
    if (f(A, n, m)) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdint>
#include <iostream>

using namespace std;

bool f(int A[], int n, int m) {
  for (int32_t bits = (1 << n) - 1; bits >= 0; bits--) {
    int sum = 0;
    for (int i = 0; i < n; i++) {
      if (bits & (1 << i)) {
        sum += A[i];
        if (sum == m) {
          return true;
        }
      }
    }
  }
  return false;
}

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

#include <iostream>
using namespace std;

int A[2010], n;

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

int main() {
  int q, m[2010], num = 0;
  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 <iostream>
using namespace std;

int A[2010];

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

int main() {
  int n, q, m[2010], num = 0;
  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;
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int n, q;
vector<int> a;

bool dfs(int target, int index, int sum) {
  if (sum == target)
    return true;
  if (index >= n)
    return false;

  bool result = false;

  result |= dfs(target, index + 1, sum);
  result |= dfs(target, index + 1, sum + a[index]);

  return result;
}

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

  cin >> n;

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

  cin >> q;

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

    cout << (dfs(m, 0, 0) ? "yes" : "no") << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

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

using namespace std;

vector<int> a;

string dfs(int m, int sum, int index) {
  if (m == sum) {
    return "yes";
  }

  if (a.size() <= index) {
    return "no";
  }

  string res;

  res = dfs(m, sum, index + 1);
  if (res == "yes")
    return "yes";

  res = dfs(m, sum + a[index], index + 1);
  if (res == "yes")
    return "yes";

  return "no";
}

int main() {
  int n, tmp;

  cin >> n;

  a.resize(n);

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

  cin >> n;

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

    cout << dfs(tmp, 0, 0) << endl;
  }

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

#include <stdio.h>

int n, A[2000];
/*判定*/
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, mi[2000], 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", &mi[i]);
  }
  for (i = 0; i < q; i++) {
    /*一致と不一致の場合*/
    if (solve(0, mi[i]))
      printf("yes\\n");
    else
      printf("no\\n");
  }
  return 0;
}


Error: 0

Fixed submission:

#include <stdio.h>

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, mi[50], 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", &mi[i]);
  }
  for (i = 0; i < q; i++) {
    /*一致と不一致の場合*/
    if (solve(0, mi[i]))
      printf("yes\\n");
    else
      printf("no\\n");
  }
  return 0;
}
Problem p02271 (C++) — buggy submission, verdict: Time Limit Exceeded

// ALDS1_5A
#include <cstdio>

int n;

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

int main() {
  int q, M, i;
  scanf("%d", &n);
  int A[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, A))
      printf("yes\\n");
    else
      printf("no\\n");
  }
  return 0;
}

Error: TLE

Fixed submission:

// ALDS1_5A
#include <cstdio>

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; q++) {
    scanf("%d", &M);
    if (solve(0, M))
      printf("yes\\n");
    else
      printf("no\\n");
  }
  return 0;
}