Problem p02637 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// gp_hash_table<int, int> mapka;

using namespace std;
#define PB push_back
#define MP make_pair
#define LL long long
#define int LL
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define RE(i, n) FOR(i, 1, n)
#define REP(i, n) FOR(i, 0, (int)(n)-1)
#define R(i, n) REP(i, n)
#define VI vector<int>
#define PII pair<int, int>
#define LD long double
#define FI first
#define SE second
#define st FI
#define nd SE
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())

template <class C> void mini(C &a4, C b4) { a4 = min(a4, b4); }
template <class C> void maxi(C &a4, C b4) { a4 = max(a4, b4); }

template <class TH> void _dbg(const char *sdbg, TH h) {
  cerr << sdbg << '=' << h << endl;
}
template <class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while (*sdbg != ',')
    cerr << *sdbg++;
  cerr << '=' << h << ',';
  _dbg(sdbg + 1, a...);
}

template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[";
  for (auto vv : V)
    os << vv << ",";
  return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.st << "," << P.nd << ")";
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr                                                                   \\
  if (0)                                                                       \\
  cout
#endif

const int MAX = 101;
int n;
vector<int> t;
int dajmi() {
  int mi = 1e9;
  int ma = 0;
  R(i, n) {
    mini(mi, t[i]);
    maxi(ma, t[i]);
  }
  if (ma > mi * 2) {
    return MAX;
  }
  if (ma == mi * 2) {
    R(i, n) {
      if (t[i] == ma) {
        return i;
      }
    }
  }
  return 0;
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(11);
  cerr << fixed << setprecision(6);
  cin >> n;
  t.resize(n);
  R(i, n) cin >> t[i];
  if (dajmi() == MAX) {
    cout << "-1\\n";
    return 0;
  }
  vector<int> res;
  vector<int> nie(n);
  int ilnie = 0;
  while (1) {
    int mi = 1e9;
    int ma = 0;
    R(i, n) {
      mini(mi, t[i]);
      maxi(ma, t[i]);
    }
    debug(ilnie);

    vector<int> zle(n);
    R(i, n) {
      if (t[i] * 2 < ma) {
        zle[i] = 1;
      }
    }
    int naj = -1;
    R(i, min(SZ(res), n)) {
      if (zle[res[SZ(res) - i - 1]]) {
        naj = i;
        zle[res[SZ(res) - i - 1]] = 0;
      }
    }
    R(i, naj + 1) { zle[res[SZ(res) - i - 1]] = 1; }

    if (ma == 0)
      break;
    R(i, n) if (!nie[i] && t[i] * 2 > ma && t[i] && !zle[i]) {
      debug(ma, t[i], i);
      res.PB(i);
      t[i]--;
      nie[i] = 1;
      ilnie++;
      goto ok;
    }
    debug(res);
    assert(0);
  ok:;
    if (SZ(res) >= n) {
      vector<int> cz(n);
      bool nope = 0;
      R(i, n) {
        if (cz[res[SZ(res) - i - 1]]) {
          nope = 1;
          break;
        }
        cz[res[SZ(res) - i - 1]] = 1;
      }
      if (!nope) {
        ilnie = 0;
        R(i, n) nie[i] = 0;
      }
    }
  }
  for (int ak : res) {
    cout << ak + 1 << " ";
  }
  cout << "\\n";
}


Error: WA

Fixed submission:

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// gp_hash_table<int, int> mapka;

using namespace std;
#define PB push_back
#define MP make_pair
#define LL long long
#define int LL
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define RE(i, n) FOR(i, 1, n)
#define REP(i, n) FOR(i, 0, (int)(n)-1)
#define R(i, n) REP(i, n)
#define VI vector<int>
#define PII pair<int, int>
#define LD long double
#define FI first
#define SE second
#define st FI
#define nd SE
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())

template <class C> void mini(C &a4, C b4) { a4 = min(a4, b4); }
template <class C> void maxi(C &a4, C b4) { a4 = max(a4, b4); }

template <class TH> void _dbg(const char *sdbg, TH h) {
  cerr << sdbg << '=' << h << endl;
}
template <class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while (*sdbg != ',')
    cerr << *sdbg++;
  cerr << '=' << h << ',';
  _dbg(sdbg + 1, a...);
}

template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[";
  for (auto vv : V)
    os << vv << ",";
  return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.st << "," << P.nd << ")";
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr                                                                   \\
  if (0)                                                                       \\
  cout
#endif

const int MAX = 101;
int n;
vector<int> t;
int dajmi() {
  int mi = 1e9;
  int ma = 0;
  R(i, n) {
    mini(mi, t[i]);
    maxi(ma, t[i]);
  }
  if (ma > mi * 2) {
    return MAX;
  }
  if (ma == mi * 2) {
    R(i, n) {
      if (t[i] == ma) {
        return i;
      }
    }
  }
  return 0;
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(11);
  cerr << fixed << setprecision(6);
  cin >> n;
  t.resize(n);
  R(i, n) cin >> t[i];
  if (dajmi() == MAX) {
    cout << "-1\\n";
    return 0;
  }
  vector<int> res;
  vector<int> nie(n);
  int ilnie = 0;
  while (1) {
    int mi = 1e9;
    int ma = 0;
    R(i, n) {
      mini(mi, t[i]);
      maxi(ma, t[i]);
    }
    debug(ilnie);
    if (ma == 0)
      break;
    R(i, n) if (!nie[i] && t[i] * 2 > ma && t[i]) {
      debug(ma, t[i], i);
      res.PB(i);
      t[i]--;
      nie[i] = 1;
      ilnie++;
      goto ok;
    }
    debug(res);
    assert(0);
  ok:;
    if (SZ(res) >= n) {
      vector<int> cz(n);
      bool nope = 0;
      R(i, n) {
        if (cz[res[SZ(res) - i - 1]]) {
          nope = 1;
          break;
        }
        cz[res[SZ(res) - i - 1]] = 1;
      }
      if (!nope) {
        ilnie = 0;
        R(i, n) nie[i] = 0;
      }
    }
  }
  for (int ak : res) {
    cout << ak + 1 << " ";
  }
  cout << "\\n";
}