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

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

int N;
bool FLG;

void recursive(int cards[14], int log[], int used_num, int sum) {

  if (FLG)
    return;

  if (used_num == N) {

    FLG = true;

    reverse(log, log + N);

    printf("%d", log[0]);
    for (int i = 1; i < N; i++) {
      printf(" %d", log[i]);
    }
    printf("\\n");

    return;
  }

  for (int i = 1; i <= 13; i++) {
    if (cards[i] > 0 && (used_num == N - 1 || (sum - i) % i == 0)) {
      int next_cards[14], next_log[N];
      for (int k = 1; k <= 13; k++) {
        next_cards[k] = cards[k];
      }
      next_cards[i]--;
      for (int k = 0; k < used_num; k++) {
        next_log[k] = log[k];
      }
      next_log[used_num] = i;

      recursive(next_cards, next_log, used_num + 1, sum - i);
    }
  }
}

void func() {

  FLG = false;

  int first[14];
  for (int i = 1; i <= 13; i++) {
    first[i] = 0;
  }

  int tmp, sum = 0;
  for (int i = 0; i < N; i++) {
    scanf("%d", &tmp);
    first[tmp]++;
    sum += tmp;
  }

  int log[N];

  recursive(first, log, 0, sum);

  if (!FLG) {
    printf("No\\n");
  }
}

int main() {

  while (true) {
    scanf("%d", &N);
    if (N == 0)
      break;

    func();
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Info {
  vector<int> V, History;
  int sum;
};

int N;

void func() {

  Info first;

  int tmp, sum = 0;
  for (int i = 0; i < N; i++) {
    scanf("%d", &tmp);
    first.V.push_back(tmp);
    sum += tmp;
  }

  queue<Info> Q;

  for (int i = 0; i < first.V.size(); i++) {

    tmp = first.V[i];

    if ((sum - tmp) % tmp != 0)
      continue;

    Info next_info;
    next_info.sum = sum - tmp;
    next_info.History.push_back(tmp);

    int count = 0;

    for (int k = 0; k < first.V.size(); k++) {
      if (count == 0 && first.V[k] == tmp) {
        count++;
        continue;
      } else {
        next_info.V.push_back(first.V[k]);
      }
    }

    Q.push(next_info);
  }

  while (!Q.empty()) {

    if (Q.front().V.size() == 0) {

      reverse(Q.front().History.begin(), Q.front().History.end());

      printf("%d", Q.front().History[0]);
      for (int i = 1; i < Q.front().History.size(); i++) {
        printf(" %d", Q.front().History[i]);
      }
      printf("\\n");

      return;
    }

    for (int i = 0; i < Q.front().V.size(); i++) {

      tmp = Q.front().V[i];
      if (Q.front().V.size() > 1 && (Q.front().sum - tmp) % tmp != 0)
        continue;

      Info next_info;
      next_info.sum = Q.front().sum - tmp;
      for (int k = 0; k < Q.front().History.size(); k++) {
        next_info.History.push_back(Q.front().History[k]);
      }
      next_info.History.push_back(tmp);

      int count = 0;

      for (int k = 0; k < Q.front().V.size(); k++) {
        if (count == 0 && Q.front().V[k] == tmp) {
          count++;
          continue;
        } else {
          next_info.V.push_back(Q.front().V[k]);
        }
      }
      Q.push(next_info);
    }
    Q.pop();
  }
  printf("No\\n");
}

int main() {

  while (true) {
    scanf("%d", &N);
    if (N == 0)
      break;

    func();
  }

  return 0;
}