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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define MOD 1000000007
#define MOD2 998244353
#define PRIM 3
#define INF (1 << 29)
#define LINF (1LL << 60)
#define EPS (1e-10)
#define PI 3.1415926535897932384626433832795028
typedef long long Int;
typedef pair<Int, Int> P;
typedef long double Real;
typedef complex<Real> CP;

Int n;
vector<Int> a;
vector<Int> ans;
Int mx, mn;
set<int> maxs, mins;
bool ok[1100];

Int min_initial(Int from, Int to) {
  auto aa = a;
  vector<int> used(n, 0);
  for (int i = from; i < to; i++) {
    if (used[ans[i]])
      return n;
    used[ans[i]] = 1;
    aa[ans[i]]++;
  }

  Int tmpmx = *max_element(aa.begin(), aa.end());
  Int tmpmn = *min_element(aa.begin(), aa.end());
  if (tmpmn * 2 < tmpmx)
    return n;
  if (tmpmn * 2 > tmpmx) {
    for (int i = 0; i < n; i++)
      if (!used[i])
        return i;
    return n;
  }
  Int max_cnt = 0;
  for (int i = 0; i < n; i++) {
    if (aa[i] == tmpmx)
      max_cnt++;
  }

  for (int i = from; i < to; i++) {
    if (aa[ans[i]] == tmpmx)
      max_cnt--;
    if (aa[ans[i]] == tmpmn) {
      if (max_cnt != 0)
        return n;
    }
  }

  for (int i = 0; i < n; i++) {
    if (aa[i] == 0)
      continue;
    if (used[i])
      continue;
    if (max_cnt == 0 && !used[i])
      return i;
    if (max_cnt > 0 && aa[i] != tmpmn && !used[i])
      return i;
  }
  return ans[from];
}

int main() {
  Int as = 0;
  cin >> n;
  a.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    as += a[i];
  }

  Int mx = *max_element(a.begin(), a.end());
  Int mn = *min_element(a.begin(), a.end());
  if (mn * 2 < mx) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = 0; i < as; i++) {
    Int nxt = n;
    for (int j = max(0ll, i - n + 1); j <= i; j++) {
      nxt = min(nxt, min_initial(j, i));
      if (!ok[j])
        break;
    }

    a[nxt]--;
    ans.push_back(nxt);
    if (ans.size() >= n) {
      set<int> hoge;
      for (int j = 0; j < n; j++) {
        hoge.insert(ans[ans.size() - 1 - j]);
      }
      if (hoge.size() == n) {
        for (int j = 0; j < n; j++)
          ok[ans.size() - 1 - j] = true;
      }
    }
  }

  for (auto elem : ans)
    cout << elem + 1 << " ";
  cout << endl;

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define MOD 1000000007
#define MOD2 998244353
#define PRIM 3
#define INF (1 << 29)
#define LINF (1LL << 60)
#define EPS (1e-10)
#define PI 3.1415926535897932384626433832795028
typedef long long Int;
typedef pair<Int, Int> P;
typedef long double Real;
typedef complex<Real> CP;

Int n;
vector<Int> a;
vector<Int> ans;
Int mx, mn;
set<int> maxs, mins;
bool ok[1100];

Int min_initial(Int from, Int to) {
  auto aa = a;
  vector<int> used(n, 0);
  for (int i = from; i < to; i++) {
    if (used[ans[i]])
      return n;
    used[ans[i]] = 1;
    aa[ans[i]]++;
  }

  Int tmpmx = *max_element(aa.begin(), aa.end());
  Int tmpmn = *min_element(aa.begin(), aa.end());
  if (tmpmn * 2 < tmpmx)
    return n;
  if (tmpmn * 2 > tmpmx) {
    for (int i = 0; i < n; i++)
      if (!used[i])
        return i;
    return n;
  }
  Int max_cnt = 0;
  for (int i = 0; i < n; i++) {
    if (aa[i] == tmpmx)
      max_cnt++;
  }

  for (int i = from; i < to; i++) {
    if (aa[ans[i]] == tmpmx)
      max_cnt--;
    if (aa[ans[i]] == tmpmn) {
      if (max_cnt != 0)
        return n;
    }
  }

  for (int i = 0; i < n; i++) {
    if (aa[i] == 0)
      continue;
    if (used[i])
      continue;
    if (max_cnt == 0 && !used[i])
      return i;
    if (max_cnt > 0 && aa[i] != tmpmn && !used[i])
      return i;
  }
  return ans[from];
}

int main() {
  Int as = 0;
  cin >> n;
  a.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    as += a[i];
  }

  Int mx = *max_element(a.begin(), a.end());
  Int mn = *max_element(a.begin(), a.end());

  if (mn * 2 < mx) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = 0; i < as; i++) {
    Int nxt = n;
    for (int j = max(0ll, i - n + 1); j <= i; j++) {
      nxt = min(nxt, min_initial(j, i));
      if (!ok[j])
        break;
    }

    a[nxt]--;
    ans.push_back(nxt);
    if (ans.size() >= n) {
      set<int> hoge;
      for (int j = 0; j < n; j++) {
        hoge.insert(ans[ans.size() - 1 - j]);
      }
      if (hoge.size() == n) {
        for (int j = 0; j < n; j++)
          ok[ans.size() - 1 - j] = true;
      }
    }
  }

  for (auto elem : ans)
    cout << elem + 1 << " ";
  cout << endl;

  return 0;
}