Problem p03089 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define repf(i, f, l) for (int i = f; i < (int)l; ++i)
#define repit(it, t)                                                           \
  for (__typeof((t).begin()) it = (t).begin(); it != (t).end(); it++)
#define endl "\n"
#define pb emplace_back
#define lb lower_bound
#define int long long
#define ul unsigned long
#define ull unsigned long long
#define ll long long
#define INF 1000000007
#define MOD 1000000007
#define fs first
#define sd second
#define DBG0(x)                                                                \
  { cout << #x << ": " << x << "\t"; }
#define DBG(x)                                                                 \
  {                                                                            \
    DBG0(x);                                                                   \
    cout << endl;                                                              \
  }
#define DBG2(x, y)                                                             \
  {                                                                            \
    DBG0(x);                                                                   \
    DBG(y);                                                                    \
  }
#define DBG3(x, y, z)                                                          \
  {                                                                            \
    DBG0(x);                                                                   \
    DBG2(y, z);                                                                \
  }
#define DBG4(w, x, y, z)                                                       \
  {                                                                            \
    DBG0(w);                                                                   \
    DBG3(x, y, z);                                                             \
  }

typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<ul> vul;
typedef vector<ull> vull;
typedef vector<bool> vbl;
typedef pair<int, int> pii;

void print(vint as) {
  rep(i, as.size()) cout << as[i] << " ";
  cout << endl;
}

vint solve(vint cur) {
  if (cur.size() == 1 && cur[0] == 1) {
    vint ans;
    ans.pb(1);
    return ans;
  }
  rep(i, cur.size()) {
    if (cur[i] != i + 1) {
      continue;
    }
    vint ccur;
    rep(j, cur.size()) {
      if (j != i) {
        ccur.pb(cur[j]);
      }
    }
    vint ret = solve(ccur);
    if (ret.size() == ccur.size()) {
      ret.pb(i + 1);
      return ret;
    }
  }
  return vint();
}

signed main(void) {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vint as = vint(n);
  rep(i, n) cin >> as[i];
  vint ans = solve(as);
  if (ans.size() != as.size()) {
    cout << -1 << endl;
    return 0;
  }
  rep(i, ans.size()) cout << ans[i] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define repf(i, f, l) for (int i = f; i < (int)l; ++i)
#define repit(it, t)                                                           \
  for (__typeof((t).begin()) it = (t).begin(); it != (t).end(); it++)
#define endl "\n"
#define pb emplace_back
#define lb lower_bound
#define int long long
#define ul unsigned long
#define ull unsigned long long
#define ll long long
#define INF 1000000007
#define MOD 1000000007
#define fs first
#define sd second
#define DBG0(x)                                                                \
  { cout << #x << ": " << x << "\t"; }
#define DBG(x)                                                                 \
  {                                                                            \
    DBG0(x);                                                                   \
    cout << endl;                                                              \
  }
#define DBG2(x, y)                                                             \
  {                                                                            \
    DBG0(x);                                                                   \
    DBG(y);                                                                    \
  }
#define DBG3(x, y, z)                                                          \
  {                                                                            \
    DBG0(x);                                                                   \
    DBG2(y, z);                                                                \
  }
#define DBG4(w, x, y, z)                                                       \
  {                                                                            \
    DBG0(w);                                                                   \
    DBG3(x, y, z);                                                             \
  }

typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<ul> vul;
typedef vector<ull> vull;
typedef vector<bool> vbl;
typedef pair<int, int> pii;

void print(vint as) {
  rep(i, as.size()) cout << as[i] << " ";
  cout << endl;
}

vint solve(vint cur) {
  rep(i, cur.size()) {
    if (cur[i] > i + 1)
      return vint();
  }
  if (cur.size() == 1 && cur[0] == 1) {
    vint ans;
    ans.pb(1);
    return ans;
  }
  rep(i, cur.size()) {
    if (cur[i] != i + 1) {
      continue;
    }
    vint ccur;
    rep(j, cur.size()) {
      if (j != i) {
        ccur.pb(cur[j]);
      }
    }
    vint ret = solve(ccur);
    if (ret.size() == ccur.size()) {
      ret.pb(i + 1);
      return ret;
    }
  }
  return vint();
}

signed main(void) {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vint as = vint(n);
  rep(i, n) cin >> as[i];
  vint ans = solve(as);
  if (ans.size() != as.size()) {
    cout << -1 << endl;
    return 0;
  }
  rep(i, ans.size()) cout << ans[i] << endl;
  return 0;
}