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

#include <bits/stdc++.h>
using namespace std;
const int K = 110, N = 1010;

int a[K], n, k;
int ret[N], curlen;

inline bool check() {
  int mn = a[1], mx = a[1];
  for (int i = 1; i <= k; i++)
    mx = max(mx, a[i]), mn = min(mn, a[i]);
  //	cerr << mn << " " << mx << "???" << endl;
  return mn * 2 >= mx;
}
int vis[K];

inline void merge(int *x, int *y, int *z, int len1, int len2) {
  int i = 1, j = 1;
  int l = 0;
  while (i <= len1 && j <= len2) {
    if (x[i] < y[j])
      z[++l] = x[i++];
    else
      z[++l] = y[j++];
  }
  while (i <= len1)
    z[++l] = x[i++];
  while (j <= len2)
    z[++l] = y[j++];
}

int b[K], x[K], y[K], z[K], l1, l2, l3;

vector<int> solve_ext(int len) {
  l1 = l2 = l3 = 0;
  for (int i = 1; i <= k; i++)
    vis[i] = 0, b[i] = a[i];
  for (int i = 0; i < k - len; i++) {
    vis[ret[curlen - i]] = curlen - i;
  }
  for (int i = 1; i <= k; i++) {
    if (!vis[i])
      b[i]--;
    if (b[i] < 0)
      return vector<int>(1, k + 1);
  }
  int mn, mx;
  mn = mx = b[1];
  for (int i = 1; i <= k; i++) {
    mn = min(mn, b[i]);
    mx = max(mx, b[i]);
  }
  if (mn * 2 >= mx) {
    vector<int> ret;
    for (int i = 1; i <= k; i++)
      if (!vis[i])
        ret.push_back(i);
    return ret;
  } else if (mn * 2 + 1 == mx) {
    vector<int> res;
    for (int i = 1; i <= k; i++)
      if (!vis[i] && b[i] == mx)
        x[++l1] = i;
    for (int i = 1; i <= k; i++)
      if (!vis[i] && b[i] == mn)
        x[++l1] = i;
    for (int i = 1; i <= k; i++)
      if (!vis[i] && b[i] != mx && b[i] != mn)
        y[++l2] = i;
    merge(x, y, z, l1, l2);
    l3 = l1 + l2;
    res = vector<int>(z + 1, z + l3 + 1);
    int p1 = 0, p2 = 0;
    for (int i = curlen - (k - len - 1); i <= curlen; i++) {
      //			if(len==29)cerr << ret[curlen] << ":" << k-len-1
      //<< " " << i << ":" << ret[i] << endl;
      if (b[ret[i]] == mx)
        p1 = i;
      if (b[ret[i]] == mn && !p2)
        p2 = i;
    }
    for (int i = 1; i <= l3; i++) {
      if (b[z[i]] == mx)
        p1 = i + curlen;
      if (b[z[i]] == mn && !p2)
        p2 = i + curlen;
    }
    if (p1 < p2)
      return res;
    else
      return vector<int>(1, k + 1);
  } else
    return vector<int>(1, k + 1);
}

bool cmp(vector<int> a, vector<int> b) {
  for (size_t i = 0; i < a.size(); i++) {
    if (i >= b.size())
      return 1;
    if (a[i] > b[i])
      return 1;
    if (a[i] < b[i])
      return 0;
  }
  return 0;
}

inline void extend() {
  vector<int> ext(1, k + 1);
  //	cerr << ret[curlen] << "!!!" ;
  for (int extlen = 1; extlen <= k && curlen + extlen <= n; extlen++) {
    int d = k - extlen;
    if (curlen - d + 1 >= 1) {
      //			if(extlen==29)cerr << ret[curlen] << "!" <<
      //endl;
      vector<int> nw = solve_ext(extlen);
      //			if(extlen==29){cerr << extlen << "::";for(size_t
      //i=0;i<nw.size();i++)cerr << nw[i] << " ";cerr << endl;}
      if (cmp(ext, nw))
        ext = nw;
    }
  }
  for (size_t i = 0; i < ext.size(); i++) {
    printf("%d ", ext[i]);
    ret[++curlen] = ext[i];
    a[ext[i]]--;
  }
  return;
}

int main() {
  cin >> k;
  for (int i = 1; i <= k; ++i)
    scanf("%d", &a[i]), n += a[i];
  if (!check()) {
    puts("-1");
    return 0;
  }
  while (curlen < n) {
    extend();
  }
  puts("");
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int K = 110, N = 1010;

int a[K], n, k;
int ret[N], curlen;

inline bool check() {
  int mn = a[1], mx = a[1];
  for (int i = 1; i <= k; i++)
    mx = max(mx, a[i]), mn = min(mn, a[i]);
  //	cerr << mn << " " << mx << "???" << endl;
  return mn * 2 >= mx;
}
int vis[K];

inline void merge(int *x, int *y, int *z, int len1, int len2) {
  int i = 1, j = 1;
  int l = 0;
  while (i <= len1 && j <= len2) {
    if (x[i] < y[j])
      z[++l] = x[i++];
    else
      z[++l] = y[j++];
  }
  while (i <= len1)
    z[++l] = x[i++];
  while (j <= len2)
    z[++l] = y[j++];
}

int b[K], x[K], y[K], z[K], l1, l2, l3;

vector<int> solve_ext(int len) {
  l1 = l2 = l3 = 0;
  for (int i = 1; i <= k; i++)
    vis[i] = 0, b[i] = a[i];
  for (int i = 0; i < k - len; i++) {
    vis[ret[curlen - i]] = curlen - i;
  }
  for (int i = 1; i <= k; i++) {
    if (!vis[i])
      b[i]--;
    if (b[i] < 0)
      return vector<int>(1, k + 1);
  }
  int mn, mx;
  mn = mx = b[1];
  for (int i = 1; i <= k; i++) {
    mn = min(mn, b[i]);
    mx = max(mx, b[i]);
  }
  if (mn * 2 >= mx) {
    vector<int> ret;
    for (int i = 1; i <= k; i++)
      if (!vis[i])
        ret.push_back(i);
    return ret;
  }

  if (mn * 2 + 1 == mx) {
    vector<int> ret;
    for (int i = 1; i <= k; i++)
      if (!vis[i] && b[i] == mx)
        x[++l1] = i;
    for (int i = 1; i <= k; i++)
      if (!vis[i] && b[i] == mn)
        x[++l1] = i;
    for (int i = 1; i <= k; i++)
      if (!vis[i] && b[i] != mx && b[i] != mn)
        y[++l2] = i;
    merge(x, y, z, l1, l2);
    l3 = l1 + l2;
    ret = vector<int>(z + 1, z + l3 + 1);
    int p1 = 0, p2 = 0;
    for (int i = curlen - (k - len - 1); i <= curlen; i++) {
      if (b[ret[i]] == mx)
        p1 = i;
      if (b[ret[i]] == mn && !p2)
        p2 = i;
    }
    for (int i = 1; i <= l3; i++) {
      if (b[z[i]] == mx)
        p1 = i + curlen;
      if (b[z[i]] == mn && !p2)
        p2 = i + curlen;
    }
    //		for(int i=1;i<=l3;i++){
    //			cerr << z[i] << " ";
    //		}
    //		puts("");
    if (p1 < p2)
      return ret;
    else
      return vector<int>(1, k + 1);
  } else
    return vector<int>(1, k + 1);
}

bool cmp(vector<int> a, vector<int> b) {
  for (size_t i = 0; i < a.size(); i++) {
    if (i >= b.size())
      return 1;
    if (a[i] > b[i])
      return 1;
    if (a[i] < b[i])
      return 0;
  }
  return 0;
}

inline void extend() {
  vector<int> ext(1, k + 1);
  for (int extlen = 1; extlen <= k; extlen++) {
    int d = k - extlen;
    if (curlen - d + 1 >= 1) {
      vector<int> nw = solve_ext(extlen);
      //			cerr << extlen << "::";for(size_t
      //i=0;i<nw.size();i++)cerr << nw[i] << " ";puts("");
      if (cmp(ext, nw))
        ext = nw;
    }
  }
  for (size_t i = 0; i < ext.size(); i++) {
    printf("%d ", ext[i]);
    //		cout << ext[i] << " ";
    ret[++curlen] = ext[i];
    a[ext[i]]--;
  }
  //	puts("");
  return;
}

int main() {
  cin >> k;
  for (int i = 1; i <= k; ++i)
    scanf("%d", &a[i]), n += a[i];
  if (!check()) {
    puts("-1");
    return 0;
  }
  while (curlen < n) {
    extend();
  }
  puts("");
}