Problem p03089 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <complex>
#include <ctype.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout << (s) << endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define br() p("")
#define pn(s) cout << (#s) << " " << (s) << endl
#define p_yes() p("Yes")
#define p_no() p("No")

const ll mod = 1e9 + 7;
const ll inf = 1e18;

template <typename T> void vprint(T &V) {
  for (auto v : V) {
    cout << v << " ";
  }
  cout << endl;
}

// vectorからindex番目の要素を削除する
template <typename T> vector<T> remove(vector<T> A, ll index) {
  A.erase(A.begin() + index);
  return A;
}

vector<ll> dfs(ll N, vector<ll> A) {
  vector<ll> ret;
  if (N == 1) {
    if (A[0] == 1) {
      ret.push_back(1);
      return ret;
    } else {
      return ret;
    }
  }

  for (int i = N - 1; i >= 0; i--) {
    if (A[i] == i + 1) {
      auto list = dfs(N - 1, remove(A, i));
      if (list.size() > 0) {
        list.push_back(A[i]);
        return list;
      }
    }
  }

  // fail
  return ret;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  // input
  ll N;
  cin >> N;

  vector<ll> A(N);
  FOR(i, 0, N) { cin >> A.at(i); }

  auto list = dfs(N, A);

  if (list.size() == 0) {
    p(-1);
  } else {
    for (ll a : list) {
      p(a);
    }
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <complex>
#include <ctype.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout << (s) << endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define br() p("")
#define pn(s) cout << (#s) << " " << (s) << endl
#define p_yes() p("Yes")
#define p_no() p("No")

const ll mod = 1e9 + 7;
const ll inf = 1e18;

template <typename T> void vprint(T &V) {
  for (auto v : V) {
    cout << v << " ";
  }
  cout << endl;
}

// vectorからindex番目の要素を削除する
template <typename T> vector<T> remove(vector<T> A, ll index) {
  A.erase(A.begin() + index);
  return A;
}

vector<ll> dfs(ll N, vector<ll> A) {
  vector<ll> ret;
  if (N == 1) {
    if (A[0] == 1) {
      ret.push_back(1);
      return ret;
    } else {
      return ret;
    }
  }

  for (int i = N - 1; i >= 0; i--) {
    if (A[i] == i + 1) {
      auto list = dfs(N - 1, remove(A, i));
      if (list.size() > 0) {
        list.push_back(A[i]);
        return list;
      } else {
        return ret;
      }
    }
  }

  // fail
  return ret;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  // input
  ll N;
  cin >> N;

  vector<ll> A(N);
  FOR(i, 0, N) { cin >> A.at(i); }

  auto list = dfs(N, A);

  if (list.size() == 0) {
    p(-1);
  } else {
    for (ll a : list) {
      p(a);
    }
  }

  return 0;
}