prithivMLmods/Coder-Stat huggingface dataset:
Problem p03089 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <stdio.h>
#include <vector>
#define FORi(N) for (int i = 0; i < N; ++i)
#define FORj(N) for (int j = 0; j < N; ++j)
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
ll gcd(ll m, ll n) {
  if (n == 0)
    return abs(m);
  return (gcd(n, m % n));
}
void putYN(bool b) {
  if (b) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
}
vector<ll> dp(vector<ll> &b, ll n) { // n以下をとる
  if (b.size() == 0)
    return vector<ll>();
  vector<ll> c, ret, d;
  for (int i = n - 1; i >= 0; --i) {
    if (b[i] == i + 1) {
      c = b;
      c.erase(c.begin() + i);
      ret = dp(c, n - 1);
      if (ret.size() == 1 && ret[0] == -1)
        continue;
      ret.push_back(i + 1);
      return ret;
    }
  }

  ret = vector<ll>{-1};
  return ret;
}
int main() {
  ll N;
  cin >> N;
  vector<ll> b(N);
  FORi(N) { cin >> b[i]; }
  vector<ll> ret = dp(b, N);
  FORi(ret.size()) { cout << ret[i] << endl; }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <stdio.h>
#include <vector>
#define FORi(N) for (int i = 0; i < N; ++i)
#define FORj(N) for (int j = 0; j < N; ++j)
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
ll gcd(ll m, ll n) {
  if (n == 0)
    return abs(m);
  return (gcd(n, m % n));
}
void putYN(bool b) {
  if (b) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
}
vector<ll> dp(vector<ll> &b, ll n) { // n以下をとる
  if (b.size() == 0)
    return vector<ll>();
  vector<ll> c, ret, d;
  for (int i = n - 1; i >= 0; --i) {
    if (b[i] == i + 1) {
      c = b;
      c.erase(c.begin() + i);
      ret = dp(c, n - 1);
      if (ret.size() == 1 && ret[0] == -1)
        break;
      ret.push_back(i + 1);
      return ret;
    }
  }

  ret = vector<ll>{-1};
  return ret;
}
int main() {
  ll N;
  cin >> N;
  vector<ll> b(N);
  FORi(N) { cin >> b[i]; }
  vector<ll> ret = dp(b, N);
  FORi(ret.size()) { cout << ret[i] << endl; }
  return 0;
}
Problem p03089 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MOD 1e9 + 7
using namespace std;
#define pp push_back
#define po pop_back
#define mp make_pair
#define clr(a) memset(a, 0, sizeof(a))
#define neg(a) memset(a, -1, sizeof(a))
#define fas(a) memset(a, false, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define debug1(x) cout << #x << ": " << x << " "
#define rev(x) reverse(x.begin(), x.end())
#define int long long
#define F first
#define S second
// int a[2000];
void printvector(std::vector<int> v) {
  for (int i = 0; i < v.size(); ++i) {
    cout << v[i] << " ";
  }
  cout << "" << endl;
}
void printarray(int a[], int n) {
  for (int i = 0; i < n; ++i) {
    cout << a[i] << " ";
  }
  cout << "" << endl;
}

int get(std::vector<int> &v) {

  // printvector(v);
  int got = -1;
  for (int i = v.size() - 1; i >= 0; i--) {
    if (v[i] == i + 1) {
      got = v[i];
      for (int j = i; j <= (int)v.size() - 2; j++) {
        // if(v.size()==9)debug(j);
        v[j] = v[j + 1];
      }
      break;
    }
  }

  v.pop_back();

  return got;
}

void solve() {
  int n;
  cin >> n;
  std::vector<int> v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  std::vector<int> ans;
  for (int j = 0; j < n; j++) {
    int g = get(v);
    if (g == -1) {
      cout << -1 << endl;
      return;
    }
    ans.pp(g);
  }

  rev(ans);
  for (int i = 0; i < n; i++)
    cout << ans[i] << endl;
}

int32_t main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  solve();

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define MOD 1e9 + 7
using namespace std;
#define pp push_back
#define po pop_back
#define mp make_pair
#define clr(a) memset(a, 0, sizeof(a))
#define neg(a) memset(a, -1, sizeof(a))
#define fas(a) memset(a, false, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define debug1(x) cout << #x << ": " << x << " "
#define rev(x) reverse(x.begin(), x.end())
#define int long long
#define F first
#define S second
// int a[2000];
void printvector(std::vector<int> v) {
  for (int i = 0; i < v.size(); ++i) {
    cout << v[i] << " ";
  }
  cout << "" << endl;
}
void printarray(int a[], int n) {
  for (int i = 0; i < n; ++i) {
    cout << a[i] << " ";
  }
  cout << "" << endl;
}

int get(std::vector<int> &v) {

  // printvector(v);
  int got = -1;
  for (int i = v.size() - 1; i >= 0; i--) {
    if (v[i] == i + 1) {
      got = v[i];
      for (int j = i; j <= (int)v.size() - 2; j++) {
        // if(v.size()==9)debug(j);
        v[j] = v[j + 1];
      }
      break;
    }
  }

  v.pop_back();

  return got;
}

void solve() {
  int n;
  cin >> n;
  std::vector<int> v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  std::vector<int> ans;
  for (int j = 0; j < n; j++) {
    int g = get(v);
    if (g == -1) {
      cout << -1 << endl;
      return;
    }
    ans.pp(g);
  }

  rev(ans);
  for (int i = 0; i < n; i++)
    cout << ans[i] << endl;
}

int32_t main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  solve();

  return 0;
}
Problem p03089 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bits/stdc++.h>
#include <vector>
#define RANGE(i, n) for (int i = 0; i < n; i++)
typedef long long ll;

using namespace std;

stack<ll> answer;

bool is_valid(const vector<ll> &in) {
  ll in_size = in.size();
  if (in_size == 0)
    return true;
  else {
    vector<ll> possible;
    RANGE(i, in_size) {
      if (in[i] == i + 1)
        possible.push_back(i);
    }
    for (ll poss : possible) {
      answer.push(poss + 1);
      vector<ll> next_vec;
      RANGE(j, in_size) {
        if (j == poss)
          continue;
        next_vec.push_back(in[j]);
      }
      if (is_valid(next_vec))
        return true;
      else
        answer.pop();
    }
    return false;
  }
}

signed main() {
  int N;
  cin >> N;
  vector<ll> b;
  for (int i = 0; i < N; i++) {
    ll a;
    cin >> a;
    b.push_back(a);
  }
  if (is_valid(b)) {
    while (!answer.empty()) {
      cout << answer.top() << endl;
      answer.pop();
    }
  } else {
    cout << -1 << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <vector>
#define RANGE(i, n) for (int i = 0; i < n; i++)
typedef long long ll;

using namespace std;

stack<ll> answer;

bool is_valid(const vector<ll> &in) {
  ll in_size = in.size();
  if (in_size == 0)
    return true;
  else {
    vector<ll> possible;
    RANGE(i, in_size) {
      if (in[i] == i + 1)
        possible.push_back(i);
      else if (in[i] > i + 1)
        return false;
    }
    for (ll poss : possible) {
      answer.push(poss + 1);
      vector<ll> next_vec;
      RANGE(j, in_size) {
        if (j == poss)
          continue;
        next_vec.push_back(in[j]);
      }
      if (is_valid(next_vec))
        return true;
      else
        answer.pop();
    }
    return false;
  }
}

signed main() {
  int N;
  cin >> N;
  vector<ll> b;
  for (int i = 0; i < N; i++) {
    ll a;
    cin >> a;
    b.push_back(a);
  }
  if (is_valid(b)) {
    while (!answer.empty()) {
      cout << answer.top() << endl;
      answer.pop();
    }
  } else {
    cout << -1 << endl;
  }
}
Problem p03089 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
#define ll long long
#define N 110
char getc() {
  char c = getchar();
  while ((c < 'A' || c > 'Z') && (c < 'a' || c > 'z') && (c < '0' || c > '9'))
    c = getchar();
  return c;
}
int gcd(int n, int m) { return m == 0 ? n : gcd(m, n % m); }
int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  return x * f;
}
int n, a[N], ans[N];
signed main() {
#ifndef ONLINE_JUDGE
  freopen("a.in", "r", stdin);
  freopen("a.out", "w", stdout);
#endif
  n = read();
  for (int i = 1; i <= n; i++)
    a[i] = read();
  for (int i = n; i >= 1; i--)
    for (int j = i; j >= 1; j--)
      if (a[j] == j) {
        for (int k = j; k < i; k++)
          a[k] = a[k + 1];
        ans[i] = j;
        break;
      }
  for (int i = 1; i <= n; i++)
    if (!ans[i]) {
      cout << -1;
      return 0;
    }
  for (int i = 1; i <= n; i++)
    cout << ans[i] << endl;
  return 0;
  // NOTICE LONG LONG!!!!!
}
// 对于第i个位置上的a[i] 需要满足其之后有i-a[i]个比它小的数被放进去
//

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
#define ll long long
#define N 110
char getc() {
  char c = getchar();
  while ((c < 'A' || c > 'Z') && (c < 'a' || c > 'z') && (c < '0' || c > '9'))
    c = getchar();
  return c;
}
int gcd(int n, int m) { return m == 0 ? n : gcd(m, n % m); }
int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  return x * f;
}
int n, a[N], ans[N];
signed main() {
  n = read();
  for (int i = 1; i <= n; i++)
    a[i] = read();
  for (int i = n; i >= 1; i--)
    for (int j = i; j >= 1; j--)
      if (a[j] == j) {
        for (int k = j; k < i; k++)
          a[k] = a[k + 1];
        ans[i] = j;
        break;
      }
  for (int i = 1; i <= n; i++)
    if (!ans[i]) {
      cout << -1;
      return 0;
    }
  for (int i = 1; i <= n; i++)
    cout << ans[i] << endl;
  return 0;
  // NOTICE LONG LONG!!!!!
}
// 对于第i个位置上的a[i] 需要满足其之后有i-a[i]个比它小的数被放进去
//
Problem p03089 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
vector<int> vet;
int n;
int main() {
  cin >> n;
  vet.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> vet[i];
    vet[i]--;
  }

  vector<int> ans;
  bool f = 1;
  while (f and vet.size()) {
    bool f = 0;
    for (int i = vet.size() - 1; i >= 0; i--) {
      if (vet[i] == i) {
        vet.erase(i + vet.begin());
        ans.push_back(i + 1);
        f = 1;
        break;
      }
    }
  }
  if (vet.size())
    cout << -1;
  else {
    reverse(ans.begin(), ans.end());
    for (auto it : ans)
      cout << it << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<int> vet;
int n;
int main() {
  cin >> n;
  vet.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> vet[i];
    vet[i]--;
  }

  vector<int> ans;
  bool f = 1;
  while (f and vet.size()) {
    f = 0;
    for (int i = vet.size() - 1; i >= 0; i--) {
      if (vet[i] == i) {
        vet.erase(i + vet.begin());
        ans.push_back(i + 1);
        f = 1;
        break;
      }
    }
  }
  if (vet.size())
    cout << -1;
  else {
    reverse(ans.begin(), ans.end());
    for (auto it : ans)
      cout << it << endl;
  }
}
Problem p03089 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (ll i = (ll)(a); i < (ll)(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define ll long long
#define lld long double
#ifdef DEBUG
#define line() cerr << "[" << __LINE__ << "] " << endl;
#define dump(i) cerr << #i ": " << i << " ";
#define dumpl(i) cerr << #i ": " << i << endl;
#else
#define line(i)
#define dump(i)
#define dumpl(i)
#endif
using namespace std;
vector<int> bfs(vector<int> v, vector<int> now, int last) {

  if (v.empty()) {
    return now;
  }
  int i = v.size();
  for (auto itr = v.end() - 1; itr != v.begin() - 1; itr--) {
    auto _v = v;
    if (i == *itr) {
      _v.erase(_v.begin() + i - 1);
      now.push_back(i);

      vector<int> res = bfs(_v, now, i - 1);
      if (res[0] != -1)
        return res;
      now.pop_back();
    }
    i--;
  }

  vector<int> ret = {-1};
  return ret;
}

int main(int argc, char const *argv[]) {
  int n;
  cin >> n;
  vector<int> b;
  rep(i, n) {
    int t;
    cin >> t;
    b.push_back(t);
  }
  auto res = bfs(b, {}, 0);
  reverse(res.begin(), res.end());
  for (int ans : res) {
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (ll i = (ll)(a); i < (ll)(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define ll long long
#define lld long double
#ifdef DEBUG
#define line() cerr << "[" << __LINE__ << "] " << endl;
#define dump(i) cerr << #i ": " << i << " ";
#define dumpl(i) cerr << #i ": " << i << endl;
#else
#define line(i)
#define dump(i)
#define dumpl(i)
#endif
using namespace std;
vector<int> bfs(vector<int> v, vector<int> now, int last) {

  if (v.empty()) {
    return now;
  }
  int i = v.size();
  for (auto itr = v.end() - 1; itr != v.begin() - 1; itr--) {
    auto _v = v;
    if (i == *itr) {
      _v.erase(_v.begin() + i - 1);
      now.push_back(i);

      vector<int> res = bfs(_v, now, i - 1);
      if (res[0] != -1)
        return res;
      now.pop_back();
    }
    i--;
  }

  vector<int> ret = {-1};
  return ret;
}

int main(int argc, char const *argv[]) {
  int n;
  cin >> n;
  vector<int> b;
  rep(i, n) {
    int t;
    cin >> t;
    b.push_back(t);
  }
  rep(i, n) {
    if (b[i] > i + 1) {
      cout << -1 << endl;
      return 0;
    }
  }
  auto res = bfs(b, {}, 0);
  reverse(res.begin(), res.end());
  for (int ans : res) {
    cout << ans << endl;
  }
  return 0;
}
Problem p03089 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007

int main() {
  ll n;
  cin >> n;
  vector<ll> a;
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    a.push_back(x);
  }
  vector<ll> b;
  while (a.size() != 0) {
    for (int i = a.size() - 1; i >= 0; i--) {
      ll now = a[i];
      if (now == i + 1) {
        b.push_back(now);
        a.erase(a.begin() + i);
        break;
      }
      if (now < a[i]) {
        cout << -1;
        return 0;
      }
    }
  }
  reverse(b.begin(), b.end());
  for (int i = 0; i < n; i++) {
    cout << b[i] << " ";
  }
  // your code goes here
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007

int main() {
  ll n;
  cin >> n;
  vector<ll> a;
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    a.push_back(x);
  }
  vector<ll> b;
  while (a.size() != 0) {
    for (int i = a.size() - 1; i >= 0; i--) {
      ll now = a[i];
      if (now == i + 1) {
        b.push_back(now);
        a.erase(a.begin() + i);
        break;
      }
      if (now > i + 1) {
        cout << -1;
        return 0;
      }
    }
  }
  reverse(b.begin(), b.end());
  for (int i = 0; i < n; i++) {
    cout << b[i] << " ";
  }
  // your code goes here
  return 0;
}
Problem p03089 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;

int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};

vector<int> tejun;

vector<int> solve(vector<int> v) {
  /*REP(i,v.size()){
    cout<<v[i]<<"    ";
  }
  cout<<endl;*/

  if (v.size() == 1 && v[0] == 1) {
    tejun.pb(1);
    return v; // 現場へお返ししまーす
  }

  REP(i, v.size()) {
    if (v[i] == i + 1) {
      vector<int> v2;
      copy(v.begin(), v.end(), back_inserter(v2));
      v2.erase(v2.begin() + i);
      vector<int> res = solve(v2);

      if (res.size() == 0) {
        // だめだった。。。

      } else {
        tejun.pb(i + 1);
        return v;
      }
    }
  }

  // ダメだった時
  vector<int> dame;
  return dame;
}

int main() {

  int n;
  cin >> n;
  vector<int> v;

  REP(i, n) {
    int t;
    cin >> t;
    v.pb(t);
  }

  REP(i, n) {
    if (v[i] > i + 1) {
      cout << -1 << endl;
      return 0;
    }
  }

  solve(v);

  if (tejun.size() == 0) {
    cout << -1 << endl;
    return 0;
  }
  REP(i, n) { cout << tejun[i] << endl; }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;

int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};

vector<int> tejun;

vector<int> solve(vector<int> v) {
  /*REP(i,v.size()){
    cout<<v[i]<<"    ";
  }
  cout<<endl;*/
  REP(i, v.size()) {
    if (v[i] > i + 1) {
      vector<int> dame;
      return dame;
    }
  }

  if (v.size() == 1 && v[0] == 1) {
    tejun.pb(1);
    return v; // 現場へお返ししまーす
  }

  REP(i, v.size()) {
    if (v[i] == i + 1) {
      vector<int> v2;
      copy(v.begin(), v.end(), back_inserter(v2));
      v2.erase(v2.begin() + i);
      vector<int> res = solve(v2);

      if (res.size() == 0) {
        // だめだった。。。

      } else {
        tejun.pb(i + 1);
        return v;
      }
    }
  }

  // ダメだった時
  vector<int> dame;
  return dame;
}

int main() {

  int n;
  cin >> n;
  vector<int> v;

  REP(i, n) {
    int t;
    cin >> t;
    v.pb(t);
  }

  REP(i, n) {
    if (v[i] > i + 1) {
      cout << -1 << endl;
      return 0;
    }
  }

  solve(v);

  if (tejun.size() == 0) {
    cout << -1 << endl;
    return 0;
  }
  REP(i, n) { cout << tejun[i] << endl; }

  return 0;
}
Problem p03089 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
#define FOR(i, s, n) for (int i = (s); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) for (int i = (n); i >= 0; i--)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
#define ATYN(n) cout << ((n) ? "Yes" : "No") << '\n';
#define CFYN(n) cout << ((n) ? "YES" : "NO") << '\n';
#define OUT(n) cout << (n) << '\n';
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

// vector print(debug or stdout)
template <class Iit> void vout(const Iit &begin, const Iit &end, ostream &out) {
  for (auto it = begin; it != end; it++) {
    auto tmp = it;
    if (++tmp == end)
      out << *it << '\n';
    else
      out << *it << " ";
  }
}

int main(void) {
  IOS int N;
  cin >> N;
  list<int> B;
  REP(i, N) {
    int b;
    cin >> b;
    B.push_back(b);
  }

  vector<int> ans;
  REP(i, N) {
    // vout(ALL(ans),cerr);
    auto it = B.end();
    it--;
    RREP(j, N - i) {
      if (j == 0) {
        OUT(-1)
        return 0;
      }
      if (*it == j) {
        ans.insert(ans.begin() + j - 1, j);
        B.erase(it);
        break;
      }
      it--;
    }
  }
  reverse(ALL(ans));
  REP(i, N)
  OUT(ans[i])

      return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
#define FOR(i, s, n) for (int i = (s); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) for (int i = (n); i >= 0; i--)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
#define ATYN(n) cout << ((n) ? "Yes" : "No") << '\n';
#define CFYN(n) cout << ((n) ? "YES" : "NO") << '\n';
#define OUT(n) cout << (n) << '\n';
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

// vector print(debug or stdout)
template <class Iit> void vout(const Iit &begin, const Iit &end, ostream &out) {
  for (auto it = begin; it != end; it++) {
    auto tmp = it;
    if (++tmp == end)
      out << *it << '\n';
    else
      out << *it << " ";
  }
}

int main(void) {
  IOS int N;
  cin >> N;
  list<int> B;
  REP(i, N) {
    int b;
    cin >> b;
    B.push_back(b);
  }

  vector<int> ans;
  REP(i, N) {
    // vout(ALL(ans),cerr);
    auto it = B.end();
    it--;
    RREP(j, N - i) {
      if (j == 0) {
        OUT(-1)
        return 0;
      }
      if (*it == j) {
        ans.push_back(j);
        B.erase(it);
        break;
      }
      it--;
    }
  }
  reverse(ALL(ans));
  REP(i, N)
  OUT(ans[i])

      return 0;
}
Problem p03089 (C++)

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

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)

#define INF 100000000
#define MOD 1000000007
#define writeln(n) cout << n << endl
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;
// vector<tuple<long long, long long, long long>>vec;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};

const llong inf = 100000000;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> b(n, 0);
  REP(i, n) { cin >> b[i], --b[i]; }

  vector<int> ans;

  for (int j = 0; j < n; j++) {
    int cnt = -1;
    for (int i = n - 1; i >= 0; --i) {
      if (b[i] == i) {
        cnt = i;
        break;
      }
    }

    if (cnt == -1) {
      cout << -1 << endl;
      return 0;
    }
    ans.push_back(cnt + 1);
    b.erase(b.begin() + cnt);
  }
  reverse(ans.begin(), ans.end());
  for (auto v : ans)
    cout << v << endl;
  return 0;
}

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

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)

#define INF 100000000
#define MOD 1000000007
#define writeln(n) cout << n << endl
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;
// vector<tuple<long long, long long, long long>>vec;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};

const llong inf = 100000000;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> b(n, 0);
  REP(i, n) { cin >> b[i], --b[i]; }

  vector<int> ans;

  for (int j = 0; j < n; j++) {
    int cnt = -1;
    for (int i = (int)b.size() - 1; i >= 0; --i) // b.size()が毎回切り替わる
    {
      if (b[i] == i) {
        cnt = i;
        break;
      }
    }

    if (cnt == -1) {
      cout << -1 << endl;
      return 0;
    }
    ans.push_back(cnt + 1);
    b.erase(b.begin() + cnt);
  }
  reverse(ans.begin(), ans.end());
  for (auto v : ans)
    cout << v << endl;
  return 0;
}
Problem p03089 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int N;
bool fin = false;

int search(vector<int> xx, vector<int> ans) {
  // int check = 1;
  // bool flag = false;
  if (xx.size() == 1) {
    for (int i = ans.size() - 1; i >= 0; i--) {
      cout << ans.at(i) << endl;
    }
    fin = true;
    return 0;
  }
  for (int i = xx.size() - 1; i > 0; i--) {
    if (fin) {
      return 0;
    }
    if (xx.at(i) == i) {
      // erase
      //  flag = true;
      vector<int> new_xx = xx;
      new_xx.erase(new_xx.begin() + i);

      ans.push_back(i);
      search(new_xx, ans);
      // check *= search(new_xx, ans);
      ans.pop_back();
    }
  }
  return 0;
}

int main() {
  cin >> N;
  vector<int> b = vector<int>(N + 1);
  for (int i = 1; i < N + 1; i++) {
    cin >> b.at(i);
  }
  vector<int> ans;
  search(b, ans);
  if (fin == false) {
    cout << -1 << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int N;
bool fin = false;

int search(vector<int> xx, vector<int> ans) {
  // int check = 1;
  // bool flag = false;
  for (int i = 1; i < xx.size(); i++) {
    if (xx.at(i) > i) {
      cout << -1 << endl;
      fin = true;
      return 0;
    }
  }
  if (xx.size() == 1) {
    for (int i = ans.size() - 1; i >= 0; i--) {
      cout << ans.at(i) << endl;
    }
    fin = true;
    return 0;
  }
  for (int i = xx.size() - 1; i > 0; i--) {
    if (fin) {
      return 0;
    }
    if (xx.at(i) == i) {
      // erase
      //  flag = true;
      vector<int> new_xx = xx;
      new_xx.erase(new_xx.begin() + i);

      ans.push_back(i);
      search(new_xx, ans);
      // check *= search(new_xx, ans);
      ans.pop_back();
    }
  }
  return 0;
}

int main() {
  cin >> N;
  vector<int> b = vector<int>(N + 1);
  for (int i = 1; i < N + 1; i++) {
    cin >> b.at(i);
  }
  vector<int> ans;
  search(b, ans);
  if (fin == false) {
    cout << -1 << endl;
  }
  return 0;
}
Problem p03089 (C++)

Buggy submission (status: Time Limit Exceeded):
// Robs Code
/***********HEADER***************/
#include <bits/stdc++.h>
/***********MACROS***************/
#define ll long long int
#define fri(l, k) for (i = l; i <= k; i++)
#define frj(l, k) for (j = l; j >= k; j--)
#define fij(a, b, l, k)                                                        \
  for (i = a; i <= b; i++)                                                     \
    for (j = l; j <= k; j++)
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define endl "\n"
#define pb push_back
#define _1 first
#define _2 second
#define mxsz 10000007
#define mxval 1e9
#define grt [](auto x, auto y) { return x > y; }
#define pii pair<ll, ll>
/*********VARIABLE****************/
using namespace std;
ll i, j, n, m, k, l, q, t, a, b, sum[3], tot[3], cnt[3];
/***********MAIN******************/
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  vector<ll> v(n);
  for (i = 0; i < n; i++)
    cin >> v[i];
  stack<ll> st;
  bool flag = 0;
  while (v.size()) {
    flag = 0;
    for (i = v.size() - 1; i >= 0; i--) {
      if (v[i] == i + 1) {
        flag = 1;
        st.push(i + 1);
        v.erase(v.begin() + i);
        break;
      }
    }
  }
  if (flag == 0) {
    cout << -1;
    return 0;
  }
  while (!st.empty()) {
    cout << st.top() << "\n";
    st.pop();
  }
  return 0;
}
//.........

Fixed submission:
// Robs Code
/***********HEADER***************/
#include <bits/stdc++.h>
/***********MACROS***************/
#define ll long long int
#define fri(l, k) for (i = l; i <= k; i++)
#define frj(l, k) for (j = l; j >= k; j--)
#define fij(a, b, l, k)                                                        \
  for (i = a; i <= b; i++)                                                     \
    for (j = l; j <= k; j++)
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define endl "\n"
#define pb push_back
#define _1 first
#define _2 second
#define mxsz 10000007
#define mxval 1e9
#define grt [](auto x, auto y) { return x > y; }
#define pii pair<ll, ll>
/*********VARIABLE****************/
using namespace std;
ll i, j, n, m, k, l, q, t, a, b, sum[3], tot[3], cnt[3];
/***********MAIN******************/
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  vector<ll> v(n);
  for (i = 0; i < n; i++)
    cin >> v[i];
  stack<ll> st;
  bool flag = 0;
  while (v.size()) {
    flag = 0;
    for (i = v.size() - 1; i >= 0; i--) {
      if (v[i] == i + 1) {
        flag = 1;
        st.push(i + 1);
        v.erase(v.begin() + i);
        break;
      }
    }
    if (flag == 0) {
      cout << -1;
      return 0;
    }
  }
  while (!st.empty()) {
    cout << st.top() << "\n";
    st.pop();
  }
  return 0;
}
//.........
Problem p03089 (C++)

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

int main() {
  int N;
  cin >> N;
  vector<int> v(N);
  for (int i = 0; i < N; i++)
    cin >> v[i];
  vector<int> r(N);
  for (int i = N - 1; i >= 0; i--) {
    int p = -1;
    for (int j = 0; j < N; j++) {
      if (v[j] == j + 1)
        p = j;
    }
    if (p == -1) {
      cout << -1 << endl;
      return 0;
    }
    r[i] = p + 1;
    v.erase(v.begin() + p);
  }
  for (int i = 0; i < N; i++)
    cout << r[i] << endl;
}

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

int main() {
  int N;
  cin >> N;
  vector<int> v(N);
  for (int i = 0; i < N; i++)
    cin >> v[i];
  vector<int> r(N);
  for (int i = N - 1; i >= 0; i--) {
    int p = -1;
    for (int j = 0; j < v.size(); j++) {
      if (v[j] == j + 1)
        p = j;
    }
    if (p == -1) {
      cout << -1 << endl;
      return 0;
    }
    r[i] = p + 1;
    v.erase(v.begin() + p);
  }
  for (int i = 0; i < N; i++)
    cout << r[i] << endl;
}
Problem p03089 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

typedef long long ll;
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(i, n) repi(i, 0, n)
#define rerepi(i, a, b) for (int i = int(a) - 1; i >= b; --i)
#define rerep(i, n) rerepi(i, n, 0)

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T>
inline void remove(std::vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

const long long INF = 1LL << 60;

// ここから開始だよ！
ll N, mark;
vector<ll> B, ans;

int main() {
  // 呪文エリア
  cin.tie(0);
  ios::sync_with_stdio(false);
  // 詠唱完了

  cin >> N;
  rep(i, N) {
    ll tmp;
    cin >> tmp;
    B.push_back(tmp);
  }

  while (!B.empty()) {
    mark = -1;
    rep(i, B.size()) {
      if (B[i] == i + 1 && mark < i + 1) {
        mark = i + 1;
      }
    }
    if (mark == -1) {
      cout << -1 << endl;
      return 0;
    } else {
      remove(B, mark);
      ans.push_back(mark);
    }
  }

  rerep(i, ans.size()) { cout << ans[i] << endl; }

  return 0;
}


Fixed submission:
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

typedef long long ll;
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(i, n) repi(i, 0, n)
#define rerepi(i, a, b) for (int i = int(a) - 1; i >= b; --i)
#define rerep(i, n) rerepi(i, n, 0)

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T>
inline void remove(std::vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

const long long INF = 1LL << 60;

// ここから開始だよ！
ll N, mark;
vector<ll> B, ans;

int main() {
  // 呪文エリア
  cin.tie(0);
  ios::sync_with_stdio(false);
  // 詠唱完了

  cin >> N;
  rep(i, N) {
    ll tmp;
    cin >> tmp;
    B.push_back(tmp);
  }

  while (!B.empty()) {
    mark = -1;
    rep(i, B.size()) {
      if (B[i] == i + 1 && mark < i + 1) {
        mark = i + 1;
      }
    }
    if (mark == -1) {
      cout << -1 << endl;
      return 0;
    } else {
      remove(B, mark - 1);
      ans.push_back(mark);
    }
  }

  rerep(i, ans.size()) { cout << ans[i] << endl; }

  return 0;
}
Problem p03089 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <functional>
#include <vector>

#define MAX_N 100

using namespace std;

int main() {
  int N;
  scanf("%d", &N);
  int b[N], ans[MAX_N] = {0};
  vector<int> bvec;
  for (int i = 0; i < N; i++) {
    scanf("%d", b + i);
    bvec.push_back(b[i]);
  }
  sort(b, b + N, greater<int>());
  for (int i = 0; i < N; i++) {
    int a = b[i],
        n = (int)(lower_bound(bvec.begin(), bvec.end(), a) - bvec.begin());
    bvec.erase(bvec.begin() + n);
    int count = 0;
    bool inserted = false;
    for (int j = N - 1; j >= 0; j--) {
      if (ans[j] != 0)
        continue;
      if (count == n - a + 1) {
        ans[j] = a;
        inserted = true;
        break;
      }
      count++;
    }
    if (!inserted) {
      printf("-1\n");
      return 0;
    }
  }
  for (int i = 0; i < N; i++)
    printf("%d\n", ans[i]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <functional>
#include <vector>

#define MAX_N 100

using namespace std;

int main() {
  int N;
  scanf("%d", &N);
  int b[N], ans[MAX_N] = {0};
  vector<int> bvec;
  for (int i = 0; i < N; i++) {
    scanf("%d", b + i);
    bvec.push_back(b[i]);
  }
  sort(b, b + N, greater<int>());
  for (int i = 0; i < N; i++) {
    int a = b[i],
        n = (int)(bvec.rend() - find(bvec.rbegin(), bvec.rend(), a)) - 1;
    bvec.erase(bvec.begin() + n);
    int count = 0;
    bool inserted = false;
    for (int j = N - 1; j >= 0; j--) {
      if (ans[j] != 0)
        continue;
      if (count == n - a + 1) {
        ans[j] = a;
        inserted = true;
        break;
      }
      count++;
    }
    if (!inserted) {
      printf("-1\n");
      return 0;
    }
  }
  for (int i = 0; i < N; i++)
    printf("%d\n", ans[i]);
  return 0;
}
Problem p03089 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>

using namespace std;

void Solve();

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(20) << fixed;
  Solve();
}

/****************************************\
| Thank you for viewing my code:)        |
| Written by RedSpica a.k.a. RanseMirage |
| Twitter:@asakaakasaka                  |
\****************************************/

void Solve() {
  int n;
  scanf("%d", &n);
  vector<int> A(n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &A[i]);
  }

  vector<int> B(n);
  for (int i = n; i >= 0; i--) {
    for (int j = i; j >= 0; j--) {
      if (A[j - 1] == j) {
        B[i - 1] = j;
        A.erase(A.begin() + j - 1);
        break;
      }
    }
  }

  if (B[0] == 0) {
    printf("%d\n", -1);
    return;
  }

  for (int i = 0; i < n; i++) {
    printf("%d\n", B[i]);
  }
}

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

using namespace std;

void Solve();

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(20) << fixed;
  Solve();
}

/****************************************\
| Thank you for viewing my code:)        |
| Written by RedSpica a.k.a. RanseMirage |
| Twitter:@asakaakasaka                  |
\****************************************/

void Solve() {
  int n;
  scanf("%d", &n);
  vector<int> A(n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &A[i]);
  }

  vector<int> B(n);
  for (int i = n; i > 0; i--) {
    for (int j = i; j > 0; j--) {
      if (A[j - 1] == j) {
        B[i - 1] = j;
        A.erase(A.begin() + j - 1);
        break;
      }
    }
  }

  if (B[0] == 0) {
    printf("%d\n", -1);
    return;
  }

  for (int i = 0; i < n; i++) {
    printf("%d\n", B[i]);
  }
}
Problem p03089 (C++)

Buggy submission (status: Runtime Error):
// In the name of God

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  vector<int> ans;
  for (int i = 0; i < n; i++) {
    ans.insert(ans.begin() + v[i] - 1, v[i]);
  }
  for (int i = 0; i < n; i++)
    cout << ans[i] << endl;
  return 0;
}


Fixed submission:
// In the name of God

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  vector<int> ans;
  for (int i = 0; i < n; i++) {
    if (ans.size() < v[i] - 1) {
      cout << -1;
      exit(0);
    }
    ans.insert(ans.begin() + v[i] - 1, v[i]);
  }
  for (int i = 0; i < n; i++)
    cout << ans[i] << endl;
  return 0;
}
Problem p03089 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
vector<int> V, ans;

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-')
      f = -1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = (x << 1) + (x << 3) + ch - '0';
  return x * f;
}

int main() {
  int n = read();
  for (int i = 1; i <= n; i++)
    V.push_back(read() - 1);
  for (int i = 1; i <= n; i++) {
    bool flag = 0;
    for (int j = V.size(); ~j; j--)
      if (V[j] == j) {
        flag = 1;
        ans.push_back(j + 1);
        V.erase(V.begin() + j);
        break;
      }
    if (!flag) {
      puts("-1");
      return 0;
    }
  }
  for (int i = ans.size() - 1; ~i; i--)
    printf("%d\n", ans[i]);
  return 0;
}


Fixed submission:
#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
vector<int> V, ans;

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-')
      f = -1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = (x << 1) + (x << 3) + ch - '0';
  return x * f;
}

int main() {
  int n = read();
  for (int i = 1; i <= n; i++)
    V.push_back(read() - 1);
  for (int i = 1; i <= n; i++) {
    bool flag = 0;
    for (int j = V.size() - 1; ~j; j--)
      if (V[j] == j) {
        flag = 1;
        ans.push_back(j + 1);
        V.erase(V.begin() + j);
        break;
      }
    if (!flag) {
      puts("-1");
      return 0;
    }
  }
  for (int i = ans.size() - 1; ~i; i--)
    printf("%d\n", ans[i]);
  return 0;
}
Problem p03089 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

const int INF = 1e9;

vector<int> check(vector<int> b, int n) {
  if (n == 1) {
    vector<int> ret;
    if (b[0] == 1)
      ret.push_back(1);
    return ret;
  }
  vector<int> ans;
  if (b[0] != 1) {
    return ans;
  }
  int max = *max_element(b.begin(), b.end());
  if (max > n) {
    return ans;
  }
  for (int i = 1; i <= n; i++) {
    if (b[i - 1] == i) {
      vector<int> b2 = b;
      b2.erase(b2.begin() + i - 1);
      ans = check(b2, n - 1);
      if (ans.size() != 0) {
        ans.push_back(i);
        return ans;
      }
    }
  }
  return ans;
}

int main() {
  int n;
  cin >> n;
  vector<int> b(n);
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  vector<int> ans = check(b, n);
  if (ans.size() != 0) {
    for (int i = 0; i < n; i++) {
      cout << ans[i] << endl;
    }
  } else {
    cout << -1 << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

const int INF = 1e9;

vector<int> check(vector<int> b, int n) {
  if (n == 1) {
    vector<int> ret;
    if (b[0] == 1)
      ret.push_back(1);
    return ret;
  }
  vector<int> ans;
  for (int i = 0; i < n; i++) {
    if (b[i] > i + 1)
      return ans;
  }
  for (int i = 1; i <= n; i++) {
    if (b[i - 1] == i) {
      vector<int> b2 = b;
      b2.erase(b2.begin() + i - 1);
      ans = check(b2, n - 1);
      if (ans.size() != 0) {
        ans.push_back(i);
        return ans;
      }
    }
  }
  return ans;
}

int main() {
  int n;
  cin >> n;
  vector<int> b(n);
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  vector<int> ans = check(b, n);
  if (ans.size() != 0) {
    for (int i = 0; i < n; i++) {
      cout << ans[i] << endl;
    }
  } else {
    cout << -1 << endl;
  }
}
Problem p03089 (C++)

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

map<vector<int>, vector<int>> memo;

vector<int> f(vector<int> &a) {
  int n = a.size();
  // for (int i = 0; i < n; i++) {
  //   cout << a[i] << " ";
  // }
  // cout << endl;

  vector<int> res;
  if (n == 1) {
    return res;
  }

  if (memo[a].size() > 0 && memo[a][0] < 0) {
    return vector<int>();
  } else if (memo[a].size() > 0) {
    return memo[a];
  }

  for (int i = n - 1; 0 < i; i--) {
    if (i == a[i]) {
      vector<int> aa = a;
      aa.erase(aa.begin() + i);
      res = f(aa);
      if (res.size() == n - 2) {
        res.push_back(i);
        memo[a] = res;
        return res;
      } else if (i == n - 1) {
        break;
      }
    }
  }

  // for (int i = 0; i < res.size(); i++) {
  //   cout << res[i] << " ";
  // }
  // cout << endl;

  memo[a] = vector<int>{-1};
  return vector<int>();
}

int main(void) {
  int n;
  cin >> n;

  vector<int> b(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
  }

  vector<int> res = f(b);

  if (res.size() == n) {
    for (int i = 0; i < n; i++) {
      cout << res[i] << endl;
    }
  } else {
    cout << -1 << endl;
  }

  // for (auto a:memo) {
  //   for (int i = 0; i < a.first.size(); i++) {
  //     cout << a.first[i] << " ";
  //   }
  //   cout << endl;
  //   for (int i = 0; i < a.second.size(); i++) {
  //     cout << a.second[i] << " ";
  //   }
  //   cout << endl;
  // }

  return 0;
}


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

map<vector<int>, vector<int>> memo;

vector<int> f(vector<int> &a) {
  int n = a.size();
  // for (int i = 0; i < n; i++) {
  //   cout << a[i] << " ";
  // }
  // cout << endl;

  vector<int> res;
  if (n == 1) {
    return res;
  }

  if (memo[a].size() > 0 && memo[a][0] < 0) {
    return vector<int>();
  } else if (memo[a].size() > 0) {
    return memo[a];
  }

  for (int i = n - 1; 0 < i; i--) {
    if (i == a[i]) {
      vector<int> aa = a;
      aa.erase(aa.begin() + i);
      res = f(aa);
      if (res.size() == n - 2) {
        res.push_back(i);
        memo[a] = res;
        return res;
      } else {
        break;
      }
    }
  }

  // for (int i = 0; i < res.size(); i++) {
  //   cout << res[i] << " ";
  // }
  // cout << endl;

  memo[a] = vector<int>{-1};
  return vector<int>();
}

int main(void) {
  int n;
  cin >> n;

  vector<int> b(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
  }

  vector<int> res = f(b);

  if (res.size() == n) {
    for (int i = 0; i < n; i++) {
      cout << res[i] << endl;
    }
  } else {
    cout << -1 << endl;
  }

  // for (auto a:memo) {
  //   for (int i = 0; i < a.first.size(); i++) {
  //     cout << a.first[i] << " ";
  //   }
  //   cout << endl;
  //   for (int i = 0; i < a.second.size(); i++) {
  //     cout << a.second[i] << " ";
  //   }
  //   cout << endl;
  // }

  return 0;
}
Problem p03089 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
vector<int> v;
int n, x, i;
int main() {
  cin >> n;
  for (i = 0; ++i <= n;) {
    cin >> x;
    if (x > i)
      return puts("-1");
    v.insert(v.begin() + x - 1, x);
  }
  for (i = -1; ++i < n;)
    cout << v[i] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<int> v;
int n, x, i;
int main() {
  cin >> n;
  for (i = 0; ++i <= n;) {
    cin >> x;
    if (x > i)
      return 0 * puts("-1");
    v.insert(v.begin() + x - 1, x);
  }
  for (i = -1; ++i < n;)
    cout << v[i] << endl;
}
Problem p03089 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
void prnt(vector<int> b) {
  for (int i = 0; i < b.size(); i++) {
    cout << b[i] << endl;
  }
  return;
}
void ok(vector<int> &b, vector<int> &ans, int *bo) {
  // prnt(b);
  // cout<<"size="<<b.size()<<endl;
  if (b.size() == 1 && b[0] == 1) {
    // cout<<"hoge\n";
    ans.push_back(1);
    *bo = 1;
    return;
  }
  for (int i = b.size() - 1; i >= 0; i--) {
    if (b[i] == i + 1) {
      // cout<<"ho\n";
      b.erase(b.begin() + i);
      ans.push_back(i + 1);
      ok(b, ans, bo);
      if (*bo == 1)
        return;
      ans.pop_back();
      b.insert(b.begin() + i, i + 1);
    }
  }
  return;
}

int main() {
  int n;
  cin >> n;
  vector<int> b(n, 0);
  vector<int> ans;
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  int bo = 0;
  ok(b, ans, &bo);
  // cout<<"bo="<<bo<<endl;
  if (bo == 1) {
    for (int i = 0; i < ans.size(); i++) {
      cout << ans[ans.size() - i - 1] << endl;
    }
  } else {
    cout << -1 << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
void prnt(vector<int> b) {
  for (int i = 0; i < b.size(); i++) {
    cout << b[i] << endl;
  }
  return;
}
void ok(vector<int> &b, vector<int> &ans, int *bo) {
  // prnt(b);
  // cout<<"size="<<b.size()<<endl;
  if (b.size() == 1 && b[0] == 1) {
    // cout<<"hoge\n";
    ans.push_back(1);
    *bo = 1;
    return;
  }
  for (int i = b.size() - 1; i >= 0; i--) {
    if (b[i] == i + 1) {
      // cout<<"ho\n";
      b.erase(b.begin() + i);
      ans.push_back(i + 1);
      ok(b, ans, bo);
      if (*bo == 1)
        return;
      ans.pop_back();
      b.insert(b.begin() + i, i + 1);
      break;
    }
  }
  return;
}

int main() {
  int n;
  cin >> n;
  vector<int> b(n, 0);
  vector<int> ans;
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  int bo = 0;
  ok(b, ans, &bo);
  // cout<<"bo="<<bo<<endl;
  if (bo == 1) {
    for (int i = 0; i < ans.size(); i++) {
      cout << ans[ans.size() - i - 1] << endl;
    }
  } else {
    cout << -1 << endl;
  }
  return 0;
}
Problem p03089 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> b(N);
  vector<int> ans(N);
  ans[0] = 1;
  for (int i = 0; i < N; i++) {
    cin >> b[i];
  }
  int j = N - 1;
  for (int i = 0; i < N; i++) {
    if (b[i] > i + 1) {
      cout << -1;
      return 0;
    }
  }
  while (b.size() != 1) {
    for (int i = N - 1; i > -1; i--) {
      if (b[i] == i + 1) {
        ans[j] = b[i];
        j--;
        b.erase(b.begin() + i);
        break;
      }
    }
  }
  for (int i = 0; i < N; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> b(N);
  vector<int> ans(N);
  ans[0] = 1;
  for (int i = 0; i < N; i++) {
    cin >> b[i];
  }
  int j = N - 1;
  for (int i = 0; i < N; i++) {
    if (b[i] > i + 1) {
      cout << -1;
      return 0;
    }
  }
  while (b.size() != 1) {
    for (int i = b.size() - 1; i > -1; i--) {
      if (b[i] == i + 1) {
        ans[j] = b[i];
        j--;
        b.erase(b.begin() + i);
        break;
      }
    }
  }
  for (int i = 0; i < N; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}