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

#define _CRT_SECURE_NO_WARNINGS 1
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define MP make_pair
#define INF ((int)(1 << 25))
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, n) REP(i, n)
#define FORI(i, k, n) for (int i = k; i < (int)n; i++)
#define TORAD 2.0 * M_PI / 360.0
#define INT(x)                                                                 \\
  int x;                                                                       \\
  cin >> x;
#define STRING(x)                                                              \\
  string x;                                                                    \\
  cin >> x;
#define STR(x) STRING(x);
#define DOUBLE(x)                                                              \\
  double x;                                                                    \\
  cin >> x;
#define DBL(x) DOUBLE(x);
#define ALL(x) begin(x), end(x)
#define RALL(x) (x).rbegin(), (x).rend()
#define DEBUG(x) cout << #x << ":" << x << endl
#define INIT(a) memset(a, -1, sizeof a)
#define CLR(a) memset(a, 0, sizeof a)
#define EPS 1e-12
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
typedef long long lint;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<int> vi;
typedef vector<lint> vl;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
// const int M = 1000000007;
template <class T> void debug(T a) {
  for (auto i : a)
    cout << i << endl;
}

int w;
vi a{1};
bool check(vi &ans) {
  int v = 0;
  FOR(i, ans.size()) { v += a[i] * ans[i]; }
  return w == v;
}

void next(vi &ans) {
  ans[0]++;
  FOR(i, ans.size()) {
    if (ans[i] == 2) {
      ans[i] = -1;
      if (i == ans.size() - 1) {
        ans.push_back(-1);
      } else {
        ans[i + 1]++;
      }
    }
  }
}

int main() {
  FORI(i, 1, 15) a.emplace_back(a.back() * 3);

  cin >> w;
  vi ans = {-1};
  while (!check(ans)) {
    next(ans);
  }
  reverse(ALL(ans));
  string c = "-0+";
  FOR(i, ans.size()) { cout << c[ans[i] + 1]; }
  cout << endl;
}

Error: MLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS 1
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define MP make_pair
#define INF ((int)(1 << 25))
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, n) REP(i, n)
#define FORI(i, k, n) for (int i = k; i < (int)n; i++)
#define TORAD 2.0 * M_PI / 360.0
#define INT(x)                                                                 \\
  int x;                                                                       \\
  cin >> x;
#define STRING(x)                                                              \\
  string x;                                                                    \\
  cin >> x;
#define STR(x) STRING(x);
#define DOUBLE(x)                                                              \\
  double x;                                                                    \\
  cin >> x;
#define DBL(x) DOUBLE(x);
#define ALL(x) begin(x), end(x)
#define RALL(x) (x).rbegin(), (x).rend()
#define DEBUG(x) cout << #x << ":" << x << endl
#define INIT(a) memset(a, -1, sizeof a)
#define CLR(a) memset(a, 0, sizeof a)
#define EPS 1e-12
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
typedef long long lint;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<int> vi;
typedef vector<lint> vl;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
// const int M = 1000000007;
template <class T> void debug(T a) {
  for (auto i : a)
    cout << i << endl;
}

vi d{1};
struct P {
  string str;
  int n = 0, v = 0;
};

int main() {
  FORI(i, 1, 15) d.emplace_back(d.back() * 3);
  int w;
  cin >> w;

  queue<P> que;
  que.push(P());

  while (!que.empty()) {
    P now = que.front();
    que.pop();
    if (now.v == w) {
      string ans = now.str;
      reverse(ALL(ans));
      cout << ans << endl;
      break;
    }
    P next = now;
    next.str += "+";
    next.v += d[next.n];
    next.n++;
    que.push(next);

    next = now;
    next.str += "-";
    next.v -= d[next.n];
    next.n++;
    que.push(next);

    next = now;
    next.str += "0";
    next.n++;
    que.push(next);
  }
}