Problem p03638 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
#define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repr(i, 0, n)
#define reprrev(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define reprev(i, n) reprrev(i, 0, n)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define mp make_pair
#define mt make_tuple
#define MOD 1000000007
#define PI 3.1415926535
typedef long long ll;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<Pi> vPi;
typedef vector<Pll> vPll;
typedef vector<vector<int>> vvi;
typedef vector<vector<bool>> vvb;
typedef vector<vector<ll>> vvll;
typedef vector<vector<char>> vvc;
typedef vector<vector<string>> vvs;
typedef vector<vector<double>> vvd;
typedef vector<vector<Pi>> vvPi;
typedef priority_queue<int, vector<int>, greater<int>> pqli;
typedef priority_queue<ll, vector<ll>, greater<ll>> pqlll;
typedef priority_queue<Pi, vector<Pi>, greater<Pi>> pqlP;
string YN(bool y) { return (y ? "YES" : "NO"); }
string yn(bool y) { return (y ? "Yes" : "No"); }
string ON(bool y) { return (y ? "OK" : "NG"); }
int main(void) {
  int h, w, n;

  cin >> h >> w >> n;

  vi a(n);

  vvi c(h, vi(n));

  rep(i, n) cin >> a[i];

  int k = 0;
  rep(i, h) {
    if (i % 2) {
      reprev(j, w) {
        if (a[k] == 0)
          k++;
        c[i][j] = k + 1;
        a[k]--;
      }
    } else {
      rep(j, w) {
        if (a[k] == 0)
          k++;
        c[i][j] = k + 1;
        a[k]--;
      }
    }
  }
  rep(i, h) {
    rep(j, w) {
      cout << c[i][j];
      if (j != w - 1)
        cout << " ";
    }
    cout << endl;
  }
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repr(i, 0, n)
#define reprrev(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define reprev(i, n) reprrev(i, 0, n)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define mp make_pair
#define mt make_tuple
#define MOD 1000000007
#define PI 3.1415926535
typedef long long ll;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<Pi> vPi;
typedef vector<Pll> vPll;
typedef vector<vector<int>> vvi;
typedef vector<vector<bool>> vvb;
typedef vector<vector<ll>> vvll;
typedef vector<vector<char>> vvc;
typedef vector<vector<string>> vvs;
typedef vector<vector<double>> vvd;
typedef vector<vector<Pi>> vvPi;
typedef priority_queue<int, vector<int>, greater<int>> pqli;
typedef priority_queue<ll, vector<ll>, greater<ll>> pqlll;
typedef priority_queue<Pi, vector<Pi>, greater<Pi>> pqlP;
string YN(bool y) { return (y ? "YES" : "NO"); }
string yn(bool y) { return (y ? "Yes" : "No"); }
string ON(bool y) { return (y ? "OK" : "NG"); }
int main(void) {
  int h, w, n;

  cin >> h >> w >> n;

  vi a(n);

  vvi c(h, vi(w));

  rep(i, n) cin >> a[i];

  int k = 0;
  rep(i, h) {
    if (i % 2) {
      reprev(j, w) {
        if (a[k] == 0)
          k++;
        c[i][j] = k + 1;
        a[k]--;
      }
    } else {
      rep(j, w) {
        if (a[k] == 0)
          k++;
        c[i][j] = k + 1;
        a[k]--;
      }
    }
  }
  rep(i, h) {
    rep(j, w) {
      cout << c[i][j];
      if (j != w - 1)
        cout << " ";
    }
    cout << endl;
  }
}