Problem p03089 (C++)

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

using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using vint = vector<int>;
using vll = vector<ll>;
template <typename T> using uset = unordered_set<T>;
template <typename T1, typename T2> using umap = unordered_map<T1, T2>;

constexpr int INF = (1 << 30) - 1;
constexpr ll LLINF = 1LL << 60;
constexpr int dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
constexpr int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
constexpr char el = '\n';
constexpr int mod = 1000000007;

template <typename T> T gcd(T a, T b) { return (b ? gcd(b, a % b) : a); }
template <typename T> T lcm(T a, T b) { return (a / gcd(a, b) * b); }
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
  for (auto &u : v)
    os << u << endl;
  return (os);
}

template <typename T>
istream &operator>>(istream &is, pair<vector<T> &, int> v) {
  while (v.second--) {
    T a;
    v.first.push_back(is >> a, a);
  }
  return (is);
}

template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &u : v)
    is >> u;
  return (is);
}

// 1 1
int main() {
  int N;
  cin >> N;
  vint B(N);
  cin >> B;
  vint ans;
  for (int i = 0; i < N; i++) {
    int p = -1;
    for (int j = 0; j < B.size(); j++) {
      if (B[j] == j + 1) {
        p = j;
      }
    }
    ans.push_back(B[p]);
    B.erase(begin(B) + p);
  }
  reverse(begin(ans), end(ans));
  for (auto &v : ans)
    cout << v << el;
  return (0);
}


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

using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using vint = vector<int>;
using vll = vector<ll>;
template <typename T> using uset = unordered_set<T>;
template <typename T1, typename T2> using umap = unordered_map<T1, T2>;

constexpr int INF = (1 << 30) - 1;
constexpr ll LLINF = 1LL << 60;
constexpr int dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
constexpr int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
constexpr char el = '\n';
constexpr int mod = 1000000007;

template <typename T> T gcd(T a, T b) { return (b ? gcd(b, a % b) : a); }
template <typename T> T lcm(T a, T b) { return (a / gcd(a, b) * b); }
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
  for (auto &u : v)
    os << u << endl;
  return (os);
}

template <typename T>
istream &operator>>(istream &is, pair<vector<T> &, int> v) {
  while (v.second--) {
    T a;
    v.first.push_back(is >> a, a);
  }
  return (is);
}

template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &u : v)
    is >> u;
  return (is);
}

// 1 1
int main() {
  int N;
  cin >> N;
  vint B(N);
  cin >> B;
  vint ans;
  for (int i = 0; i < N; i++) {
    int p = -1;
    for (int j = 0; j < B.size(); j++) {
      if (B[j] == j + 1) {
        p = j;
      }
    }
    if (p == -1) {
      cout << -1 << endl;
      return (0);
    }
    ans.push_back(B[p]);
    B.erase(begin(B) + p);
  }
  reverse(begin(ans), end(ans));
  for (auto &v : ans)
    cout << v << el;
  return (0);
}