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

#include <algorithm>
#include <queue>
#include <set>
#include <stdio.h>
#include <vector>
using namespace std;
int n;
int ret = 0;
set<vector<int>> S;
inline int ABS(int a) { return max(a, -a); }
void dfs(vector<int> a, int b, int last) {
  if (b >= ret)
    return;
  // std::sort(a.begin(),a.end());
  // if(S.count(a))return;
  // S.insert(a);
  int sz = a.size();
  int mv = 0;
  for (int i = 0; i < sz; i++) {
    if (a[i] == n) {
      ret = b;
      return;
    }
    mv = max(mv, a[i]);
  }
  if (b == ret - 1)
    return;
  if ((mv << (ret - b - 1)) < n)
    return;
  for (int i = 0; i < sz; i++) {
    for (int j = 0; j < sz; j++) {
      if (a[i] + a[j] == n || ABS(a[i] - a[j]) == n) {
        ret = b + 1;
        return;
      }
    }
  }
  vector<int> vis;
  for (int i = sz - 1; i >= 0; i--)
    for (int j = i; j >= 0; j--) {
      if (a[i] != last && a[j] != last)
        continue;
      bool ok = true;
      for (int k = 0; k < sz; k++)
        if (a[i] + a[j] == a[k]) {
          ok = false;
          break;
        }
      for (int k = 0; k < vis.size(); k++)
        if (vis[k] == a[i] + a[j]) {
          ok = false;
          break;
        }
      if (ok) {
        a.push_back(a[i] + a[j]);
        vis.push_back(a[i] + a[j]);
        dfs(a, b + 1, a[i] + a[j]);
        a.pop_back();
      }
      ok = true;
      if (a[i] == a[j])
        continue;
      for (int k = 0; k < sz; k++)
        if (ABS(a[i] - a[j]) == a[k]) {
          ok = false;
          break;
        }
      for (int k = 0; k < vis.size(); k++)
        if (vis[k] == ABS(a[i] - a[j])) {
          ok = false;
          break;
        }
      if (ok) {
        a.push_back(ABS(a[i] - a[j]));
        vis.push_back(ABS(a[i] - a[j]));
        dfs(a, b + 1, ABS(a[i] - a[j]));
        a.pop_back();
      }
    }
}
int main() {
  int a;
  while (scanf("%d", &a), a) {
    S.clear();
    n = a;
    int c = a;
    ret = 0;
    while (c) {
      if (c % 2)
        ret++;
      ret++;
      c /= 2;
    }
    ret -= 2;
    ret = min(ret, 13);
    vector<int> st;
    st.push_back(1);
    dfs(st, 0, 1);
    printf("%d\\n", ret);
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <queue>
#include <set>
#include <stdio.h>
#include <vector>
using namespace std;
int n;
int ret = 0;
set<vector<int>> S;
inline int ABS(int a) { return max(a, -a); }
void dfs(vector<int> a, int b, int last) {
  if (b >= ret)
    return;
  std::sort(a.begin(), a.end());
  if (S.count(a))
    return;
  S.insert(a);
  int sz = a.size();
  int mv = 0;
  for (int i = 0; i < sz; i++) {
    if (a[i] == n) {
      ret = b;
      return;
    }
    mv = max(mv, a[i]);
  }
  if (b == ret - 1)
    return;
  if ((mv << (ret - b - 1)) < n)
    return;
  for (int i = 0; i < sz; i++) {
    for (int j = 0; j < sz; j++) {
      if (a[i] + a[j] == n || ABS(a[i] - a[j]) == n) {
        ret = b + 1;
        return;
      }
    }
  }
  vector<int> vis;
  for (int i = 0; i < sz; i++)
    for (int j = i; j < sz; j++) {
      if (a[i] != last && a[j] != last)
        continue;
      bool ok = true;
      for (int k = 0; k < sz; k++)
        if (a[i] + a[j] == a[k]) {
          ok = false;
          break;
        }
      for (int k = 0; k < vis.size(); k++)
        if (vis[k] == a[i] + a[j]) {
          ok = false;
          break;
        }
      if (ok) {
        a.push_back(a[i] + a[j]);
        vis.push_back(a[i] + a[j]);
        dfs(a, b + 1, a[i] + a[j]);
        a.pop_back();
      }
      ok = true;
      if (a[i] == a[j])
        continue;
      for (int k = 0; k < sz; k++)
        if (ABS(a[i] - a[j]) == a[k]) {
          ok = false;
          break;
        }
      for (int k = 0; k < vis.size(); k++)
        if (vis[k] == ABS(a[i] - a[j])) {
          ok = false;
          break;
        }
      if (ok) {
        a.push_back(ABS(a[i] - a[j]));
        vis.push_back(ABS(a[i] - a[j]));
        dfs(a, b + 1, ABS(a[i] - a[j]));
        a.pop_back();
      }
    }
}
int main() {
  int a;
  while (scanf("%d", &a), a) {
    S.clear();
    n = a;
    int c = a;
    ret = 0;
    while (c) {
      if (c % 2)
        ret++;
      ret++;
      c /= 2;
    }
    ret -= 2;
    vector<int> st;
    st.push_back(1);
    dfs(st, 0, 1);
    printf("%d\\n", ret);
  }
}