Problem p03640 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define REP(i, n)                                                              \
  for (long long(i) = 0, i_bound = (long long)(n); (i) < i_bound; ++i)
#define FOR(i, a, b) for (long long(i) = (a); (i) < (b); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, vector<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
const long long MOD = static_cast<long long>(1e9) + 7LL;
const long long INF = 1234567891234567890LL;
int main() {
  cin.tie(0);
  cout.sync_with_stdio(false);
  int H, W, N;
  cin >> H >> W >> N;
  vector<int> A(N);
  REP(i, N) cin >> A[i];
  vector<vector<int>> res(H, vector<int>(W));
  int col = 0;
  int row = 0;
  REP(i, N) {
    while (A[i]) {
      if (col + A[i] >= W) {
        FOR(j, col, N) res[row][j] = i + 1;
        A[i] -= W - col;
        col = 0;
        row++;
      } else {
        FOR(j, col, col + A[i]) res[row][j] = i + 1;
        col += A[i];
        A[i] = 0;
      }
    }
    // cout << "-----------" << endl;
    // cout << row << " " << col << endl;
    // cout << res ;
    // cout << "-----------" << endl;
  }
  REP(i, H) {
    if (i % 2 == 1)
      reverse(ALL(res[i]));
    cout << res[i] << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define REP(i, n)                                                              \
  for (long long(i) = 0, i_bound = (long long)(n); (i) < i_bound; ++i)
#define FOR(i, a, b) for (long long(i) = (a); (i) < (b); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, vector<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
const long long MOD = static_cast<long long>(1e9) + 7LL;
const long long INF = 1234567891234567890LL;
int main() {
  cin.tie(0);
  cout.sync_with_stdio(false);
  int H, W, N;
  cin >> H >> W >> N;
  vector<int> A(N);
  REP(i, N) cin >> A[i];
  vector<vector<int>> res(H, vector<int>(W));
  int col = 0;
  int row = 0;
  REP(i, N) {
    while (A[i]) {
      if (col + A[i] >= W) {
        FOR(j, col, W) res[row][j] = i + 1;
        A[i] -= W - col;
        col = 0;
        row++;
      } else {
        FOR(j, col, col + A[i]) res[row][j] = i + 1;
        col += A[i];
        A[i] = 0;
      }
    }
    // cout << "-----------" << endl;
    // cout << row << " " << col << endl;
    // cout << res ;
    // cout << "-----------" << endl;
  }
  REP(i, H) {
    if (i % 2 == 1)
      reverse(ALL(res[i]));
    cout << res[i] << endl;
  }
  return 0;
}