Problem p02637 (C++)

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

int main() {
  ios_base::sync_with_stdio(false), cin.tie(nullptr);
  int k;
  cin >> k;
  vector<int> freq(k);
  for (int &x : freq)
    cin >> x;
  int minv = 1000000, maxv = -1;
  for (int x : freq) {
    minv = min(minv, x);
    maxv = max(maxv, x);
  }
  int sum = 0;
  for (int x : freq)
    sum += x;
  if (minv * 2 < maxv) {
    cout << -1 << '\n';
    return 0;
  }
  vector<int> ans;
  int maxworks = 0;
  while ((int)ans.size() < sum) {
    bool found = false;
    for (int nxt = 0; nxt < k; nxt++) {
      vector<int> cans = ans;
      cans.push_back(nxt);
      int cmaxworks = maxworks;
      {
        if ((int)cans.size() >= k) {
          vector<int> d(k, 0);
          for (int i = 0; i < k; i++) {
            d[cans[(int)cans.size() - 1 - i]]++;
          }
          bool works = true;
          for (int i = 0; i < k; i++)
            if (!d[i])
              works = false;
          if (works)
            cmaxworks = (int)cans.size();
        }
      }
      vector<int> cfreq = freq;
      cfreq[nxt]--;
      if (cfreq[nxt] < 0)
        continue;
      vector<int> known;
      for (int f = cmaxworks; f < (int)cans.size(); f++)
        known.push_back(cans[f]);
      vector<int> zk = known;
      sort(zk.begin(), zk.end());
      if (unique(zk.begin(), zk.end()) != zk.end())
        continue;
      bool ok = false;
      {
        vector<int> dfreq = cfreq;
        for (int i = 0; i < k; i++)
          dfreq[i]--;
        multiset<int> g;
        for (int f : dfreq)
          g.insert(f);
        if (*g.rbegin() <= 2 * *g.begin())
          ok = true;
        for (int a : known) {
          g.erase(g.find(dfreq[a]));
          dfreq[a]++;
          g.insert(dfreq[a]);
          if (*g.rbegin() <= 2 * *g.begin())
            ok = true;
        }
      }
      {
        vector<int> dfreq = cfreq;
        for (int i = 0; i < k; i++)
          dfreq[i]--;
        for (int a : known) {
          dfreq[a]++;
        }
        multiset<int> g;
        for (int f : dfreq)
          g.insert(f);
        vector<int> cur(k, 0);
        for (int a : known)
          cur[a] = 1;
        for (int b = cmaxworks - 1; b >= 0; b--) {
          if (cur[cans[b]])
            break;
          cur[cans[b]] = 1;
          int a = cans[b];
          g.erase(g.find(dfreq[a]));
          dfreq[a]++;
          g.insert(dfreq[a]);
          if (*g.rbegin() <= 2 * *g.begin())
            ok = true;
        }
      }
      {
        vector<int> dfreq = cfreq;
        for (int i = 0; i < k; i++)
          dfreq[i]--;
        vector<int> cur(k, 0);
        for (int a : known)
          cur[a] = 1;
        int minval = 1000000;
        for (int b : dfreq)
          minval = min(minval, b);
        for (int i = 0; i < k; i++) {
          if (cur[i])
            continue;
          if (dfreq[i] > 2 * minval) {
            dfreq[i]--;
            cur[i]--;
          }
        }
        int ming = 100000, maxg = 0;
        for (int b : dfreq)
          ming = min(ming, b);
        for (int b : dfreq)
          maxg = max(maxg, b);
        if (ming * 2 >= maxg) {
          ok = true;
        }
      }
      if (ok) {
        ans.push_back(nxt);
        maxworks = cmaxworks;
        found = true;
        freq[nxt]--;
        break;
      }
    }
    // for(int b : ans) cerr << b << ' ';
    // cerr << '\n';
    assert(found);
  }
  for (int b : ans)
    cout << b + 1 << ' ';
  cout << '\n';
}

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

int main() {
  ios_base::sync_with_stdio(false), cin.tie(nullptr);
  int k;
  cin >> k;
  vector<int> freq(k);
  for (int &x : freq)
    cin >> x;
  int minv = 1000000, maxv = -1;
  for (int x : freq) {
    minv = min(minv, x);
    maxv = max(maxv, x);
  }
  int sum = 0;
  for (int x : freq)
    sum += x;
  if (minv * 2 < maxv) {
    cout << -1 << '\n';
    return 0;
  }
  vector<int> ans;
  int maxworks = 0;
  while ((int)ans.size() < sum) {
    bool found = false;
    for (int nxt = 0; nxt < k; nxt++) {
      vector<int> cans = ans;
      cans.push_back(nxt);
      int cmaxworks = maxworks;
      {
        if ((int)cans.size() >= k) {
          vector<int> d(k, 0);
          for (int i = 0; i < k; i++) {
            d[cans[(int)cans.size() - 1 - i]]++;
          }
          bool works = true;
          for (int i = 0; i < k; i++)
            if (!d[i])
              works = false;
          if (works)
            cmaxworks = (int)cans.size();
        }
      }
      vector<int> cfreq = freq;
      cfreq[nxt]--;
      if (cfreq[nxt] < 0)
        continue;
      vector<int> known;
      for (int f = cmaxworks; f < (int)cans.size(); f++)
        known.push_back(cans[f]);
      vector<int> zk = known;
      sort(zk.begin(), zk.end());
      if (unique(zk.begin(), zk.end()) != zk.end())
        continue;
      bool ok = false;
      {
        vector<int> dfreq = cfreq;
        for (int i = 0; i < k; i++)
          dfreq[i]--;
        multiset<int> g;
        for (int f : dfreq)
          g.insert(f);
        if (*g.rbegin() <= 2 * *g.begin())
          ok = true;
        reverse(known.begin(), known.end());
        for (int a : known) {
          g.erase(g.find(dfreq[a]));
          dfreq[a]++;
          g.insert(dfreq[a]);
          if (*g.rbegin() <= 2 * *g.begin())
            ok = true;
        }
      }
      {
        vector<int> dfreq = cfreq;
        for (int i = 0; i < k; i++)
          dfreq[i]--;
        for (int a : known) {
          dfreq[a]++;
        }
        multiset<int> g;
        for (int f : dfreq)
          g.insert(f);
        vector<int> cur(k, 0);
        for (int a : known)
          cur[a] = 1;
        for (int b = cmaxworks - 1; b >= 0; b--) {
          if (cur[cans[b]])
            break;
          cur[cans[b]] = 1;
          int a = cans[b];
          g.erase(g.find(dfreq[a]));
          dfreq[a]++;
          g.insert(dfreq[a]);
          if (*g.rbegin() <= 2 * *g.begin())
            ok = true;
        }
      }
      {
        vector<int> dfreq = cfreq;
        for (int i = 0; i < k; i++)
          dfreq[i]--;
        vector<int> cur(k, 0);
        for (int a : known)
          cur[a] = 1;
        int minval = 1000000;
        for (int b : dfreq)
          minval = min(minval, b);
        for (int i = 0; i < k; i++) {
          if (cur[i])
            continue;
          if (dfreq[i] > 2 * minval) {
            dfreq[i]--;
            cur[i]--;
          }
        }
        int ming = 100000, maxg = 0;
        for (int b : dfreq)
          ming = min(ming, b);
        for (int b : dfreq)
          maxg = max(maxg, b);
        if (ming * 2 >= maxg) {
          ok = true;
        }
      }
      if (ok) {
        ans.push_back(nxt);
        maxworks = cmaxworks;
        found = true;
        freq[nxt]--;
        break;
      }
    }
    // for(int b : ans) cerr << b << ' ';
    // cerr << '\n';
    assert(found);
  }
  for (int b : ans)
    cout << b + 1 << ' ';
  cout << '\n';
}