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

#include <bits/stdc++.h>
using namespace std;
#define repn(i, n) for (int i = 1; i <= n; i++)

int n, k, a[105], b[105], ans[1005], mn = 100000, mx = -100000;
int main() {
  cin >> k;
  repn(i, k) {
    cin >> a[i];
    n += a[i];
    mn = min(mn, a[i]);
    mx = max(mx, a[i]);
  }
  if (2 * mn < mx) {
    cout << -1 << '\\n';
    return 0;
  }
  int las = 0;
  repn(id, n) {
    repn(i, k) {
      if (a[i] == 0)
        continue;
      bool OK = 0;
      for (int add = 0; add < k; add++) {
        if (n - id < add || add + id - las > k)
          break;
        if (add + id < k)
          continue;
        memcpy(b, a, sizeof(a));
        b[i]--;
        bool ex[105] = {};
        for (int x = id + add - k + 1; x < id; x++)
          ex[ans[x]] = 1;
        ex[i] = 1;
        int cnt = 0;
        repn(x, k) {
          if (!ex[x]) {
            cnt++;
            if (a[x] == 0)
              cnt++;
            b[x]--;
          }
        }
        if (cnt != add)
          continue;
        if (n - id - add <= k) {
          bool bad = 0;
          repn(x, k) {
            if (b[x] > 1)
              bad = 1;
          }
          if (!bad) {
            repn(x, k) {
              if (!ex[x]) {
                if (b[x] == 0)
                  b[x]++;
                else
                  bad = 1;
              }
            }
          }
          if (!bad) {
            for (int x = id; x >= 1; x--) {
              int v = (x == id ? i : ans[x]);
              if (b[v] >= 1)
                break;
              else
                b[v]++;
            }
          }
          bad = 0;
          repn(x, k) if (b[x] != 1) bad = 1;
          if (!bad) {
            OK = 1;
            break;
          }
        } else {
          mn = 100000, mx = -100000;
          repn(x, k) {
            mn = min(mn, b[x]);
            mx = max(mx, b[x]);
          }
          if (2 * mn >= mx) {
            OK = 1;
            break;
          }
          if (2 * mn + 1 < mx) {
            continue;
          }
          bool need[105] = {};
          repn(x, k) {
            if (b[x] == mx)
              need[x] = 1;
          }
          bool bad = 0;
          repn(x, k) {
            if (!ex[x]) {
              if (need[x] == 1)
                bad = 1;
              need[x] = 1;
            }
          }
          if (!bad) {
            for (int x = id; x >= 1; x--) {
              int v = (x == id ? i : ans[x]);
              if (need[v])
                break;
              need[v] = 1;
            }
          }
          bad = 0;
          repn(x, k) {
            if (!need[x] && mn == b[x])
              bad = 1;
          }
          if (!bad) {
            OK = 1;
            break;
          }
        }
      }
      if (OK) {
        ans[id] = i;
        if (id >= k) {
          unordered_set<int> S;
          for (int i = id - k + 1; i <= id; i++)
            S.insert(ans[i]);
          if (S.size() == k)
            las = id;
        }
        a[i]--;
        break;
      }
    }
    cout << ans[id] << ' ';
  }
  cout << '\\n';
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define repn(i, n) for (int i = 1; i <= n; i++)

int n, k, a[105], b[105], ans[1005], mn = 100000, mx = -100000;
int main() {
  cin >> k;
  repn(i, k) {
    cin >> a[i];
    n += a[i];
    mn = min(mn, a[i]);
    mx = max(mx, a[i]);
  }
  if (2 * mn < mx) {
    cout << -1 << '\\n';
    return 0;
  }
  int las = 0;
  repn(id, n) {
    repn(i, k) {
      if (a[i] == 0)
        continue;
      bool OK = 0;
      for (int add = 0;; add++) {
        if (n - id < add || add + id - las > k)
          break;
        memcpy(b, a, sizeof(a));
        b[i]--;
        bool ex[105] = {};
        for (int x = id + add - k + 1; x < id; x++)
          ex[ans[x]] = 1;
        ex[i] = 1;
        int cnt = 0;
        repn(x, k) {
          if (!ex[x]) {
            cnt++;
            if (a[x] == 0)
              cnt++;
            b[x]--;
          }
        }
        if (cnt != add)
          continue;
        if (n - id - add <= k) {
          bool bad = 0;
          repn(x, k) {
            if (b[x] > 1)
              bad = 1;
          }
          if (!bad) {
            repn(x, k) {
              if (!ex[x]) {
                if (b[x] == 0)
                  b[x]++;
                else
                  bad = 1;
              }
            }
          }
          if (!bad) {
            for (int x = id; x >= 1; x--) {
              int v = (x == id ? i : ans[x]);
              if (b[v] >= 1)
                break;
              else
                b[v]++;
            }
          }
          bad = 0;
          repn(x, k) if (b[x] != 1) bad = 1;
          if (!bad) {
            OK = 1;
            break;
          }
        } else {
          mn = 100000, mx = -100000;
          repn(x, k) {
            mn = min(mn, b[x]);
            mx = max(mx, b[x]);
          }
          if (2 * mn >= mx) {
            OK = 1;
            break;
          }
          if (2 * mn + 1 < mx) {
            continue;
          }
          bool need[105] = {};
          repn(x, k) {
            if (b[x] == mx)
              need[x] = 1;
          }
          bool bad = 0;
          repn(x, k) {
            if (!ex[x]) {
              if (need[x] == 1)
                bad = 1;
              need[x] = 1;
            }
          }
          if (!bad) {
            for (int x = id; x >= 1; x--) {
              int v = (x == id ? i : ans[x]);
              if (need[v])
                break;
              need[v] = 1;
            }
          }
          bad = 0;
          repn(x, k) {
            if (!need[x] && mn == b[x])
              bad = 1;
          }
          if (!bad) {
            OK = 1;
            break;
          }
        }
      }
      if (OK) {
        ans[id] = i;
        if (id >= k) {
          unordered_set<int> S;
          for (int i = id - k + 1; i <= id; i++)
            S.insert(ans[i]);
          if (S.size() == k)
            las = id;
        }
        a[i]--;
        break;
      }
    }
    cout << ans[id] << ' ';
  }
  cout << '\\n';
  return 0;
}