Problem p03089 (C++)

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

// typedef
//------------------------------------------
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<string> VS;
typedef pair<LL, LL> PLL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define PB push_back
#define MP make_pair
#define SORT(c) sort((c).begin(), (c).end())
#define COUT(x) cout << x << endl

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;
const ULL INF = 1e18;
const int dx[]{1, -1, 0, 0}, dy[]{0, 0, 1, -1};

// function
//--------------------------------------------
LL gcd(LL, LL);
bool isEven(LL);
void coutY();
void coutN();

int main() {
  LL n;
  cin >> n;
  VLL b(n);
  REP(i, n) cin >> b[i];
  VLL ans;
  for (int i = b.size() - 1; i >= 0 && !b.empty();) {
    if (b[i] == i + 1) {
      // cout<<b[i]<<endl;
      ans.PB(b[i]);
      b.erase(b.begin() + i);
    } else {
      --i;
    }
  }
  if (b.empty()) {
    reverse(ALL(ans));
    REP(i, n) COUT(ans[i]);
  } else {
    COUT(-1);
  }
}

LL gcd(LL a, LL b) {
  if (a == 0 || b == 0)
    return 0;
  if (a < b)
    swap(a, b);
  if (a % b == 0)
    return b;
  else
    gcd(b, a % b);
}
bool isEven(LL a) { return !((bool)(a % 2)); }
void coutY() {
  cout << "Yes" << endl;
  return;
}
void coutN() {
  cout << "No" << endl;
  return;
}


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

// typedef
//------------------------------------------
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<string> VS;
typedef pair<LL, LL> PLL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define PB push_back
#define MP make_pair
#define SORT(c) sort((c).begin(), (c).end())
#define COUT(x) cout << x << endl

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;
const ULL INF = 1e18;
const int dx[]{1, -1, 0, 0}, dy[]{0, 0, 1, -1};

// function
//--------------------------------------------
LL gcd(LL, LL);
bool isEven(LL);
void coutY();
void coutN();

int main() {
  LL n;
  cin >> n;
  VLL b(n);
  REP(i, n) cin >> b[i];
  VLL ans;
  while (b[0] == 1 && !b.empty()) {
    for (int i = b.size() - 1; i >= 0 && !b.empty();) {
      if (b[i] == i + 1) {
        ans.PB(b[i]);
        b.erase(b.begin() + i);
        i = (int)b.size() - 1;
      } else {
        i--;
      }
    }
  }
  if (b.empty()) {
    reverse(ALL(ans));
    REP(i, n) COUT(ans[i]);
  } else {
    COUT(-1);
  }
}

LL gcd(LL a, LL b) {
  if (a == 0 || b == 0)
    return 0;
  if (a < b)
    swap(a, b);
  if (a % b == 0)
    return b;
  else
    gcd(b, a % b);
}
bool isEven(LL a) { return !((bool)(a % 2)); }
void coutY() {
  cout << "Yes" << endl;
  return;
}
void coutN() {
  cout << "No" << endl;
  return;
}