Problem p03089 (C++)

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

using namespace std;

#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define REV(i, n) for (ll(i) = (n)-1; (i) >= 0; --i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \
  {                                                                            \
    REP(WW, n) cerr << v[WW] << ' ';                                           \
    cerr << endl << endl;                                                      \
  }
#define SHOW2d(v, WW, HH)                                                      \
  {                                                                            \
    REP(W_, WW) {                                                              \
      REP(H_, HH) cerr << v[W_][H_] << ' ';                                    \
      cerr << endl;                                                            \
    }                                                                          \
    cerr << endl;                                                              \
  }
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 998244353
#define fastcin()                                                              \
  cin.tie(0);                                                                  \
  ios::sync_with_stdio(false)

typedef long long ll;
typedef pair<ll, ll> P;

int n;
int mp[111];

void out() {
  REV(i, n) cout << mp[i] << endl;
  exit(0);
}

void dfs(vector<int> &v, int dep) {
  if (v.size() == 0) {
    out();
  }
  REP(i, v.size()) {
    if (v[i] == i + 1) {
      mp[dep] = i + 1;
      auto tmp = v;
      tmp.erase(tmp.begin() + i);
      dfs(tmp, dep + 1);
    }
    if (v[i] > i + 1)
      return;
  }
}

int main() {

  cin >> n;
  vector<int> v(n);
  REP(i, n) cin >> v[i];

  dfs(v, 0);
  cout << -1 << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define REV(i, n) for (ll(i) = (n)-1; (i) >= 0; --i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \
  {                                                                            \
    REP(WW, n) cerr << v[WW] << ' ';                                           \
    cerr << endl << endl;                                                      \
  }
#define SHOW2d(v, WW, HH)                                                      \
  {                                                                            \
    REP(W_, WW) {                                                              \
      REP(H_, HH) cerr << v[W_][H_] << ' ';                                    \
      cerr << endl;                                                            \
    }                                                                          \
    cerr << endl;                                                              \
  }
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 998244353
#define fastcin()                                                              \
  cin.tie(0);                                                                  \
  ios::sync_with_stdio(false)

typedef long long ll;
typedef pair<ll, ll> P;

int n;
int mp[111];

void out() {
  REV(i, n) cout << mp[i] << endl;
  exit(0);
}

void dfs(vector<int> &v, int dep) {
  if (v.size() == 0) {
    out();
  }
  REV(i, v.size()) {
    if (v[i] == i + 1) {
      mp[dep] = i + 1;
      auto tmp = v;
      tmp.erase(tmp.begin() + i);
      dfs(tmp, dep + 1);
    }
    if (v[i] > i + 1)
      return;
  }
}

int main() {

  cin >> n;
  vector<int> v(n);
  REP(i, n) cin >> v[i];

  dfs(v, 0);
  cout << -1 << endl;

  return 0;
}