Problem p01151 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define pb(e) push_back(e)
#define foreach(c, i) for (iter(c) i = (c).begin(); i != c.end(); ++i)
#define mp(a, b) make_pair(a, b)

typedef long long ll;
typedef pair<ll, ll> P;
const int INF = 1000 * 1000 * 1000 + 7;
const double EPS = 1e-10;
const int maxS = 400;

inline ll encode(const vector<int> &cards) {
  ll res = 0;
  rep(i, 13) { res = res << 3 | cards[i]; }
  return res;
}

inline vector<int> decode(ll code) {
  vector<int> cards;
  rep(i, 13) { cards.pb((code >> (12 - i) * 3) & 7); }
  return cards;
}

set<ll> memo[maxS];
vector<int> res;

bool dfs(int sum, ll code) {
  if (code == 0)
    return true;
  if (memo[sum].count(code))
    return false;

  memo[sum].insert(code);

  vector<int> v = decode(code);
  rep(i, 13) {
    int n = i + 1;
    if (v[i] == 0 || sum % n > 0)
      continue;

    v[i]--;
    if (dfs(sum - n, encode(v))) {
      // cout << n << endl;
      res.push_back(n);
      return true;
    }

    v[i]++;
  }
  return false;
}

int main() {
  int N, c;
  while (cin >> N && N) {
    //    cout << N << endl;
    int sum = 0;
    res = vector<int>();
    rep(i, maxS) memo[i].clear();

    vector<int> v(13, 0);
    rep(i, N) {
      cin >> c;
      sum += c;
      v[--c]++;
    }

    //    rep(i, 13) cout << v[i] << endl;
    if (dfs(sum, encode(v))) {
      assert(N == (int)res.size());
      rep(i, N) cout << res[i] << (i == N - 1 ? '\\n' : ' ');
    } else {
      cout << "No" << endl;
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define pb(e) push_back(e)
#define foreach(c, i) for (iter(c) i = (c).begin(); i != c.end(); ++i)
#define mp(a, b) make_pair(a, b)

typedef long long ll;
typedef pair<ll, ll> P;
const int INF = 1000 * 1000 * 1000 + 7;
const double EPS = 1e-10;
const int maxS = 400;

inline ll encode(const vector<int> &cards) {
  ll res = 0;
  rep(i, 13) { res = res << 3 | cards[i]; }
  return res;
}

inline vector<int> decode(ll code) {
  vector<int> cards;
  rep(i, 13) { cards.pb((code >> (12 - i) * 3) & 7); }
  return cards;
}

set<ll> memo[maxS];
vector<int> res;

bool dfs(int sum, ll code) {
  if (code == 0)
    return true;
  if (memo[sum].count(code))
    return false;

  memo[sum].insert(code);

  vector<int> v = decode(code);
  rep(i, 13) {
    int n = i + 1;
    if (v[i] == 0 || sum % n > 0)
      continue;

    v[i]--;
    if (dfs(sum + n, encode(v))) {
      // cout << n << endl;
      res.push_back(n);
      return true;
    }

    v[i]++;
  }
  return false;
}

int main() {
  int N, c;
  while (cin >> N && N) {
    res = vector<int>();
    rep(i, maxS) memo[i].clear();

    vector<int> v(13, 0);
    rep(i, N) {
      cin >> c;
      v[--c]++;
    }

    //    rep(i, 13) cout << v[i] << endl;
    if (dfs(0, encode(v))) {
      assert(N == (int)res.size());
      reverse(res.begin(), res.end());
      rep(i, N) cout << res[i] << (i == N - 1 ? '\\n' : ' ');
    } else {
      cout << "No" << endl;
    }
  }
  return 0;
}