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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define rrep(i, a, b) for (int i = (b); i-- > (a);)
#define all(v) (v).begin(), (v).end()
#define trav(x, v) for (auto &x : v)
#define sz(v) int(v.size())
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);

  int k;
  cin >> k;
  vi a(k);
  trav(x, a) cin >> x;

  int mn = 1000, mx = 1;
  trav(x, a) mn = min(mn, x);
  trav(x, a) mx = max(mx, x);

  if (mn * 2 < mx) {
    cout << -1 << endl;
    return 0;
  }

  int len = 0;
  trav(x, a) len += x;
  vi ans(len, -1);

  vi cnt(k, 0);
  vector<bool> happy(k, true);
  vi prev(k, -1);

  rep(i, 0, len) {
    mx = 0, mn = 1000;
    trav(x, a) mx = max(mx, x);
    trav(x, a) mn = min(mn, x);
    int imx = 1000;
    rep(d, 0, k) if (a[d] == mn) imx = min(imx, prev[d]);
    rep(d, 0, k) if (happy[d] && a[d] * 2 > mx &&
                     !(mx >= 2 * mn + 1 && prev[d] > imx)) {
      ans[i] = d;
      break;
    }
    if (ans[i] < 0) {
      rep(j, 0, i) cout << 1 + ans[j] << " ";
      cout << endl;
      trav(x, a) cout << x << " ";
      cout << endl;

      cout << mx << " " << mn << endl;
      cout << a[ans[i]] << endl;
      cout << imx << " " << prev[ans[i]] << endl;
      assert(false);
    }
    prev[ans[i]] = i;
    happy[ans[i]] = false;
    --a[ans[i]];
    ++cnt[ans[i]];
    if (i >= k)
      --cnt[ans[i - k]];
    if (count(all(cnt), 0) == 0) {
      rep(d, 0, k) happy[d] = true;
    }
  }

  trav(x, ans) cout << x + 1 << " ";
  cout << endl;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define rrep(i, a, b) for (int i = (b); i-- > (a);)
#define all(v) (v).begin(), (v).end()
#define trav(x, v) for (auto &x : v)
#define sz(v) int(v.size())
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);

  int k;
  cin >> k;
  vi a(k);
  trav(x, a) cin >> x;

  int mn = 1000, mx = 1;
  trav(x, a) mn = min(mn, x);
  trav(x, a) mx = max(mx, x);

  if (mn * 2 < mx) {
    cout << -1 << endl;
    return 0;
  }

  int len = 0;
  trav(x, a) len += x;
  vi ans(len, -1);

  vi cnt(k, 0);
  vector<bool> happy(k, true);
  vi prev(k, -1);

  rep(i, 0, len) {
    mx = 0, mn = 1000;
    trav(x, a) mx = max(mx, x);
    trav(x, a) mn = min(mn, x);
    int imx = 1000;
    rep(d, 0, k) if (a[d] == mn) imx = min(imx, prev[d]);
    rep(d, 0, k) if (happy[d] && a[d] * 2 > mx &&
                     !(mx = 2 * mn + 1 && a[d] < mx && prev[d] > imx)) {
      ans[i] = d;
      break;
    }
    if (ans[i] < 0) {
      rep(j, 0, i) cout << 1 + ans[j] << " ";
      cout << endl;
      trav(x, a) cout << x << " ";
      cout << endl;

      cout << mx << " " << mn << endl;
      cout << a[ans[i]] << endl;
      cout << imx << " " << prev[ans[i]] << endl;
      assert(false);
    }
    prev[ans[i]] = i;
    happy[ans[i]] = false;
    --a[ans[i]];
    ++cnt[ans[i]];
    if (i >= k)
      --cnt[ans[i - k]];
    if (count(all(cnt), 0) == 0) {
      rep(d, 0, k) happy[d] = true;
    }
  }

  trav(x, ans) cout << x + 1 << " ";
  cout << endl;
}