Problem p03638 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, start, end) for (long long i = start; i < end; ++i)
#define repreverse(i, start, end) for (long long i = start; i >= end; --i)
#define all(x) (x).begin(), (x).end()
#define len(x) ((long long)(x).size())
#define g(a, b) __gcd((a), (b))
#define l(a, b) ((a) / __gcd((a), (b)) * (b))
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vllvll = vector<vll>;
using pll = pair<ll, ll>;
template <class T> void print1d(T x, ll n = -1) {
  if (n == -1)
    n = x.size();
  rep(i, 0, n) { cout << x[i] << ' '; }
  cout << '\n';
}
template <class T> void print2d(T x, ll r = -1, ll c = -1) {
  if (r == -1)
    r = x.size();
  if (c == -1)
    c = x[0].size();
  rep(i, 0, r) print1d(x[i], c);
}
template <class T, class U> bool haskey(T mp, U key) {
  return mp.find(key) != mp.end();
}
template <class T, class U> bool isin(T el, U container) {
  return find(all(container), el) != container.end();
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> ld deg2rad(T deg) { return M_PI * deg / 180.0; }
template <class T> ld rad2deg(T rad) { return 180.0 * rad / M_PI; }
const long double pi = M_PI;
const long long big = 1LL << 50;
const long long inf = 1LL << 60;
const long long mod = 1e9 + 7;

int main() {
  ll H, W;
  cin >> H >> W;
  ll N;
  cin >> N;
  vll a(N);
  rep(i, 0, N) cin >> a[i];
  queue<ll> q;
  rep(i, 0, N) {
    rep(j, 0, a[i]) { q.push(i + 1); }
  }

  vllvll c(1000, vll(1000));

  rep(i, 0, H) {
    rep(j, 0, W) {
      c[i][j] = q.front();
      q.pop();
    }
    ++i;
    if (i > N - 1)
      break;
    repreverse(j, W - 1, 0) {
      c[i][j] = q.front();
      q.pop();
    }
  }
  rep(i, 0, H) {
    rep(j, 0, W) { cout << c[i][j] << ' '; }
    cout << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, start, end) for (long long i = start; i < end; ++i)
#define repreverse(i, start, end) for (long long i = start; i >= end; --i)
#define all(x) (x).begin(), (x).end()
#define len(x) ((long long)(x).size())
#define g(a, b) __gcd((a), (b))
#define l(a, b) ((a) / __gcd((a), (b)) * (b))
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vllvll = vector<vll>;
using pll = pair<ll, ll>;
template <class T> void print1d(T x, ll n = -1) {
  if (n == -1)
    n = x.size();
  rep(i, 0, n) { cout << x[i] << ' '; }
  cout << '\n';
}
template <class T> void print2d(T x, ll r = -1, ll c = -1) {
  if (r == -1)
    r = x.size();
  if (c == -1)
    c = x[0].size();
  rep(i, 0, r) print1d(x[i], c);
}
template <class T, class U> bool haskey(T mp, U key) {
  return mp.find(key) != mp.end();
}
template <class T, class U> bool isin(T el, U container) {
  return find(all(container), el) != container.end();
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> ld deg2rad(T deg) { return M_PI * deg / 180.0; }
template <class T> ld rad2deg(T rad) { return 180.0 * rad / M_PI; }
const long double pi = M_PI;
const long long big = 1LL << 50;
const long long inf = 1LL << 60;
const long long mod = 1e9 + 7;

int main() {
  ll H, W;
  cin >> H >> W;
  ll N;
  cin >> N;
  vll a(N);
  rep(i, 0, N) cin >> a[i];
  queue<ll> q;
  rep(i, 0, N) {
    rep(j, 0, a[i]) { q.push(i + 1); }
  }

  vllvll c(1000, vll(1000));

  rep(i, 0, H) {
    rep(j, 0, W) {
      c[i][j] = q.front();
      q.pop();
    }
    ++i;
    if (i > H - 1)
      break;
    repreverse(j, W - 1, 0) {
      c[i][j] = q.front();
      q.pop();
    }
  }
  rep(i, 0, H) {
    rep(j, 0, W) { cout << c[i][j] << ' '; }
    cout << endl;
  }
}