Problem p03640 (python)

Fixed submission:
import sys
import numpy as np


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(10 ** 9)


def main():
    H, W = map(int, input().split())
    N = int(input())
    A = list(map(int, input().split()))
    C = np.zeros(H * W, dtype=np.int32)
    first = 0
    for i, a in enumerate(A, 1):
        C[first : first + a] = i
        first += a

    ans = C.reshape((H, W))
    ans[1::2, :] = ans[1::2, ::-1]

    for i in range(H):
        print(" ".join(ans[i].astype(str)))


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(10 ** 9)


def main():
    H, W = map(int, input().split())
    N = int(input())
    A = list(map(int, input().split()))
    C = [[0 for _ in range(W)] for _ in range(H)]
    h = 0
    w = 0
    flag = True
    for i in range(N):
        a = A[i]
        if flag:
            if w == W:
                h += 1
                for j in range(a):
                    C[h][j] = i + 1
                w = a
            elif w + a <= W:
                for j in range(w, w + a):
                    C[h][j] = i + 1
                w += a
            else:
                b = W - w
                for j in range(w, W):
                    C[h][j] = i + 1
                h += 1
                for j in range(a - b):
                    C[h][W - j - 1] = i + 1
                flag = False
                w = W - (a - b)
        else:
            if w == 0:
                h += 1
                for j in range(a):
                    C[h][j] = i + 1
                w = a
                flag = True
            elif w - a >= 0:
                for j in range(w - a, a):
                    C[h][j] = i + 1
                w -= a
            else:
                for j in range(w):
                    C[h][j] = i + 1
                h += 1
                for j in range(a - w):
                    C[h][j] = i + 1
                flag = True
                w = a - w
    for i in range(H):
        print(*C[i], sep=" ")


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(10 ** 9)


def main():
    H, W = map(int, input().split())
    N = int(input())
    A = list(map(int, input().split()))
    C = [[0 for _ in range(W)] for _ in range(H)]
    h = 0
    w = 0
    flag = True
    for i in range(N):
        a = A[i]
        if flag:
            if w == W:
                h += 1
                for j in range(a):
                    C[h][j] = i + 1
                w = a
            elif w + a <= W:
                for j in range(w, w + a):
                    C[h][j] = i + 1
                w += a
            else:
                b = W - w
                for j in range(w, W):
                    C[h][j] = i + 1
                h += 1
                for j in range(a - b):
                    C[h][W - j - 1] = i + 1
                flag = False
                w = W - (a - b)
        else:
            if w == 0:
                h += 1
                for j in range(a):
                    C[h][j] = i + 1
            elif w - a >= 0:
                for j in range(w - a, a):
                    C[h][j] = i + 1
                w -= a
            else:
                for j in range(w):
                    C[h][j] = i + 1
                h += 1
                for j in range(a - w):
                    C[h][j] = i + 1
                flag = True
                w = a - w
    for i in range(H):
        print(*C[i], sep=" ")


if __name__ == "__main__":
    main()