prithivMLmods/Coder-Stat huggingface dataset:
Problem p03638 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using pint = pair<int, int>;
ll mod = 1000000007;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
ll dp[100100][13];

int main() {
  int h, w, n;
  cin >> h >> w;
  cin >> n;
  vector<int> a(n + 10), b(h * w + 10);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  int j1 = 0;
  for (int i = 0; i < n; ++i) {
    while (a[i]--) {
      b[j1] = i + 1;
      j1++;
    }
  }
  vector<vector<int>> c(h, vector<int>(w, 0));
  j1 = 0;
  for (int i = 0; i < h; ++i) {
    if (i % 2 == 0) {
      for (int j = 0; j < w; ++j) {
        c[i][j] = b[j1];
        j1++;
      }
    } else {
      for (int j = w - 1; j >= 0; ++j) {
        c[i][j] = b[j1];
        j1++;
      }
    }
  }
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (j != w - 1)
        cout << c[i][j] << " ";
      else
        cout << c[i][j];
    }
    cout << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using pint = pair<int, int>;
ll mod = 1000000007;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
ll dp[100100][13];

int main() {
  int h, w, n;
  cin >> h >> w;
  cin >> n;
  vector<int> a(n + 10), b(h * w + 10);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  int j1 = 0;
  for (int i = 0; i < n; ++i) {
    while (a[i]--) {
      b[j1] = i + 1;
      j1++;
    }
  }
  vector<vector<int>> c(h, vector<int>(w, 0));
  j1 = 0;
  for (int i = 0; i < h; ++i) {
    if (i % 2 == 0) {
      for (int j = 0; j < w; ++j) {
        c[i][j] = b[j1];
        j1++;
      }
    } else {
      for (int j = w - 1; j >= 0; --j) {
        c[i][j] = b[j1];
        j1++;
      }
    }
  }
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (j != w - 1)
        cout << c[i][j] << " ";
      else
        cout << c[i][j];
    }
    cout << endl;
  }
  return 0;
}
Problem p03638 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;

int h, w, n;
int a[10010], mp[110][110];

int main() {
  scanf("%d%d", &h, &w);
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  int k = 1;
  for (int i = 1; i <= h; i++) {
    int j;
    if (i % 2 == 1)
      j = 1;
    else
      j = w;
    while (j >= 1 && j <= n) {
      while (a[k] <= 0)
        k++;
      if (a[k] > 0) {
        mp[i][j] = k;
        a[k]--;
        if (i % 2 == 1)
          j++;
        else
          j--;
      }
    }
  }
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      printf("%d ", mp[i][j]);
    }
    printf("\n");
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;

int h, w, n;
int a[10010], mp[110][110];

int main() {
  scanf("%d%d", &h, &w);
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  int k = 1;
  for (int i = 1; i <= h; i++) {
    int j;
    if (i % 2 == 1)
      j = 1;
    else
      j = w;
    while (j >= 1 && j <= w) {
      while (a[k] <= 0)
        k++;
      if (a[k] > 0) {
        mp[i][j] = k;
        a[k]--;
        if (i % 2 == 1)
          j++;
        else
          j--;
      }
    }
  }
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      printf("%d ", mp[i][j]);
    }
    printf("\n");
  }
  return 0;
}
Problem p03638 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>

using namespace std;

int main(void) {
  int H, W, N, *a, **map, h = 0, w = 0;
  scanf("%d %d %d", &H, &W, &N);
  a = (int *)malloc(N * sizeof(int));
  for (int i = 0; i < N; i++) {
    scanf("%d", &a[i]);
  }
  map = (int **)malloc(N * sizeof(int *));
  for (int i = 0; i < H; i++) {
    map[i] = (int *)malloc(W * sizeof(int));
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < a[i]; j++) {
      map[h][w] = i + 1;
      if (h % 2 == 0) {
        if (w == W - 1) {
          h++;
        } else {
          w++;
        }
      } else {
        if (w == 0) {
          h++;
        } else {
          w--;
        }
      }
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      printf("%d", map[i][j]);
      if (j != W - 1) {
        printf(" ");
      }
    }
    printf("\n");
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>

using namespace std;

int main(void) {
  int H, W, N, *a, **map, h = 0, w = 0;
  scanf("%d %d %d", &H, &W, &N);
  a = (int *)malloc(N * sizeof(int));
  for (int i = 0; i < N; i++) {
    scanf("%d", &a[i]);
  }
  map = (int **)malloc(H * sizeof(int *));
  for (int i = 0; i < H; i++) {
    map[i] = (int *)malloc(W * sizeof(int));
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < a[i]; j++) {
      map[h][w] = i + 1;
      if (h % 2 == 0) {
        if (w == W - 1) {
          h++;
        } else {
          w++;
        }
      } else {
        if (w == 0) {
          h++;
        } else {
          w--;
        }
      }
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      printf("%d", map[i][j]);
      if (j != W - 1) {
        printf(" ");
      }
    }
    printf("\n");
  }
  return 0;
}
Problem p03638 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <string.h>
const int N = 1000 + 5;
int a[N], b[N][N], c[N * N];
int main() {
  int i, j, n, h, k, w;
  scanf("%d %d", &h, &w);
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  int cnt = 0;
  for (j = 0; j < n; j++) {
    for (k = 0; k < a[j]; k++) {
      c[cnt++] = j + 1;
    }
  }
  cnt = 0;
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      b[i][j] = c[cnt++];
    }
  }
  for (i = 0; i < h; i++) {
    if (i % 2) {
      for (j = w - 1; j >= 0; j--) {
        if (j < w - 1)
          printf(" ");
        printf("%d", b[i][j]);
      }
    } else {
      for (j = 0; j < w; j++) {
        if (j)
          printf(" ");
        printf("%d", b[i][j]);
      }
    }
    printf("\n");
  }
}

Fixed submission:
#include <stdio.h>
#include <string.h>
const int N = 1000 + 5;
int a[N * N], b[N][N], c[N * N];
int main() {
  int i, j, n, h, k, w;
  scanf("%d %d", &h, &w);
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  int cnt = 0;
  for (j = 0; j < n; j++) {
    for (k = 0; k < a[j]; k++) {
      c[cnt++] = j + 1;
    }
  }
  cnt = 0;
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      b[i][j] = c[cnt++];
    }
  }
  for (i = 0; i < h; i++) {
    if (i % 2) {
      for (j = w - 1; j >= 0; j--) {
        if (j < w - 1)
          printf(" ");
        printf("%d", b[i][j]);
      }
    } else {
      for (j = 0; j < w; j++) {
        if (j)
          printf(" ");
        printf("%d", b[i][j]);
      }
    }
    printf("\n");
  }
}
Problem p03638 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define vi push_back
#define vo pop_back
#define mp make_pair
#define fe first
#define se second
typedef long long ll;

const int MXN = 5;

int h, w, n, Colors[MXN * MXN], Square[MXN][MXN];
bool CheckFlag;

void Input() {
  cin >> h >> w >> n;
  for (int i = 1; i <= n; i++)
    cin >> Colors[i];
}

int ColorToUse(int x) {
  for (int i = x; i <= n; i++)
    if (Colors[i] > 0)
      return i;
  return 0;
}

bool Check(int i, int j) {
  if (!CheckFlag)
    return i < h && j < w;
  return i >= 0 && j < w;
}

int Con() {
  if (!CheckFlag)
    return h - 1;
  return 0;
}

void Solve() {
  int clr = ColorToUse(1), d = 1, s = 0, j = 0;
  for (int i = s; Check(i, j); i += d) {
    Square[i][j] = clr;
    Colors[clr]--;
    clr = ColorToUse(clr);
    if (i == Con()) {
      j++;
      i = Con();
      if (!CheckFlag)
        i++;
      else
        i--;
      d *= -1;
      CheckFlag ^= 1;
    }
  }
}

void Output() {
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cout << Square[i][j] << ' ';
    }
    cout << endl;
  }
}

int main() {
  Input();
  Solve();
  Output();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define vi push_back
#define vo pop_back
#define mp make_pair
#define fe first
#define se second
typedef long long ll;

const int MXN = 1e2 + 10;

int h, w, n, Colors[MXN * MXN], Square[MXN][MXN];
bool CheckFlag;

void Input() {
  cin >> h >> w >> n;
  for (int i = 1; i <= n; i++)
    cin >> Colors[i];
}

int ColorToUse(int x) {
  for (int i = x; i <= n; i++)
    if (Colors[i] > 0)
      return i;
  return 0;
}

bool Check(int i, int j) {
  if (!CheckFlag)
    return i < h && j < w;
  return i >= 0 && j < w;
}

int Con() {
  if (!CheckFlag)
    return h - 1;
  return 0;
}

void Solve() {
  int clr = ColorToUse(1), d = 1, s = 0, j = 0;
  for (int i = s; Check(i, j); i += d) {
    Square[i][j] = clr;
    Colors[clr]--;
    clr = ColorToUse(clr);
    if (i == Con()) {
      j++;
      i = Con();
      if (!CheckFlag)
        i++;
      else
        i--;
      d *= -1;
      CheckFlag ^= 1;
    }
  }
}

void Output() {
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cout << Square[i][j] << ' ';
    }
    cout << endl;
  }
}

int main() {
  Input();
  Solve();
  Output();
  return 0;
}
Problem p03638 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define pb push_back

int cnt, h, w, N;
vector<int> a;

int main() {
  cin >> h >> w;
  cin >> N;
  a.resize(N);

  rep(i, N) { cin >> a[i]; }
  int c[h][w];

  int k = 1;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      c[i][j] = k;
      a[k - 1]--;
      if (a[k - 1] == 0)
        k++;
    }
    i++;
    for (int j = w - 1; j >= 0; j--) {
      c[i][j] = k;
      a[k - 1]--;
      if (a[k - 1] == 0)
        k++;
    }
  }

  rep(i, h) {
    rep(j, w) { cout << c[i][j] << " "; }
    cout << endl;
  }

  return 0;
}


Fixed submission:
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define pb push_back

int cnt, h, w, N;
vector<int> a;

int main() {
  cin >> h >> w;
  cin >> N;
  a.resize(N);

  rep(i, N) { cin >> a[i]; }
  int c[h][w];

  int k = 1;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      c[i][j] = k;
      a[k - 1]--;
      if (a[k - 1] == 0)
        k++;
    }
    i++;
    if (i == h)
      break;
    for (int j = w - 1; j >= 0; j--) {
      c[i][j] = k;
      a[k - 1]--;
      if (a[k - 1] == 0)
        k++;
    }
  }

  rep(i, h) {
    rep(j, w) { cout << c[i][j] << " "; }
    cout << endl;
  }

  return 0;
}
Problem p03638 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int main() {
  int h, w, a[105], b[105][105];
  scanf("%d %d", &h, &w);
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  int p = 1, l = 1;
  for (int i = 1; i <= n; i++) {
    while (a[i] != 0) {
      a[i]--;
      b[l][p++] = i;
      if (p == w + 1) {
        p = 1;
        l++;
      }
    }
  }
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++) {
      if (i % 2 == 1) {
        if (j == w)
          printf("%d\n", b[i][j]);
        else
          printf("%d ", b[i][j]);
      } else {
        if (j == w)
          printf("%d\n", b[i][w - j + 1]);
        else
          printf("%d ", b[i][w - j + 1]);
      }
    }
}

Fixed submission:
#include <stdio.h>
int main() {
  int h, w, a[100005], b[105][105];
  scanf("%d %d", &h, &w);
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  int p = 1, l = 1;
  for (int i = 1; i <= n; i++) {
    while (a[i] != 0) {
      a[i]--;
      b[l][p++] = i;
      if (p == w + 1) {
        p = 1;
        l++;
      }
    }
  }
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++) {
      if (i % 2 == 1) {
        if (j == w)
          printf("%d\n", b[i][j]);
        else
          printf("%d ", b[i][j]);
      } else {
        if (j == w)
          printf("%d\n", b[i][w - j + 1]);
        else
          printf("%d ", b[i][w - j + 1]);
      }
    }
}
Problem p03638 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;

const int N = 102;
int in[N], C[N][N];

int main() {
  int n, m, q;
  cin >> n >> m >> q;
  for (int i = 1; i <= q; ++i)
    cin >> in[i];

  queue<pair<int, int>> qu;
  for (int i = 1; i <= n; ++i) {
    if (i & 1)
      for (int j = m; j >= 1; j--)
        qu.emplace(i, j);
    else
      for (int j = 1; j <= m; ++j)
        qu.emplace(i, j);
  }

  for (int i = 1; i <= q; ++i) {
    int k = in[i];
    while (k--) {
      C[qu.front().first][qu.front().second] = i;
      qu.pop();
    }
  }

  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      printf("%d%c", C[i][j], " \n"[j == m]);
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;

const int N = 105;
int in[N * N], C[N][N];

int main() {
  int n, m, q;
  cin >> n >> m >> q;
  for (int i = 1; i <= q; ++i)
    cin >> in[i];

  queue<pair<int, int>> qu;
  for (int i = 1; i <= n; ++i) {
    if (i & 1)
      for (int j = m; j >= 1; j--)
        qu.emplace(i, j);
    else
      for (int j = 1; j <= m; ++j)
        qu.emplace(i, j);
  }

  for (int i = 1; i <= q; ++i) {
    int k = in[i];
    while (k--) {
      C[qu.front().first][qu.front().second] = i;
      qu.pop();
    }
  }

  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      printf("%d%c", C[i][j], " \n"[j == m]);
}
Problem p03638 (Python)

Buggy submission (status: Runtime Error):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10**9)
INF = 1 << 60
MOD = 1000000007


def main():
    H, W, N, *A = map(int, read().split())

    C = [[0] * W for _ in range(H)]
    vec = [i for i, a in enumerate(A, 1) for _ in range(a)]

    for i in range(H):
        for j in range(W):
            color = vec[i * H + j]
            if i % 2 == 0:
                C[i][j] = color
            else:
                C[i][W - j - 1] = color

    for row in C:
        print(*row)

    return


if __name__ == "__main__":
    main()


Fixed submission:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10**9)
INF = 1 << 60
MOD = 1000000007


def main():
    H, W, N, *A = map(int, read().split())

    C = [[0] * W for _ in range(H)]
    vec = [i for i, a in enumerate(A, 1) for _ in range(a)]

    for i in range(H):
        for j in range(W):
            color = vec[i * W + j]
            if i % 2 == 0:
                C[i][j] = color
            else:
                C[i][W - j - 1] = color

    for row in C:
        print(*row)

    return


if __name__ == "__main__":
    main()
Problem p03638 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef pair<int, int> P;
typedef long long LL;
typedef vector<LL> VEC;
typedef vector<VEC> VVEC;

const LL INF = 1 << 30;
const LL MOD = 1000000007;

LL gcd(LL a, LL b) {
  if (b == 0)
    return a;

  return gcd(b, a % b);
}

LL kaijo(LL n) {
  LL ans = 1;
  for (LL i = 1; i <= n; ++i) {
    (ans *= i) %= MOD;
  }
  return ans;
}

int main() {
  int h, w;
  cin >> h >> w;
  int n;
  cin >> n;
  VEC a(n);
  VVEC ans(h, VEC(w));
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }

  // sort(a.begin(),a.end(),greater<LL>());
  int k = 1;
  for (int i = 0; i < h; i++) {
    if (i % 2 == 0) {
      for (int j = 0; j < w; ++j) {
        ans[i][j] = k;
        a[k]--;
        if (a[k] == 0)
          k++;
      }

    } else {
      for (int j = w - 1; j >= 0; --j) {
        ans[i][j] = k;
        a[k]--;
        if (a[k] == 0)
          k++;
      }
    }
  }
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      cout << ans[i][j] << " ";
    }
    cout << "\n";
  }
  cout << endl;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef pair<int, int> P;
typedef long long LL;
typedef vector<int> VEC;
typedef vector<VEC> VVEC;

const LL INF = 1 << 30;
const LL MOD = 1000000007;

LL gcd(LL a, LL b) {
  if (b == 0)
    return a;

  return gcd(b, a % b);
}

LL kaijo(LL n) {
  LL ans = 1;
  for (LL i = 1; i <= n; ++i) {
    (ans *= i) %= MOD;
  }
  return ans;
}

int main() {
  int h, w;
  cin >> h >> w;
  int n;
  cin >> n;
  VEC a(n);
  VVEC ans(h, VEC(w));
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }

  // sort(a.begin(),a.end(),greater<LL>());
  int k = 1;
  for (int i = 0; i < h; i++) {
    if (i % 2 == 0) {
      for (int j = 0; j < w; ++j) {
        ans[i][j] = k;
        a[k]--;
        if (a[k] == 0)
          k++;
      }

    } else {
      for (int j = w - 1; j >= 0; --j) {
        ans[i][j] = k;
        a[k]--;
        if (a[k] == 0)
          k++;
      }
    }
  }
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      cout << ans[i][j] << " ";
    }
    cout << "\n";
  }
  cout << endl;
}
Problem p03638 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;
int test[1234];
int uiop[12345][12345];
int main() {
  long long n, m, k, l, a = 0, b = 0, c = 1;
  cin >> n >> m >> k;
  for (int h = 0; h < k; h++) {
    cin >> l;
    test[h] = l;
  }
  for (int h = 0; h < n; h++) {
    if (h % 2 == 0) {
      for (int i = 0; i < m; i++) {
        uiop[h][i] = c;
        a++;
        if (test[b] == a) {
          a = 0;
          b++;
          c++;
        }
      }
    } else {
      for (int i = m - 1; i >= 0; i--) {
        uiop[h][i] = c;
        a++;
        if (test[b] == a) {
          a = 0;
          b++;
          c++;
        }
      }
    }
  }
  for (int h = 0; h < n; h++) {
    for (int i = 0; i < m; i++) {
      cout << uiop[h][i] << ' ';
    }
    cout << endl;
  }
  cin >> n;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;
int test[12345];
int uiop[12345][12345];
int main() {
  long long n, m, k, l, a = 0, b = 0, c = 1;
  cin >> n >> m >> k;
  for (int h = 0; h < k; h++) {
    cin >> l;
    test[h] = l;
  }
  for (int h = 0; h < n; h++) {
    if (h % 2 == 0) {
      for (int i = 0; i < m; i++) {
        uiop[h][i] = c;
        a++;
        if (test[b] == a) {
          a = 0;
          b++;
          c++;
        }
      }
    } else {
      for (int i = m - 1; i >= 0; i--) {
        uiop[h][i] = c;
        a++;
        if (test[b] == a) {
          a = 0;
          b++;
          c++;
        }
      }
    }
  }
  for (int h = 0; h < n; h++) {
    for (int i = 0; i < m; i++) {
      cout << uiop[h][i] << ' ';
    }
    cout << endl;
  }
  cin >> n;
}
Problem p03638 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

// #define int long long
struct Fast {
  Fast() {
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
} fast;
using intpair = pair<int, int>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define ceiv(a, b) ((int)((a) + (b)-1) / (int)(b))

int H, W, N;
vector<vector<int>> ans(100, vector<int>(100, -1));

bool is_safe(int x, int y) {
  if (ans[y][x] != -1)
    return false;
  return (0 <= x && x < W) && (0 <= y && y < H);
}

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

signed main() {
  cin >> H >> W >> N;

  queue<int> brush;
  reps(i, 1, N) {
    int a;
    cin >> a;
    rep(j, a) brush.push(i);
  }

  int x = 0, y = 0, dir = 0;
  rep(i, H * W) {
    ans[y][x] = brush.front();
    brush.pop();
    if (!is_safe(x + dx[dir], y + dy[dir])) {
      dir = (dir + 1) % 4;
    }
    x += dx[dir];
    y += dy[dir];
  }

  rep(i, H) {
    rep(j, W) cout << (j ? " " : "") << ans[i][j];
    cout << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

// #define int long long
struct Fast {
  Fast() {
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
} fast;
using intpair = pair<int, int>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define ceiv(a, b) ((int)((a) + (b)-1) / (int)(b))

int H, W, N;
vector<vector<int>> ans(100, vector<int>(100, -1));

bool is_safe(int x, int y) {
  if ((0 <= x && x < W) && (0 <= y && y < H)) {
    return ans[y][x] == -1;
  }
  return false;
}

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

signed main() {
  cin >> H >> W >> N;

  queue<int> brush;
  reps(i, 1, N) {
    int a;
    cin >> a;
    rep(j, a) brush.push(i);
  }

  int x = 0, y = 0, dir = 0;
  rep(i, H * W) {
    ans[y][x] = brush.front();
    brush.pop();
    if (!is_safe(x + dx[dir], y + dy[dir])) {
      dir = (dir + 1) % 4;
    }
    x += dx[dir];
    y += dy[dir];
  }

  rep(i, H) {
    rep(j, W) cout << (j ? " " : "") << ans[i][j];
    cout << endl;
  }
}
Problem p03638 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>

using namespace std;

int h, w, n;
int a[111];
int res[111][111];

int main(void) {
  scanf("%d%d%d", &h, &w, &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", a + i);
  }
  int p = 0, c = 0;
  for (int i = 0; i < h; i++) {
    if (i % 2 == 0) {
      for (int j = 0; j < w; j++) {
        int x;
        if (c++ < a[p])
          x = p + 1;
        else {
          c = 1;
          x = ++p + 1;
        }
        res[i][j] = x;
      }
    }
    if (i % 2 == 1) {
      for (int j = w - 1; j >= 0; j--) {
        int x;
        if (c++ < a[p])
          x = p + 1;
        else {
          c = 1;
          x = ++p + 1;
        }
        res[i][j] = x;
      }
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      printf("%d%c", res[i][j], j == w - 1 ? '\n' : ' ');
  }
  return 0;
}


Fixed submission:
#include <cstdio>

using namespace std;

int h, w, n;
int a[11111];
int res[111][111];

int main(void) {
  scanf("%d%d%d", &h, &w, &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", a + i);
  }
  int p = 0, c = 0;
  for (int i = 0; i < h; i++) {
    if (i % 2 == 0) {
      for (int j = 0; j < w; j++) {
        int x;
        if (c++ < a[p])
          x = p + 1;
        else {
          c = 1;
          x = ++p + 1;
        }
        res[i][j] = x;
      }
    }
    if (i % 2 == 1) {
      for (int j = w - 1; j >= 0; j--) {
        int x;
        if (c++ < a[p])
          x = p + 1;
        else {
          c = 1;
          x = ++p + 1;
        }
        res[i][j] = x;
      }
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      printf("%d%c", res[i][j], j == w - 1 ? '\n' : ' ');
  }
  return 0;
}
Problem p03638 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define MOD 1000000007;
#define PI 3.14159265358979323846;
#define rep(i, n) for (ll i = 0; i < (ll)n; i++)
using namespace std;
typedef long long ll;
static const int MAX = 50;
static const long long INFTYL = 9223372036854775807;
ll mod = 1000000007;

int main() {
  ll H, W;
  cin >> H >> W;
  ll N;
  cin >> N;

  ll a[100];
  rep(i, N) { cin >> a[i]; }

  ll c[100][100];
  rep(i, 100) rep(j, 100) c[i][j] = -1;

  ll currentH = 0;
  ll currentW = 0;
  rep(i, N) {
    rep(j, a[i]) {
      c[currentH][currentW] = i;

      if (currentH % 2 == 0) {
        currentW++;
        if (currentW == W) {
          currentH++;
          currentW--;
        }
      } else {
        currentW--;
        if (currentW == -1) {
          currentH++;
          currentW++;
        }
      }
    }
  }

  rep(i, H) {
    rep(j, W) {
      if (c[i][j] != -1) {
        cout << c[i][j] + 1;
      }
      if (j != W - 1) {
        cout << " ";
      }
    }
    cout << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define MOD 1000000007;
#define PI 3.14159265358979323846;
#define rep(i, n) for (ll i = 0; i < (ll)n; i++)
using namespace std;
typedef long long ll;
static const int MAX = 50;
static const long long INFTYL = 9223372036854775807;
ll mod = 1000000007;

int main() {
  ll H, W;
  cin >> H >> W;
  ll N;
  cin >> N;

  ll a[10000];
  rep(i, N) { cin >> a[i]; }

  ll c[100][100];
  rep(i, 100) rep(j, 100) c[i][j] = -1;

  ll currentH = 0;
  ll currentW = 0;
  rep(i, N) {
    rep(j, a[i]) {
      c[currentH][currentW] = i;

      if (currentH % 2 == 0) {
        currentW++;
        if (currentW == W) {
          currentH++;
          currentW--;
        }
      } else {
        currentW--;
        if (currentW == -1) {
          currentH++;
          currentW++;
        }
      }
    }
  }

  rep(i, H) {
    rep(j, W) {
      if (c[i][j] != -1) {
        cout << c[i][j] + 1;
      }
      if (j != W - 1) {
        cout << " ";
      }
    }
    cout << endl;
  }
  return 0;
}
Problem p03638 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int h, w;
  cin >> h >> w;
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  vector<vector<int>> ans(h, vector<int>(w));

  int y = 0;
  int x = 0;
  auto coloring = [&](int color) {
    ans[y][x] = color;
    if (y == 0) {
      if (x % 2 == 0) {
        y++;
      } else {
        x++;
      }
    } else if (y == h - 1) {
      if (x % 2 == 0) {
        x++;
      } else {
        y--;
      }
    } else {
      if (x % 2 == 0) {
        y++;
      } else {
        y--;
      }
    }
  };

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < a[i]; j++) {
      coloring(i + 1);
    }
  }

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cout << ans[i][j] << " ";
    }
    cout << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int h, w;
  cin >> h >> w;
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  vector<vector<int>> ans(h, vector<int>(w));

  int y = 0;
  int x = 0;
  auto coloring = [&](int color) {
    ans[y][x] = color;
    if (y == 0) {
      if (x % 2 == 0 && h != 1) {
        y++;
      } else {
        x++;
      }
    } else if (y == h - 1) {
      if (x % 2 == 0) {
        x++;
      } else {
        y--;
      }
    } else {
      if (x % 2 == 0) {
        y++;
      } else {
        y--;
      }
    }
  };

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < a[i]; j++) {
      coloring(i + 1);
    }
  }

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cout << ans[i][j] << " ";
    }
    cout << endl;
  }

  return 0;
}
Problem p03638 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, k, n) for (int i = k; i < (int)(n); i++)
typedef long long ll;

#define vi vector<int>
#define pb push_back

int a[100 * 10 + 10];
int main() {
  int h, w, n, num, ni;

  cin >> h >> w >> n;
  ni = 0;
  rep(i, n) {
    cin >> num;
    for (int j = 0; j < num; j++) {
      a[ni] = i + 1;
      ni++;
    }
  }
  rep(y, h) {
    if (y % 2 == 0) {
      rep(x, w) {
        cout << a[y * w + x];
        if (x != w - 1)
          cout << " ";
      }
    } else {
      rep(x, w) {
        cout << a[((y + 1) * w - 1) - x];
        if (x != w - 1)
          cout << " ";
      }
    }
    cout << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, k, n) for (int i = k; i < (int)(n); i++)
typedef long long ll;

#define vi vector<int>
#define pb push_back

int a[100 * 100 + 10];
int main() {
  int h, w, n, num, ni;

  cin >> h >> w >> n;
  ni = 0;
  rep(i, n) {
    cin >> num;
    for (int j = 0; j < num; j++) {
      a[ni] = i + 1;
      ni++;
    }
  }
  rep(y, h) {
    if (y % 2 == 0) {
      rep(x, w) {
        cout << a[y * w + x];
        if (x != w - 1)
          cout << " ";
      }
    } else {
      rep(x, w) {
        cout << a[((y + 1) * w - 1) - x];
        if (x != w - 1)
          cout << " ";
      }
    }
    cout << endl;
  }
  return 0;
}
Problem p03638 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define all(x) begin(x), end(x)
#define dbg(x) cerr << #x << " = " << x << endl
#define _ << ' ' <<
using namespace std;
using ll = long long;
using vi = vector<int>;

int a[101];
int sol[101][101];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h, w, n;
  cin >> h >> w >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];

  int k = 1;
  for (int i = 0; i < h; ++i) {
    if (i % 2) {
      for (int j = 0; j < w; ++j) {
        if (a[k] == 0)
          k++;
        a[k]--;
        sol[i][j] = k;
      }
    } else {
      for (int j = w - 1; j >= 0; --j) {
        if (a[k] == 0)
          k++;
        a[k]--;
        sol[i][j] = k;
      }
    }
  }
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j)
      cout << sol[i][j] << ' ';
    cout << '\n';
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define all(x) begin(x), end(x)
#define dbg(x) cerr << #x << " = " << x << endl
#define _ << ' ' <<
using namespace std;
using ll = long long;
using vi = vector<int>;

int a[10000];
int sol[101][101];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h, w, n;
  cin >> h >> w >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];

  int k = 1;
  for (int i = 0; i < h; ++i) {
    if (i % 2) {
      for (int j = 0; j < w; ++j) {
        if (a[k] == 0)
          k++;
        a[k]--;
        sol[i][j] = k;
      }
    } else {
      for (int j = w - 1; j >= 0; --j) {
        if (a[k] == 0)
          k++;
        a[k]--;
        sol[i][j] = k;
      }
    }
  }
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j)
      cout << sol[i][j] << ' ';
    cout << '\n';
  }
}
Problem p03638 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int num[105];
int ma[105][105];
int main() {
  ios::sync_with_stdio(false);
  int h, w, n;
  cin >> h >> w >> n;
  for (int i = 1; i <= n; i++) {
    cin >> num[i];
  }
  int hh = 1, ww = 1;
  for (int i = 1; i <= n; i++) {
    while (num[i]) {
      //            cout << "i = " << i << " hh = " << hh << " ww = " << ww
      //            <<endl;
      ma[hh][ww] = i;
      num[i]--;
      if (hh & 1)
        ww++;
      else
        ww--;
      if (hh & 1 && ww == w + 1)
        hh++, ww--;
      else if (!(hh & 1) && ww == 0)
        hh++, ww++;
    }
  }
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cout << ma[i][j];
      if (j == w)
        cout << "\n";
      else
        cout << " ";
    }
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int num[10000 + 5];
int ma[105][105];
int main() {
  ios::sync_with_stdio(false);
  int h, w, n;
  cin >> h >> w >> n;
  for (int i = 1; i <= n; i++) {
    cin >> num[i];
  }
  int hh = 1, ww = 1;
  for (int i = 1; i <= n; i++) {
    while (num[i]) {
      //            cout << "i = " << i << " hh = " << hh << " ww = " << ww
      //            <<endl;
      ma[hh][ww] = i;
      num[i]--;
      if (hh & 1)
        ww++;
      else
        ww--;
      if (hh & 1 && ww == w + 1)
        hh++, ww--;
      else if (!(hh & 1) && ww == 0)
        hh++, ww++;
    }
  }
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cout << ma[i][j];
      if (j == w)
        cout << "\n";
      else
        cout << " ";
    }
  }

  return 0;
}
Problem p03638 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int h, w, n, a[1010], ans[101][101], k = 1;

int main() {
  cin >> h >> w >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= h; i++) {
    if (i % 2 == 1) {
      for (int j = 1; j <= w; j++) {
        if (a[k] > 0)
          ans[i][j] = k, a[k]--;
        else {
          k++;
          ans[i][j] = k;
          a[k]--;
        }
      }
    } else {
      for (int j = w; j >= 1; j--) {
        if (a[k] > 0)
          ans[i][j] = k, a[k]--;
        else {
          k++;
          ans[i][j] = k;
          a[k]--;
        }
      }
    }
  }
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cout << ans[i][j] << " ";
    }
    cout << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int h, w, n, a[10100], ans[110][111], k = 1;

int main() {
  cin >> h >> w >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= h; i++) {
    if (i % 2 == 1) {
      for (int j = 1; j <= w; j++) {
        if (a[k] > 0)
          ans[i][j] = k, a[k]--;
        else {
          k++;
          ans[i][j] = k;
          a[k]--;
        }
      }
    } else {
      for (int j = w; j >= 1; j--) {
        if (a[k] > 0)
          ans[i][j] = k, a[k]--;
        else {
          k++;
          ans[i][j] = k;
          a[k]--;
        }
      }
    }
  }
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cout << ans[i][j] << " ";
    }
    cout << endl;
  }
  return 0;
}
Problem p03638 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>

using namespace std;

int a[1005], b[105][105];

int main() {
  int h, w, n;
  cin >> h >> w >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  int now = a[1], next = 2;
  for (int i = 0; i < h; ++i) {
    if (i % 2) {
      for (int j = w - 1; j >= 0; --j) {
        if (now == 0) {
          now = a[next];
          ++next;
        }
        b[i][j] = next - 1;
        --now;
      }
    } else {
      for (int j = 0; j < w; ++j) {
        if (now == 0) {
          now = a[next];
          ++next;
        }
        b[i][j] = next - 1;
        --now;
      }
    }
  }
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      cout << b[i][j] << " \n"[j == w - 1];
    }
  }
  return 0;
}


Fixed submission:
#include <iostream>
#include <string>

using namespace std;

int a[10005], b[105][105];

int main() {
  int h, w, n;
  cin >> h >> w >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  int now = a[1], next = 2;
  for (int i = 0; i < h; ++i) {
    if (i % 2) {
      for (int j = w - 1; j >= 0; --j) {
        if (now == 0) {
          now = a[next];
          ++next;
        }
        b[i][j] = next - 1;
        --now;
      }
    } else {
      for (int j = 0; j < w; ++j) {
        if (now == 0) {
          now = a[next];
          ++next;
        }
        b[i][j] = next - 1;
        --now;
      }
    }
  }
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      cout << b[i][j] << " \n"[j == w - 1];
    }
  }
  return 0;
}
Problem p03638 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
#define OP(m) cout << m << endl
typedef long long ll;
typedef unsigned long long ull;

int m[100][100];

int main() {
  int h, w, n;
  cin >> h >> w >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  vector<int> c;
  rep(i, n) rep(j, a[i]) c.push_back(i + 1);

  int k = 0;
  rep(i, n) {
    bool flag = (i % 2 == 0 ? 0 : 1);
    int j = (flag == 0 ? 0 : w - 1);
    int cnt = 0;
    while (cnt < w) {
      m[i][j] = c[k];
      flag == 0 ? j++ : j--;
      k++;
      cnt++;
    }
  }

  rep(i, h) rep(j, w) j != w - 1 ? cout << m[i][j] << " " : OP(m[i][j]);

  return 0;
}

Fixed submission:
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
#define OP(m) cout << m << endl
typedef long long ll;
typedef unsigned long long ull;

int m[100][100];

int main() {
  int h, w, n;
  cin >> h >> w >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  vector<int> c;
  rep(i, n) rep(j, a[i]) c.push_back(i + 1);

  int k = 0;
  rep(i, h) {
    bool flag = (i % 2 == 0 ? 0 : 1);
    int j = (flag == 0 ? 0 : w - 1);
    int cnt = 0;
    while (cnt < w) {
      m[i][j] = c[k];
      flag == 0 ? j++ : j--;
      k++;
      cnt++;
    }
  }

  rep(i, h) rep(j, w) j != w - 1 ? cout << m[i][j] << " " : OP(m[i][j]);

  return 0;
}
Problem p03638 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int H, W, N, A, now = 0;

int f1() {
  if (!A) {
    cin >> A;
    now++;
  }
  A--;
  return now;
}

void f2(int i) {
  vector<int> V(W);
  for (int i = 0; i < W; i++)
    V.at(i) = f1();
  if (i % 2)
    reverse(V.begin(), V.end());
  for (int i = 0; i < N; i++) {
    if (i != 0)
      cout << " ";
    cout << V.at(i);
  }
  cout << "\n";
}

int main() {
  cin >> H >> W >> N;
  for (int i = 0; i < H; i++)
    f2(i);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int H, W, N, A, now = 0;

int f1() {
  if (!A) {
    cin >> A;
    now++;
  }
  A--;
  return now;
}

void f2(int i) {
  vector<int> V(W);
  for (int i = 0; i < W; i++)
    V.at(i) = f1();
  if (i % 2)
    reverse(V.begin(), V.end());
  for (int i = 0; i < W; i++) {
    if (i != 0)
      cout << " ";
    cout << V.at(i);
  }
  cout << "\n";
}

int main() {
  cin >> H >> W >> N;
  for (int i = 0; i < H; i++)
    f2(i);
}
Problem p03638 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int m, n, a, arr[10000], brr[10000][10000], tmp[1000], num;
void input() {
  cin >> m >> n;
  cin >> a;
  for (int i = 0; i < a; i++)
    cin >> arr[i];
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < arr[i]; j++) {
      tmp[num++] = i + 1;
    }
  }
}
void solve() {
  num = 0;
  for (int i = 0; i < m; i++) {
    for (int j = 0, k = n - 1; j < n, k >= 0; j++, k--) {
      if (i % 2 == 0)
        brr[i][j] = tmp[num++];
      else
        brr[i][k] = tmp[num++];
    }
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      cout << brr[i][j] << " ";
    }
    cout << endl;
  }
}
int main() { input(), solve(); }


Fixed submission:
#include <iostream>
using namespace std;
int m, n, a, arr[10000], brr[10000][10000], tmp[10000], num;
void input() {
  cin >> m >> n;
  cin >> a;
  for (int i = 0; i < a; i++)
    cin >> arr[i];
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < arr[i]; j++) {
      tmp[num++] = i + 1;
    }
  }
}
void solve() {
  num = 0;
  for (int i = 0; i < m; i++) {
    for (int j = 0, k = n - 1; j < n, k >= 0; j++, k--) {
      if (i % 2 == 0)
        brr[i][j] = tmp[num++];
      else
        brr[i][k] = tmp[num++];
    }
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      cout << brr[i][j] << " ";
    }
    cout << endl;
  }
}
int main() { input(), solve(); }
Problem p03638 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int INF = 1000000000;

int main() {
  int H, W;
  scanf("%d %d", &H, &W);
  int N;
  scanf("%d", &N);
  vector<int> avail(N + 5);
  for (int i = 1; i <= N; ++i) {
    scanf("%d", &avail[i]);
  }
  vector<vector<int>> grid(N, vector<int>(N, 0));
  int color = 1;
  for (int r = 0; r < H; ++r) {
    if (r % 2 == 0) {
      // ==>
      int c = 0;
      while (c < W) {
        grid[r][c] = color;
        if (--avail[color] == 0) {
          ++color;
        }
        ++c;
      }
    } else {
      // <==
      int c = W - 1;
      while (c >= 0) {
        grid[r][c] = color;
        if (--avail[color] == 0) {
          ++color;
        }
        --c;
      }
    }
  }
  for (int r = 0; r < H; ++r) {
    printf("%d", grid[r][0]);
    for (int c = 1; c < W; ++c) {
      printf(" %d", grid[r][c]);
    }
    printf("\n");
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int INF = 1000000000;

int main() {
  int H, W;
  scanf("%d %d", &H, &W);
  int N;
  scanf("%d", &N);
  vector<int> avail(N + 5);
  for (int i = 1; i <= N; ++i) {
    scanf("%d", &avail[i]);
  }
  vector<vector<int>> grid(H, vector<int>(W, 0));
  int color = 1;
  for (int r = 0; r < H; ++r) {
    if (r % 2 == 0) {
      // ==>
      int c = 0;
      while (c < W) {
        grid[r][c] = color;
        if (--avail[color] == 0) {
          ++color;
        }
        ++c;
      }
    } else {
      // <==
      int c = W - 1;
      while (c >= 0) {
        grid[r][c] = color;
        if (--avail[color] == 0) {
          ++color;
        }
        --c;
      }
    }
  }
  for (int r = 0; r < H; ++r) {
    printf("%d", grid[r][0]);
    for (int c = 1; c < W; ++c) {
      printf(" %d", grid[r][c]);
    }
    printf("\n");
  }
  return 0;
}
Problem p03638 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> Pi;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, n) for (int i = 1; i <= (n); i++)
#define rep3(i, i0, n) for (int i = i0; i < (n); i++)
#define pb push_back
#define mod 1000000007
const ll INF = 1LL << 60;
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;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
#define all(x) x.begin(), x.end()
#define mp make_pair
bool compare(Pi a, Pi b) {
  if (a.first != b.first) {
    return a.first < b.first;

  } else {
    return a.second < b.second;
  }
}

bool In_map(ll y, ll x, ll h, ll w) {
  if (y < 0 || x < 0 || y >= h || x >= w) {
    return 0;
  } else {
    return 1;
  }
}
const vector<ll> dx{1, 0, -1, 0};
const vector<ll> dy{0, 1, 0, -1};

int main() {
  ll H, W, N;
  cin >> H >> W >> N;
  vector<ll> A;
  rep(i, N) {
    ll a;
    cin >> a;
    rep(j, a) { A.pb(i + 1); }
  }
  vector<vector<ll>> ans(H, vector<ll>(W));
  rep(i, H) {
    if (i % 2 == 0) {
      rep(j, W) { ans[i][j] = A[W * i + j]; }
    } else {
      rep(j, N) { ans[i][W - j - 1] = A[W * i + j]; }
    }
  }

  rep(i, H) {
    rep(j, W) { cout << ans[i][j] << " "; }
    cout << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> Pi;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, n) for (int i = 1; i <= (n); i++)
#define rep3(i, i0, n) for (int i = i0; i < (n); i++)
#define pb push_back
#define mod 1000000007
const ll INF = 1LL << 60;
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;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
#define all(x) x.begin(), x.end()
#define mp make_pair
bool compare(Pi a, Pi b) {
  if (a.first != b.first) {
    return a.first < b.first;

  } else {
    return a.second < b.second;
  }
}

bool In_map(ll y, ll x, ll h, ll w) {
  if (y < 0 || x < 0 || y >= h || x >= w) {
    return 0;
  } else {
    return 1;
  }
}
const vector<ll> dx{1, 0, -1, 0};
const vector<ll> dy{0, 1, 0, -1};

int main() {
  ll H, W, N;
  cin >> H >> W >> N;
  vector<ll> A;
  rep(i, N) {
    ll a;
    cin >> a;
    rep(j, a) { A.pb(i + 1); }
  }
  vector<vector<ll>> ans(H, vector<ll>(W));
  rep(i, H) {
    if (i % 2 == 0) {
      rep(j, W) { ans[i][j] = A[W * i + j]; }
    } else {
      rep(j, W) { ans[i][W - j - 1] = A[W * i + j]; }
    }
  }

  rep(i, H) {
    rep(j, W) { cout << ans[i][j] << " "; }
    cout << endl;
  }

  return 0;
}
Problem p03638 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
const int maxn = 107;
int a[maxn];
int d[maxn][maxn];
int n, m, c;
int main() {
  cin >> n >> m >> c;
  for (int i = 1; i <= c; i++)
    cin >> a[i];
  int k = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (i & 1) {
        if (a[k] > 0) {
          d[i][j] = k;
          a[k]--;
          if (a[k] == 0)
            k++;
        }
      } else {
        if (a[k] > 0) {
          d[i][m - j + 1] = k;
          a[k]--;
          if (a[k] == 0)
            k++;
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cout << d[i][j] << " ";
    }
    cout << endl;
  }
}


Fixed submission:
#include <iostream>
using namespace std;
const int maxn = 107;
int a[maxn * maxn];
int d[maxn][maxn];
int n, m, c;
int main() {
  cin >> n >> m >> c;
  for (int i = 1; i <= c; i++)
    cin >> a[i];
  int k = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (i & 1) {
        if (a[k] > 0) {
          d[i][j] = k;
          a[k]--;
          if (a[k] == 0)
            k++;
        }
      } else {
        if (a[k] > 0) {
          d[i][m - j + 1] = k;
          a[k]--;
          if (a[k] == 0)
            k++;
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cout << d[i][j] << " ";
    }
    cout << endl;
  }
}
Problem p03638 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int h, w, n;
  cin >> h >> w >> n;

  vector<int> a(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];

  vector<vector<int>> c(h, vector<int>(w));

  int x = 0, y = 0, dir = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < a[i]; ++j) {
      c[y][x] = i + 1;
      if (dir == 0) { // to right
        ++x;
        if (x == w) {
          ++y;
          x = w - 1;
          dir = 1;
        }
      } else { // to left
        --x;
        if (x == -1) {
          ++y;
          x = 0;
          dir = 1;
        }
      }
    }
  }

  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j)
      cout << c[i][j] << (j == w - 1 ? '\n' : ' ');

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int h, w, n;
  cin >> h >> w >> n;

  vector<int> a(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];

  vector<vector<int>> c(h, vector<int>(w));

  int x = 0, y = 0, dir = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < a[i]; ++j) {
      c[y][x] = i + 1;
      if (dir == 0) { // to right
        ++x;
        if (x == w) {
          ++y;
          x = w - 1;
          dir = 1;
        }
      } else { // to left
        --x;
        if (x == -1) {
          ++y;
          x = 0;
          dir = 0;
        }
      }
    }
  }

  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j)
      cout << c[i][j] << (j == w - 1 ? '\n' : ' ');

  return 0;
}