Problem p03640 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define pb push_back
#define ft first
#define sd second
#define mpr make_pair
#define all(x) begin((x)), end((x))
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define max3(a, b, c) max((a), max((b), (c)))
#define max4(a, b, c, d) max(max((a), (b)), max((c), (d)))
#define min3(a, b, c) min((a), min((b), (c)))
#define min4(a, b, c, d) min(min((a), (b)), min((c), (d)))
#define isz(x) (int)(x).size()

typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef pair<int, int> pii;
typedef vector<int> vi;

/* <------------------------------------------------------------------------------------>
 */

int ans[107][107], a[107];

void solve() {
  int h, w, n;
  scanf("%d%d%d", &h, &w, &n);
  forn(i, n) scanf("%d", a + i);
  int i = 0, j = 0;
  forn(k, n) while (a[k]--) {
    ans[i][j] = k + 1;
    if (i & 1) {
      --j;
      if (j == -1) {
        ++j;
        ++i;
      }
    } else {
      ++j;
      if (j == w) {
        --j;
        ++i;
      }
    }
  }
  forn(i, h) {
    forn(j, w) printf("%d ", ans[i][j]);
    puts("");
  }
}

/* <------------------------------------------------------------------------------------>
 */

// #define LOCAL
// #define FILE_NAME

int main() {
#if defined LOCAL
  freopen("input.txt", "r", stdin);
#elif defined FILE_NAME
  freopen(FILE_NAME ".in", "r", stdin);
  freopen(FILE_NAME ".out", "w", stdout);
#endif

  ios_base::sync_with_stdio(0);
  cin.tie(0);

  solve();

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define pb push_back
#define ft first
#define sd second
#define mpr make_pair
#define all(x) begin((x)), end((x))
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define max3(a, b, c) max((a), max((b), (c)))
#define max4(a, b, c, d) max(max((a), (b)), max((c), (d)))
#define min3(a, b, c) min((a), min((b), (c)))
#define min4(a, b, c, d) min(min((a), (b)), min((c), (d)))
#define isz(x) (int)(x).size()

typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef pair<int, int> pii;
typedef vector<int> vi;

/* <------------------------------------------------------------------------------------>
 */

int ans[107][107], a[(int)1e4 + 7];

void solve() {
  int h, w, n;
  scanf("%d%d%d", &h, &w, &n);
  forn(i, n) scanf("%d", a + i);
  int i = 0, j = 0;
  forn(k, n) while (a[k]--) {
    ans[i][j] = k + 1;
    if (i & 1) {
      --j;
      if (j == -1) {
        ++j;
        ++i;
      }
    } else {
      ++j;
      if (j == w) {
        --j;
        ++i;
      }
    }
  }
  forn(i, h) {
    forn(j, w) printf("%d ", ans[i][j]);
    puts("");
  }
}

/* <------------------------------------------------------------------------------------>
 */

// #define LOCAL
// #define FILE_NAME

int main() {
#if defined LOCAL
  freopen("input.txt", "r", stdin);
#elif defined FILE_NAME
  freopen(FILE_NAME ".in", "r", stdin);
  freopen(FILE_NAME ".out", "w", stdout);
#endif

  ios_base::sync_with_stdio(0);
  cin.tie(0);

  solve();

  return 0;
}