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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <set>

#define MAX 60

using namespace std;

int N, ans[MAX], remain[14];
bool found;

void rec(int sum, int cnt) {

  if (found)
    return;

  if (cnt >= N) {
    for (int i = N - 1; i >= 0; i--) {
      if (i != N - 1)
        cout << " ";
      cout << ans[i];
    }
    cout << endl;

    found = true;

    return;
  }

  for (int i = 1; i <= 13; i++) {
    if (remain[i] <= 0)
      continue;
    if ((sum - i) % i == 0) {

      remain[i]--;
      ans[cnt] = i;
      rec(sum - i, cnt + 1);
      if (found)
        return;
      remain[i]++;
    }
  }
}

int main() {

  while (cin >> N, N) {

    for (int i = 0; i < 14; i++)
      remain[i] = 0;

    int c[N + 1], sum = 0;
    for (int i = 0; i < N; i++) {
      cin >> c[i];
      sum += c[i];
      remain[c[i]]++;
    }

    sort(c, c + N);

    found = false;
    rec(sum, 0);

    if (!found) {
      cout << "No" << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define F first
#define S second
#define INF 1 << 30

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int res[64], N, c[64], used[64], endf;

void dfs(int sum, stack<int> st) {
  if (st.size() == N) {
    endf = 1;
    int f = 1;
    while (!st.empty()) {
      if (!f)
        printf(" ");
      f = 0;
      printf("%d", st.top());
      st.pop();
    }
    puts("");
    return;
  }

  rep(i, 14) {
    if (!c[i])
      continue;
    if ((sum - i) % i != 0)
      continue;
    st.push(i);
    c[i]--;
    dfs(sum - i, st);
    if (endf)
      return;
    c[i]++;
    st.pop();
  }
}

int main() {
  while (scanf("%d", &N) && N) {
    int in, sum = 0;
    endf = 0;
    memset(c, 0, sizeof(c));
    rep(i, N) {
      scanf("%d", &in);
      sum += sum;
      c[in]++;
    }

    rep(i, 14) {
      //      printf("%d\\n", i);
      if (c[i] == 0)
        continue;
      if ((sum - i) % i != 0)
        continue;
      c[i]--;
      stack<int> st;
      st.push(i);
      dfs(sum - i, st);
      if (endf)
        break;
      c[i]++;
    }

    if (!endf)
      puts("NO");
  }
  return 0;
}
Problem p01151 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <set>

#define MAX 60

using namespace std;

int N, ans[MAX], remain[14];
bool found;

void rec(int sum, int cnt) {

  if (found)
    return;

  if (cnt >= N) {
    for (int i = N - 1; i >= 0; i--) {
      if (i != N - 1)
        cout << " ";
      cout << ans[i];
    }
    cout << endl;

    found = true;

    return;
  }

  for (int i = 1; i <= 13; i++) {
    if (remain[i] <= 0)
      continue;
    if ((sum - i) % i == 0) {

      remain[i]--;
      ans[cnt] = i;
      rec(sum - i, cnt + 1);
      if (found)
        return;
      remain[i]++;
    }
  }
}

int main() {

  while (cin >> N, N) {

    for (int i = 0; i < 14; i++)
      remain[i] = 0;

    int c[N + 1], sum = 0;
    for (int i = 0; i < N; i++) {
      cin >> c[i];
      sum += c[i];
      remain[c[i]]++;
    }

    sort(c, c + N);

    found = false;
    rec(sum, 0);

    if (!found) {
      cout << "No" << endl;
    }
  }
  return 0;
}

/*
void rec(int sum,int limit,int *c,int cnt){

  if(found)return;

  if(limit <= 0){

    for(int i=N-1;i>=0;i--){
      if(i != N-1)cout << ' ';
      cout << ans[i];
    }
    cout << endl;

    found = true;

    return;
  }

  for(int i=0;i<limit;i++){
    if( (sum-c[i]) % c[i] == 0){
      ans[cnt] = c[i];

      int sub = c[i];
      swap(c[i],c[limit-1]);
      rec(sum-sub,limit-1,c,cnt+1);
      if(found)return;
      swap(c[i],c[limit-1]);

    }
  }
}
*/

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <set>

#define MAX 60

using namespace std;

typedef unsigned long long ull;

set<ull> used;
int N, ans[MAX], Index[MAX];
bool found;

void rec(int sum, int limit, int *c, int cnt, ull state) {

  if (found)
    return;
  if (used.find(state) != used.end())
    return;
  used.insert(state);

  if (limit <= 0) {

    for (int i = N - 1; i >= 0; i--) {
      if (i != N - 1)
        cout << ' ';
      cout << ans[i];
    }
    cout << endl;

    found = true;

    return;
  }

  for (int i = 0; i < limit; i++) {
    if ((sum - c[i]) % c[i] == 0) {
      ans[cnt] = c[i];

      int sub = c[i];
      int index = Index[i];
      swap(c[i], c[limit - 1]);
      swap(Index[i], Index[limit - 1]);
      rec(sum - sub, limit - 1, c, cnt + 1, state | (1 << index));
      if (found)
        return;
      swap(c[i], c[limit - 1]);
      swap(Index[i], Index[limit - 1]);
    }
  }
}

int main() {

  while (cin >> N, N) {

    int c[N + 1], sum = 0;
    for (int i = 0; i < N; i++) {
      cin >> c[i];
      sum += c[i];
      Index[i] = i;
    }

    sort(c, c + N);

    used.clear();
    found = false;
    rec(sum, N, c, 0, 0);

    if (!found) {
      cout << "No" << endl;
    }
  }
  return 0;
}
Problem p01151 (C++) — buggy submission, verdict: Memory Limit Exceeded

// 36
#include <iostream>
#include <vector>

using namespace std;

vector<int> v;

bool dfs(int n, int *c) {
  if (n == 0) {
    for (int i = v.size() - 1; i >= 0; i--) {
      cout << v[i] << (i ? ' ' : '\\n');
    }
    return true;
  } else {
    for (int i = 1; i <= 13; i++) {
      if (c[i] && n % i == 0) {
        c[i]--;
        v.push_back(i);
        if (dfs(n - i, c))
          return true;
        v.pop_back();
        c[i]++;
      }
    }
    return false;
  }
}

int main() {
  for (int n; cin >> n, n;) {
    int c[14] = {};
    int s = 0;
    for (int i = 0; i < n; i++) {
      int cc;
      cin >> cc;
      s += cc;
      c[cc]++;
    }
    v.clear();
    if (!dfs(s, c)) {
      cout << "No" << endl;
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

// 06
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

int n;
int c[14];
vector<int> v;
set<long long> f;

long long en(long long s) {
  long long e = 0;
  for (int i = 1; i <= 13; i++) {
    e = e * 5 + c[i];
  }
  e |= s << 32;
  return e;
}

bool dfs(int s) {
  if (f.count(en(s))) {
    return false;
  } else {
    bool n = true;
    for (int i = 1; i <= 13; i++) {
      n &= !c[i];
      if (c[i] && s % i == 0) {
        c[i]--;
        if (dfs(s + i)) {
          v.push_back(i);
          return true;
        }
        c[i]++;
      }
    }
    if (!n) {
      f.insert(en(s));
    }
    return n;
  }
}

int main() {
  while (cin >> n, n) {
    fill(c, c + 13, 0);
    v.clear();
    for (int i = 0; i < n; i++) {
      int r;
      cin >> r;
      c[r]++;
    }
    if (!dfs(0)) {
      cout << "No" << endl;
    } else {
      for (int i = v.size() - 1; i >= 0; i--) {
        cout << v[i] << (i ? ' ' : '\\n');
      }
    }
  }
  return 0;
}
Problem p01151 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
unordered_map<int, bool> mp;
int n, a[14], ans[52], c;

int cal() {
  int res = 0;
  for (int i = 1; i < 13; i++)
    res = res * 5 + a[i];
  return res;
}

bool dfs(int s, int d) {
  if (d == n)
    return 1;
  int y = cal();
  if (mp.count(y))
    return mp[y];
  for (int i = 13; i > 0; i--) {
    if (a[i] && (s - i) % i == 0) {
      ans[d] = i;
      a[i]--;
      if (dfs(s - i, d + 1))
        return mp[y] = 1;
      a[i]++;
    }
  }
  return 0;
}

int main() {
  while (cin >> n, n) {
    memset(a, 0, sizeof(a));
    mp.clear();
    c = 0;
    for (int i = 0, b; i < n; i++)
      scanf("%d", &b), a[b]++, c += b;
    if (dfs(c, 0)) {
      for (int i = n - 1; i >= 0; i--) {
        if (i != n - 1)
          printf(" ");
        printf("%d", ans[i]);
      }
    } else
      printf("No");
    printf("\\n");
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
unordered_map<int, bool> mp;
int n, a[14], ans[52], c;

int cal() {
  int res = 0;
  for (int i = 1; i < 13; i++)
    res = res * 5 + a[i];
  return res;
}

bool dfs(int s, int d) {
  if (d == n)
    return 1;
  // if(c==1e7)return 0;
  c++;
  int y = cal();
  if (mp.count(y))
    return mp[y];
  for (int i = 13; i >= 1; i--) {
    if (a[i] && s % i == 0) {
      ans[d] = i;
      a[i]--;
      if (dfs(s + i, d + 1))
        return mp[y] = 1;
      a[i]++;
    }
  }
  return 0;
}

int main() {
  while (cin >> n, n) {
    memset(a, 0, sizeof(a));
    mp.clear();
    c = 0;
    for (int i = 0, b; i < n; i++)
      scanf("%d", &b), a[b]++;
    if (dfs(0, 0)) {
      for (int i = 0; i < n; i++) {
        if (i)
          printf(" ");
        printf("%d", ans[i]);
      }
    } else
      printf("No");
    ;
    printf("\\n");
  }
  return 0;
}
Problem p01151 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

vector<int> use;

bool solve(long long rest, int sum) {
  if (rest == 0)
    return true;

  for (int i = 0; i < 13; ++i) {
    if ((sum - (i + 1)) % (i + 1) == 0 && (rest & (7LL << (3 * i)))) {
      use.push_back(i + 1);
      if (solve(rest - (1LL << (3 * i)), sum - (i + 1)))
        return true;
      use.pop_back();
    }
  }
  return false;
}

int main() {
  for (;;) {
    int n;
    cin >> n;
    if (n == 0)
      return 0;

    long long rest = 0;
    int sum = 0;
    for (int i = 0; i < n; ++i) {
      int a;
      cin >> a;
      rest += (1LL << (3 * (a - 1)));
      sum += a;
    }

    use.clear();
    if (solve(rest, sum)) {
      for (int i = n - 1; i > 0; --i)
        cout << use[i] << ' ';
      cout << use[0] << endl;
    } else {
      cout << "No" << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

vector<int> use;

bool solve(long long rest, int sum) {
  if (rest == 0)
    return true;

  for (int i = 12; i >= 0; --i) {
    if (sum % (i + 1) == 0 && (rest & (7LL << (3 * i)))) {
      use.push_back(i + 1);
      if (solve(rest - (1LL << (3 * i)), sum + i + 1))
        return true;
      use.pop_back();
    }
  }
  return false;
}

int main() {
  for (;;) {
    int n;
    cin >> n;
    if (n == 0)
      return 0;

    long long rest = 0;
    for (int i = 0; i < n; ++i) {
      int a;
      cin >> a;
      rest += (1LL << (3 * (a - 1)));
    }

    use.clear();
    if (solve(rest, 0)) {
      cout << use[0];
      for (int i = 1; i < n; ++i)
        cout << ' ' << use[i];
      cout << endl;
    } else {
      cout << "No" << endl;
    }
  }
}
Problem p01151 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int n;
vector<int> dfs(vector<int> &v, vector<int> &ans, int idx, int sum) {
  if (idx == n)
    return ans;
  REP(i, v.size()) {
    if (v[i] > 0 && (sum - (i + 1)) % (i + 1) == 0) {
      v[i]--;
      ans.push_back(i + 1);
      vector<int> res = dfs(v, ans, idx + 1, sum - (i + 1));
      if (!res.empty())
        return res;
      ans.pop_back();
      v[i]++;
    }
  }
  return vector<int>();
}

int main() {
  while (cin >> n, n) {
    vector<int> v(13, 0);
    int sum = 0;
    REP(i, n) {
      int t;
      cin >> t;
      sum += t;
      v[t - 1]++;
    }
    vector<int> ans;
    vector<int> res = dfs(v, ans, 0, sum);
    reverse(res.begin(), res.end());
    if (!res.empty())
      debug(res.begin(), res.end());
    else
      cout << "No" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int n;
vector<int> dfs(vector<int> &v, vector<int> &ans, int idx, int sum) {
  if (idx == n)
    return ans;
  REP(i, v.size()) {
    if (v[i] > 0 && sum % (i + 1) == 0) {
      v[i]--;
      ans.push_back(i + 1);
      vector<int> res = dfs(v, ans, idx + 1, sum + i + 1);
      if (!res.empty())
        return res;
      ans.pop_back();
      v[i]++;
    }
  }
  return vector<int>();
}

int main() {
  while (cin >> n, n) {
    vector<int> v(13, 0);
    REP(i, n) {
      int t;
      cin >> t;
      v[t - 1]++;
    }
    vector<int> ans;
    vector<int> res = dfs(v, ans, 0, 0);
    if (!res.empty())
      debug(res.begin(), res.end());
    else
      cout << "NO" << endl;
  }
  return 0;
}
Problem p01151 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

int input[52], n, summ;
int used[52];
int cnt[14];

bool dfs(vector<int> &v, int sum) {
  if (v.size() == n)
    return true;
  rep(i, 14) {
    if (cnt[i] == 0)
      continue;
    if ((sum - i) % i == 0) {
      cnt[i]--;
      v.push_back(i);
      if (dfs(v, sum - i))
        return true;
      v.pop_back();
      cnt[i]++;
    }
  }
  return false;
}

int main() {
  while (cin >> n, n) {
    summ = 0;
    memset(cnt, 0, sizeof(cnt));
    memset(used, -1, sizeof(used));
    rep(i, n) {
      cin >> input[i];
      summ += input[i];
      cnt[input[i]]++;
    }
    rep(i, 52) used[i] = -1;

    sort(input, input + n);
    reverse(input, input + n);
    vector<int> ans;
    if (dfs(ans, summ)) {
      reverse(ans.begin(), ans.end());
      rep(i, n - 1) cout << ans[i] << " ";
      cout << ans[n - 1] << endl;
    } else {
      cout << "No" << endl;
    }
  }
}

Error: TLE

Fixed submission:

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

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

int input[52], n, summ;
int used[52];

bool dfs(int cc, int sum) {
  if (cc == n)
    return true;
  rep(i, n) {
    if (used[i] != -1)
      continue;
    if ((summ - input[i] - sum) % input[i] == 0) {
      used[i] = cc;
      if (dfs(cc + 1, sum + input[i]))
        return true;
      used[i] = -1;
    }
  }
  return false;
}

int main() {
  while (cin >> n, n) {
    summ = 0;
    rep(i, n) {
      cin >> input[i];
      summ += input[i];
    }
    rep(i, 52) used[i] = -1;

    sort(input, input + n);
    reverse(input, input + n);
    if (dfs(0, 0)) {
      vector<int> ans(n, 0);
      rep(i, n) { ans[used[i]] = input[i]; }
      reverse(ans.begin(), ans.end());
      rep(i, n - 1) cout << ans[i] << " ";
      cout << ans[n - 1] << endl;
    } else {
      cout << "No" << endl;
    }
  }
}
Problem p01151 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

using namespace std;

bool dfs(int a[14], int r[54], int n, int s, int p) {
  if (p == n && s == 0) {
    cout << r[n - 1];
    for (int i = n - 2; i >= 0; i--) {
      cout << " " << r[i];
    }
    cout << endl;
    return true;
  }
  if (p == n || s <= 0) {
    return false;
  }

  for (int i = 1; i <= 13; i++) {
    if (a[i] && (s - i) % i == 0) {
      a[i]--;
      r[p] = i;
      if (dfs(a, r, n, s - i, p + 1)) {
        return true;
      }
      a[i]++;
    }
  }

  return false;
}

int main() {
  int n, a[14], s, t, r[54];

  while (cin >> n && n) {
    fill(a, a + 14, 0);
    s = 0;
    for (int i = 0; i < n; i++) {
      cin >> t;
      a[t]++;
      s += t;
    }

    if (!dfs(a, r, n, s, 0)) {
      cout << "No" << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

bool dfs(int a[14], vector<int> vec, set<int> stt, int n, int s, int p) {
  if (p == n && s == 0) {
    cout << vec[vec.size() - 1];
    for (int i = vec.size() - 2; i >= 0; i--) {
      cout << " " << vec[i];
    }
    cout << endl;

    return true;
  }
  if (p == n || s <= 0) {
    return false;
  }

  for (set<int>::iterator it = stt.begin(); it != stt.end(); it++) {
    int t = *it;
    if ((s - t) % t == 0) {
      a[t]--;
      vector<int> v2 = vec;
      v2.push_back(t);
      set<int> st2 = stt;
      if (a[t] == 0) {
        st2.erase(t);
      }
      if (dfs(a, v2, st2, n, s - t, p + 1)) {
        return true;
      }
      a[t]++;
    }
  }

  return false;
}

int main() {
  int n, a[14], s, t;

  while (cin >> n && n) {
    s = 0;
    set<int> stt;
    vector<int> vec;
    fill(a, a + 14, 0);
    for (int i = 0; i < n; i++) {
      cin >> t;
      s += t;
      a[t]++;
      stt.insert(t);
    }

    if (!dfs(a, vec, stt, n, s, 0)) {
      cout << "No" << endl;
    }
  }

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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

int a[13];
int n;
vector<int> v;

bool dfs(int sum) {
  if (sum == 0)
    return 1;
  // cout << sum << endl;
  REP(i, 13) {
    if (a[i] == 0)
      continue;
    if ((sum - (i + 1)) % (i + 1) == 0) {
      v.push_back(i + 1);
      a[i]--;
      if (dfs(sum - (i + 1)))
        return 1;
      a[i]++;
      v.pop_back();
    }
  }
  return 0;
}

int main() {
  while (cin >> n, n) {
    memset(a, 0, sizeof(a));
    int sum = 0;
    REP(i, n) {
      int b;
      cin >> b;
      a[b - 1]++;
      sum += b;
    }
    v.clear();
    if (dfs(sum)) {
      reverse(ALL(v));
      REP(i, v.size()) {
        if (i)
          cout << " ";
        cout << v[i];
      }
      cout << endl;
    } else {
      cout << "No" << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

int a[52];
int n;
vector<int> v;
typedef long long ll;
bool dfs(ll S, int sum) {
  // cout << bitset<6>(S) << " " << sum << endl;
  if (S == (1 << n) - 1)
    return 1;
  int mae = -1;
  REP(i, n) {
    if (S >> i & 1)
      continue;
    if (sum % a[i] == 0) {
      if (a[i] == mae)
        continue;
      mae = a[i];
      v.push_back(a[i]);
      if (dfs(S | 1 << i, sum + a[i]))
        return 1;
      v.pop_back();
    }
  }
  return 0;
}

int main() {
  while (cin >> n, n) {
    REP(i, n)
    cin >> a[i];
    sort(a, a + n);
    if (dfs(0, 0)) {
      REP(i, v.size()) {
        if (i)
          cout << " ";
        cout << v[i];
      }
      cout << endl;
    } else {
      cout << "No" << endl;
    }
  }
}
Problem p01151 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

inline ll encode(const vector<int> &a) {
  ll res = 0;
  for (int i = 13; i >= 1; i--)
    res = res * 5 + a[i];
  return res;
}

set<int> memo;
vector<int> ans;

bool rec(ll sum, vector<int> &rem) {
  int a = encode(rem);
  if (a == 0)
    return true;
  if (memo.count(a))
    return false;
  memo.insert(a);

  for (int i = 13; i >= 1; i--) {
    if (rem[i] && sum % i == 0) {
      rem[i]--;
      ans.push_back(i);
      if (rec(sum - i, rem))
        return true;
      ans.pop_back();
      rem[i]++;
    }
  }
  return false;
}

int main() {
  int n;
  while (cin >> n, n) {
    vector<int> num(14, 0);
    int sum = 0;
    for (int i = 0; i < n; i++) {
      int tmp;
      cin >> tmp;
      num[tmp]++;
      sum += tmp;
    }

    memo.clear();
    ans.clear();
    rec(sum, num);
    if ((int)ans.size() == n) {
      reverse(ans.begin(), ans.end());
      for (int i = 0; i < n; i++) {
        cout << ans[i] << (i == n - 1 ? "\\n" : " ");
      }
    } else
      cout << "No" << endl;
  }
}

Error: MLE

Fixed submission:

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

inline ll encode(const vector<int> &a) {
  ll res = 0;
  for (int i = 13; i >= 1; i--)
    res = res * 5 + a[i];
  return res;
}

set<int> memo;
vector<int> ans;

bool rec(ll sum, vector<int> &rem) {
  int a = encode(rem);
  if (a == 0)
    return true;
  if (memo.count(a))
    return false;
  memo.insert(a);

  for (int i = 13; i >= 1; i--) {
    if (rem[i] && sum % i == 0) {
      rem[i]--;
      ans.push_back(i);
      if (rec(sum + i, rem))
        return true;
      ans.pop_back();
      rem[i]++;
    }
  }
  return false;
}

int main() {
  int n;
  while (cin >> n, n) {
    vector<int> num(14, 0);
    for (int i = 0; i < n; i++) {
      int tmp;
      cin >> tmp;
      num[tmp]++;
    }

    memo.clear();
    ans.clear();
    rec(0, num);
    if ((int)ans.size() == n) {
      for (int i = 0; i < n; i++) {
        cout << ans[i] << (i == n - 1 ? "\\n" : " ");
      }
    } else
      cout << "No" << endl;
  }
}