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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream> // require freopen
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
using namespace std;
#define REP(i, m, n) for (int i = m; i < n; i++)
#define rep(i, n) REP(i, 0, n)
int b[100], m, n;
bool C(int d) {
  int v[20];
  rep(i, m) v[i] = 0;
  int ii = 0;
  rep(i, n) {
    while (ii < m && v[ii] + b[i] > d)
      ii++;
    if (ii == m)
      return false;
    v[ii] += b[i];
  }
  return true;
}
int main(void) {
  //	cut here before submit
  //	freopen ("testcase.persistence", "r", stdin );

  for (;;) {
    scanf("%d%d", &m, &n);
    if (!m)
      break;
    rep(i, n) scanf("%d", b + i);
    int l, r, mid, res = 9999999;
    l = 0;
    r = 1500000;
    while (l <= r) {
      mid = (l + r) / 2;
      // printf("%d %d %d\\n",l,mid,r);
      if (C(mid)) {
        res = min(res, mid);
        r = mid - 1;
      } else
        l = mid + 1;
    }
    printf("%d\\n", res);
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int calc_m(vector<int> w, int curr_w) {
  int n = w.size();
  int cm = 0;
  int j = 0;
  while (j < n) {
    int cw = 0;
    while (cw + w[j] <= curr_w && j < n) {
      cw += w[j];
      j++;
    } // end while
    cm++;
  } // end if

  return cm;
}

int main() {
  const int MaxW = 1500000;
  int m, n; // {IÌi 1  m  20, {Ìª 1  n 
            // 100

  while (cin >> m >> n && m && n) {
    vector<int> w(n, 0); // {Ì 1  w[i]  1000000
    int res = 0;         // {IÌ ans  1500000
    for (int i = 0; i < n; i++) {
      cin >> w[i];
    } // end for
    int l = w[0];
    int r = MaxW;
    int mid = 0;
    while (l <= r) {
      mid = (l + r) / 2;
      int curr_m = calc_m(w, mid);
      if (curr_m > m) {
        l = mid + 1;
      } else {
        r = mid - 1;
      } // end if
    }   // end while
    cout << mid << endl;
  } // end loop

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

#include <iostream>

using namespace std;
int m, n, w[100];

int calc(int t) {
  int sum = 0, ret = 1;
  for (int i = 0; i < n; i++) {
    if (w[i] > t)
      return 1500001;
    if (sum + w[i] > t) {
      sum = 0;
      ret++;
    }
    sum += w[i];
  }
  return ret;
}

int main() {

  while (1) {
    cin >> m >> n;
    if (m == 0 && n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> w[i];
    }

    int l = 1, h = 1500000;
    while (h - l > 1) {
      int md = (l + h) / 2;
      if (calc(md) > m)
        l = md;
      else
        h = md;
    }

    cout << h << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;
int m, n, w[100];

int calc(int t) {
  int sum = 0, ret = 1;
  for (int i = 0; i < n; i++) {
    if (w[i] > t)
      return 1500001;
    if (sum + w[i] > t) {
      sum = 0;
      ret++;
    }
    sum += w[i];
  }
  return ret;
}

int main() {

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

    int l = 1, h = 1500000;
    while (h - l > 1) {
      int md = (l + h) / 2;
      if (calc(md) > m)
        l = md;
      else
        h = md;
    }

    cout << h << endl;
  }

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

#include <algorithm>
#include <stdio.h>
using namespace std;
int c[60];
int v[60];
int u[60];
int n;
int solve(int a, int b) {
  if (b == 0) {
    bool ok = true;
    for (int i = 0; i < n; i++)
      if (!u[i]) {
        ok = false;
        break;
      }
    if (ok)
      return 1;
    int at = 0;
    for (int i = 0; i < n; i++) {
      if (v[i]) {
        v[i] = 0;
        int res = solve(0, c[i]);
        if (res)
          return 1;
        v[i] = 1;
      }
      if (!u[i])
        break;
    }
    return 0;
  }
  if (a == n)
    return 0;
  if (!u[a] && b >= c[a]) {
    u[a] = v[a] = 1;
    if (solve(a + 1, b - c[a]))
      return 1;
    u[a] = v[a] = 0;
  }
  int rem = 0;
  for (int i = a + 1; i < n; i++)
    if (b >= c[i] && !u[i])
      rem += c[i];
  if (rem < b)
    return 0;
  return solve(a + 1, b);
}
int main() {
  int a;
  while (scanf("%d", &a), a) {
    n = a;
    for (int i = 0; i < a; i++)
      scanf("%d", c + i);
    for (int i = 0; i < a; i++)
      c[i] = -c[i];
    std::sort(c, c + a);
    for (int i = 0; i < a; i++)
      c[i] = -c[i];
    for (int i = 0; i < a; i++)
      v[i] = u[i] = 0;
    v[0] = u[0] = 1;
    if (solve(0, 0))
      printf("YES\\n");
    else
      printf("NO\\n");
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
using namespace std;
int c[60];
int v[60];
int u[60];
int n;
int solve(int a, int b) {
  if (b == 0) {
    bool ok = true;
    for (int i = 0; i < n; i++)
      if (!u[i]) {
        ok = false;
        break;
      }
    if (ok)
      return 1;
    int at = 0;
    for (int i = 0; i < n; i++) {
      if (v[i]) {
        v[i] = 0;
        int res = solve(0, c[i]);
        if (res)
          return 1;
        v[i] = 1;
      }
    }
    return 0;
  }
  if (a == n)
    return 0;
  if (!u[a] && b >= c[a]) {
    u[a] = v[a] = 1;
    if (solve(a + 1, b - c[a]))
      return 1;
    u[a] = v[a] = 0;
  }
  int rem = 0;
  for (int i = a + 1; i < n; i++)
    if (b >= c[i] && !u[i])
      rem += c[i];
  if (rem < b)
    return 0;
  return solve(a + 1, b);
}
int main() {
  int a;
  while (scanf("%d", &a), a) {
    n = a;
    for (int i = 0; i < a; i++)
      scanf("%d", c + i);
    for (int i = 0; i < a; i++)
      c[i] = -c[i];
    std::sort(c, c + a);
    for (int i = 0; i < a; i++)
      c[i] = -c[i];
    for (int i = 0; i < a; i++)
      v[i] = u[i] = 0;
    v[0] = u[0] = 1;
    if (solve(0, 0))
      printf("YES\\n");
    else
      printf("NO\\n");
  }
}
Problem p00182 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <functional>

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

using namespace std;

bool dfs(int i);

int n, a[50];

bool fil[50];

bool dfs2(int i, int j, int rem) { // partition
  if (rem == 0)
    return dfs(i + 1);
  if (j == n)
    return false;

  if (!fil[j] && a[j] <= rem) {
    fil[j] = true;
    if (dfs2(i, j + 1, rem - a[j]))
      return true;
    fil[j] = false;
  }
  return dfs2(i, j + 1, rem);
}

bool dfs(int i) {
  if (i == n)
    return true;

  if (!fil[i])
    return false;

  return dfs2(i, i + 1, a[i]) || dfs(i + 1);
}

int main() {
  for (; scanf("%d", &n), n;) {
    rep(i, n) {
      scanf("%d", a + i);
      fil[i] = false;
    }
    sort(a, a + n, greater<int>());
    fil[0] = true;
    puts(dfs(0) ? "YES" : "NO");
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <functional>

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

using namespace std;

bool dfs(int i);

int n, a[50];

bool fil[50];

bool dfs2(int i, int j, int rem) { // partition
  if (rem == 0)
    return dfs(i + 1);
  if (j == n)
    return false;

  if (!fil[j] && a[j] <= rem) {
    fil[j] = true;
    if (dfs2(i, j + 1, rem - a[j]))
      return true;
    fil[j] = false;
  }
  return dfs2(i, j + 1, rem);
}

int dp[15][1 << 15];
bool dfs(int i) {
  if (i == n)
    return true;

  if (!fil[i])
    return false;

  int S = 0;
  if (n - i < 15) {
    rep(j, n - i) if (fil[i + j]) S |= 1 << j;
    if (dp[n - i][S] != -1)
      return dp[S];
  }

  bool res = dfs(i + 1) || dfs2(i, i + 1, a[i]);
  if (n - i < 15)
    dp[n - i][S] = res;
  return res;
}

int main() {
  for (; scanf("%d", &n), n;) {
    rep(i, n) {
      scanf("%d", a + i);
      fil[i] = false;
    }
    sort(a, a + n, greater<int>());
    fil[0] = true;
    rep(i, 15) rep(S, 1 << 15) dp[i][S] = -1;
    puts(dfs(0) ? "YES" : "NO");
  }

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

#include <algorithm>
#include <bitset>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;

const int N = 50;

int n;
vector<int> beaker;
bitset<N> full;
bitset<N> used;

bool pour(int water, int start);
bool check();

bool pour(int water, int start) {
  for (int i = start; i < n; ++i) {
    if (used[i])
      continue;

    if (water < beaker[i])
      continue;
    else if (water == beaker[i]) {
      full.set(i);
      used.set(i);
      if (check())
        return true;
      full.reset(i);
      used.reset(i);
    } else {
      full.set(i);
      used.set(i);
      if (pour(water - beaker[i], i + 1))
        return true;
      full.reset(i);
      used.reset(i);
    }
  }
  return false;
}

bool check() {
  if (used.count() == n)
    return true;
  for (int i = 0; i < n; ++i) {
    if (!used[i] && !used[i])
      return false;
    if (!full[i])
      continue;
    full.reset(i);
    if (pour(beaker[i], i + 1))
      return true;
    full.set(i);
  }
  return false;
}

int main() {
  while (cin >> n, n) {
    beaker.resize(n);
    for (auto &b : beaker)
      cin >> b;
    sort(beaker.begin(), beaker.end(), greater<int>());
    full = bitset<N>(1);
    used = bitset<N>(1);
    cout << (check() ? "YES" : "NO") << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <functional>
#include <iostream>
#include <set>
#include <tuple>
#include <vector>
using namespace std;

const int N = 50;
typedef tuple<string, string> Node;

int n;
vector<int> beaker;
bitset<N> full;
bitset<N> used;
set<Node> memo;

bool pour(int water, int start);
bool check();

bool pour(int water, int start) {
  for (int i = start; i < n; ++i) {
    if (full[i] || used[i])
      continue;

    if (water < beaker[i])
      continue;
    else if (water == beaker[i]) {
      full.set(i);
      used.set(i);
      if (check())
        return true;
      full.reset(i);
      used.reset(i);
    } else {
      full.set(i);
      used.set(i);
      if (pour(water - beaker[i], i + 1))
        return true;
      full.reset(i);
      used.reset(i);
    }
  }
  return false;
}

bool check() {
  if (memo.count(Node(full.to_string(), used.to_string())))
    return false;
  memo.insert(Node(full.to_string(), used.to_string()));
  if (used.count() == n)
    return true;
  for (int i = 0; i < n; ++i) {
    if (!full[i])
      continue;
    full.reset(i);
    if (pour(beaker[i], i + 1))
      return true;
    full.set(i);
  }
  return false;
}

int main() {
  while (cin >> n, n) {
    beaker.resize(n);
    for (auto &b : beaker)
      cin >> b;
    sort(beaker.begin(), beaker.end(), greater<int>());
    full = bitset<N>(1);
    used = bitset<N>(1);
    memo.clear();
    cout << (check() ? "YES" : "NO") << endl;
  }
}
Problem p00182 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <set>
using namespace std;

typedef long long ll;

int n;
int lim[52], vol[52];
set<ll> no[52];

bool solve(int idx);

bool distribute(int idx, int next) {
  if (idx == n) {
    return false;
  }

  ll bit = 0;

  for (int i = idx; i < n; i++) {
    bit <<= 1;
    if (vol[i] == lim[i]) {
      bit++;
    }
  }

  if (no[idx].find(bit) != no[idx].end()) {
    return false;
  }
  no[idx].insert(bit);

  for (int i = next; i < n; i++) {
    if (vol[i] != 0)
      continue;
    if (vol[idx] < lim[i])
      continue;
    if (i - 1 != idx && lim[i - 1] == lim[i] && vol[i - 1] == 0)
      continue;
    vol[idx] -= lim[i];
    vol[i] = lim[i];

    if (vol[idx] != 0 && distribute(idx, i + 1))
      return true;
    if (vol[idx] == 0 && solve(idx + 1))
      return true;

    vol[idx] += lim[i];
    vol[i] = 0;
  }

  return false;
}

bool solve(int idx) {
  if (idx == n)
    return true;
  if (vol[idx] == 0)
    return false;

  if (solve(idx + 1))
    return true;
  if (distribute(idx, idx + 1))
    return true;

  return false;
}

int main() {
  while (cin >> n, n) {
    for (int i = 0; i < n; i++) {
      cin >> lim[i];
      no[i].clear();
      vol[i] = 0;
    }
    sort(lim, lim + n, greater<int>());

    vol[0] = lim[0];
    cout << (solve(0) ? "YES" : "NO") << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
using namespace std;

typedef long long ll;

int n;
int lim[52], vol[52];
set<ll> no[52];

bool solve(int idx);

bool distribute(int idx, int next) {
  if (idx == n) {
    return false;
  }

  ll bit = 0;

  for (int i = idx; i < n; i++) {
    bit <<= 1;
    if (vol[i] == lim[i]) {
      bit++;
    }
  }

  if (no[idx].find(bit) != no[idx].end()) {
    return false;
  }
  no[idx].insert(bit);

  for (int i = next; i < n; i++) {
    if (vol[i] != 0)
      continue;
    if (vol[idx] < lim[i])
      continue;
    vol[idx] -= lim[i];
    vol[i] = lim[i];

    if (vol[idx] != 0 && distribute(idx, i + 1))
      return true;
    if (vol[idx] == 0 && solve(idx + 1))
      return true;

    vol[idx] += lim[i];
    vol[i] = 0;
  }

  return false;
}

bool solve(int idx) {
  if (idx == n)
    return true;
  if (vol[idx] == 0)
    return false;

  if (solve(idx + 1))
    return true;
  if (distribute(idx, idx + 1))
    return true;

  return false;
}

int main() {
  while (cin >> n, n) {
    for (int i = 0; i < n; i++) {
      cin >> lim[i];
      no[i].clear();
      vol[i] = 0;
    }
    sort(lim, lim + n, greater<int>());

    vol[0] = lim[0];
    cout << (solve(0) ? "YES" : "NO") << endl;
  }
}
Problem p00182 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int u[99], w[99], fl[99], n;
bool rec(int c, int s, int from) {
  int prev = -1, f;
  if (c == 0) {
    for (s = 0; s < n && u[s]; s++)
      ;
    if (s == n)
      return 1;
    for (int i = from + 1; i < n; i++)
      if (fl[i] && w[i] != prev) {
        fl[i] = 0;
        prev = w[i];
        f = rec(prev, i + 1, i);
        fl[i] = 1;
        if (f)
          return 1;
      }
  } else {
    for (int i = s; i < n; i++)
      if (!u[i] && w[i] != prev && w[i] <= c) {
        u[i] = fl[i] = 1;
        f = rec(c - w[i], i + 1, from);
        u[i] = fl[i] = 0;
        prev = w[i];
        if (f)
          return 1;
      }
  }
  return 0;
}
int main() {
  while (cin >> n, n) {
    rep(i, n) cin >> w[i], u[i] = fl[i] = 0;
    sort(w, w + n, greater<int>());
    u[0] = 1;
    cout << (n == 1 || rec(w[0], 1, 0) ? "YES" : "NO") << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int u[99], w[99], fl[99], n;
bool rec(int c, int s) {
  int prev = -1, f;
  if (c == 0) {
    for (s = 0; s < n && u[s]; s++)
      ;
    if (s == n)
      return 1;
    for (int i = 1; i < n; i++)
      if (fl[i] && w[i] != prev) {
        fl[i] = 0;
        prev = w[i];
        f = rec(prev, i + 1);
        fl[i] = 1;
        if (f)
          return 1;
      }
  } else {
    for (int i = s; i < n; i++)
      if (!u[i] && w[i] != prev && w[i] <= c) {
        u[i] = fl[i] = 1;
        f = rec(c - w[i], i + 1);
        u[i] = fl[i] = 0;
        prev = w[i];
        if (f)
          return 1;
      }
  }
  return 0;
}
int main() {
  while (cin >> n, n) {
    rep(i, n) cin >> w[i], u[i] = fl[i] = 0;
    sort(w, w + n, greater<int>());
    u[0] = 1;
    cout << (n == 1 || rec(w[0], 1) ? "YES" : "NO") << endl;
  }
  return 0;
}
Problem p00182 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

bool dfs(int i);
int n, a[50];
bool fil[50];

bool dfs2(int i, int j, int rem) {
  if (rem == 0)
    return dfs(i + 1);
  if (j == n)
    return false;

  if (!fil[j] && a[j] <= rem) {
    fil[j] = true;
    if (dfs2(i, j + 1, rem - a[j]))
      return true;
    fil[j] = false;
  }
  return dfs2(i, j + 1, rem);
}

bool dfs(int i) {
  if (i == n)
    return true;
  if (!fil[i])
    return false;
  return dfs2(i, i + 1, a[i]) || dfs(i + 1);
}

int main() {
  for (; scanf("%d", &n), n;) {
    rep(i, n) {
      scanf("%d", a + i);
      fil[i] = false;
    }
    sort(a, a + n, greater<int>());
    fil[0] = true;
    puts(dfs(0) ? "YES" : "NO");
  }

  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

bool dfs(int i);
int n, a[50];
bool fil[50];

bool dfs2(int i, int j, int rem) {
  if (rem == 0)
    return dfs(i + 1);
  if (j == n)
    return false;

  if (!fil[j] && a[j] <= rem) {
    fil[j] = true;
    if (dfs2(i, j + 1, rem - a[j]))
      return true;
    fil[j] = false;
  }
  return dfs2(i, j + 1, rem);
}

bool dfs(int i) {
  if (i == n)
    return true;
  if (!fil[i])
    return false;
  return dfs2(i, i + 1, a[i]) || dfs(i + 1);
}

int main() {
  for (; scanf("%d", &n), n;) {
    rep(i, n) {
      scanf("%d", a[i]);
      fil[i] = false;
    }
    sort(a, a + n, greater<int>());
    fil[0] = true;
    puts(dfs(0) ? "YES" : "NO");
  }

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

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

int n;
int a[50];
bool b[50];

bool pourAll(int p);

bool pour(int p, int q, int rest) {
  if (!rest) {
    return pourAll(p + 1);
  }
  if (q == n) {
    return false;
  }
  if (!b[q] && a[q] <= rest) {
    b[q] = true;
    if (pour(p, q + 1, rest - a[q])) {
      return true;
    }
    b[q] = false;
  }
  return pour(p, q + 1, rest);
}

bool pourAll(int p) {
  if (p == n) {
    return true;
  }
  if (!b[p]) {
    return false;
  }
  if (pour(p, p + 1, a[p])) {
    return true;
  }
  return pourAll(p + 1);
}

bool solve() {
  sort(a, a + n, greater<int>());
  b[0] = true;
  for (int i = 1; i < n; i++) {
    b[i] = false;
  }

  return pourAll(0);
}

int main() {
  bool ans;
  while (1) {
    cin >> n;
    if (!n) {
      break;
    }
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    ans = solve();
    cout << (ans ? "YES" : "NO") << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int n;
int a[50];
int b[50];
int finished;

bool dfs(int p) {
  if (p - finished > n - p) {
    return false;
  }
  for (int i = 0; i < p; i++) {
    if (b[i] < a[p]) {
      continue;
    }
    bool failed = false;

    if (b[i] == a[i]) {
      finished--;
    }
    b[i] -= a[p];
    b[p] = a[p];
    finished++;
    if (!b[i]) {
      finished++;
    }

    if (b[i] && b[i] < a[n - 1]) {
      failed = true;
    }

    if (!failed) {
      if (p == n - 1) {
        if (finished == n) {
          return true;
        }
      } else {
        bool ret = dfs(p + 1);
        if (ret) {
          return true;
        }
      }
    }

    finished--;
    if (!b[i]) {
      finished--;
    }
    b[i] += a[p];
    b[p] = 0;
    if (b[i] == a[i]) {
      finished++;
    }
  }
  return false;
}

bool solve() {
  sort(a, a + n, greater<int>());
  b[0] = a[0];
  for (int i = 1; i < n; i++) {
    b[i] = 0;
  }
  finished = 1;

  bool ans;
  if (n > 1) {
    ans = dfs(1);
  } else {
    ans = true;
  }

  return ans;
}

int main() {
  bool ans;
  while (1) {
    cin >> n;
    if (!n) {
      break;
    }
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    ans = solve();
    cout << (ans ? "YES" : "NO") << endl;
  }
  return 0;
}
Problem p00182 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int n;
bool ans;
vector<int> b;
void mainDFS(int x, vector<int> &c);

void subSolve(int x, int r, vector<int> &c, int X) {
  //?????????
  if (ans)
    return;

  //???????????§??\\?????????
  if (x == n) {
    if (r == 0) {
      mainDFS(X + 1, c);
    }
    return;
  }
  //????????????????????????
  if (c[x] == 0 && b[x] <= r) {
    c[x] = b[x];
    subSolve(x + 1, r - b[x], c, X);
    c[x] = 0;
  }
  //???????????????
  subSolve(x + 1, r, c, X);
}

void mainDFS(int x, vector<int> &c) {
  if (x == n) {
    ans = 1;
    return;
  }
  //?????????
  if (ans)
    return;
  if (c[x] == 0) {
    return;
  } else {
    // ?¬??????????????????????????????¶????????????
    if (x + 1 < n && c[x + 1] == 0) {
      c[x + 1] = b[x + 1];
      subSolve(x + 2, c[x] - b[x + 1], c, x);
      c[x + 1] = 0;
    } else {
      //???????????????
      mainDFS(x + 1, c);
      //??\\????????????
      subSolve(x + 1, c[x], c, x);
    }
  }
}
int main() {
  while (cin >> n && n) {
    b = vector<int>(n);
    for (int i = 0; i < n; i++)
      cin >> b[i];
    sort(b.rbegin(), b.rend());
    vector<int> s(n, 0);
    s[0] = b[0];
    ans = 0;
    mainDFS(0, s);
    if (ans)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <time.h>
#include <vector>
using namespace std;
clock_t start, endd;

vector<int> v;
int dn[100] = {};
int dfs(int x, int r) {
  endd = clock();
  if ((double)(endd - start) > 0.02 * CLOCKS_PER_SEC) {
    return 0;
  }
  if (!dn[x])
    return 0;
  if (r < 0)
    return 0;
  if (r == 0) {
    return dfs(x + 1, v[x + 1]);
  }
  if (x == v.size() - 1)
    return r != 0;
  if (r == v[x]) {
    if (dfs(x + 1, v[x + 1]))
      return 1;
  }

  long long can[2] = {}, tmp[2] = {};
  can[0] |= 1;

  // branch-cutting
  for (int i = x + 1; i < v.size(); i++) {
    if (!dn[i]) {
      if (v[x] < 64) {
        tmp[0] = can[0] << v[x];
        tmp[1] = (can[0] >> (64 - v[x])) | (can[1] << v[x]);
      } else {
        tmp[0] = 0;
        tmp[1] = can[0] << (v[x] - 64);
      }
      can[0] |= tmp[0];
      can[1] |= tmp[1];
    }
  }
  if (!(can[v[x] / 64] >> (v[x] % 64) & 1)) {
    return 0;
  }

  for (int i = x + 1; i < v.size(); i++) {
    if (dn[i] == 0) {
      dn[i] = 1;
      if (dfs(x, r - v[i]))
        return 1;
      dn[i] = 0;
    }
  }
  return 0;
}
int main() {
  int n;
  while (cin >> n && n) {
    v.resize(n);
    for (int i = 0; i < n; i++)
      dn[i] = 0;
    // for(int i = 0 ; i < n ; i++) v[i] = rand() % 100 + 1;

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

    sort(v.rbegin(), v.rend());

    dn[0] = 1;
    start = clock();
    cout << (dfs(0, v[0]) ? "YES" : "NO") << endl;
    dn[1] = 0;
  }
}