Problem:
 Score : 400 points    Problem Statement We have a grid with H rows and W columns of squares.
Snuke is painting these squares in colors 1 , 2 , ... , N .
Here, the following conditions should be satisfied:   For each i ( 1 ≤ i ≤ N ), there are exactly a_i squares painted in Color i . Here, a_1 + a_2 + ... + a_N = H W .  For each i ( 1 ≤ i ≤ N ), the squares painted in Color i are 4-connected . That is, every square painted in Color i can be reached from every square painted in Color i by repeatedly traveling to a horizontally or vertically adjacent square painted in Color i .   Find a way to paint the squares so that the conditions are satisfied.
It can be shown that a solution always exists.      Constraints  1 ≤ H, W ≤ 100  1 ≤ N ≤ H W  a_i ≥ 1  a_1 + a_2 + ... + a_N = H W         Input Input is given from Standard Input in the following format:  H  W  N  a_1  a_2  ...  a_N       Output Print one way to paint the squares that satisfies the conditions.
Output in the following format:  c_{1 1}  ...  c_{1 W}  :  c_{H 1}  ...  c_{H W}   Here, c_{i j} is the color of the square at the i -th row from the top and j -th column from the left.        Sample Input 1 2 2
3
2 1 1      Sample Output 1 1 1
2 3  Below is an example of an invalid solution:  1 2
3 1  This is because the squares painted in Color 1 are not 4-connected.       Sample Input 2 3 5
5
1 2 3 4 5      Sample Output 2 1 4 4 4 3
2 5 4 5 3
2 5 5 5 3       Sample Input 3 1 1
1
1      Sample Output 3 1

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 

from decimal import *
import numpy as np


H, W = MAP()
N = INT()
a = LIST()

A = []
for i in range(N):
	A += [i+1]*a[i]

ans = []
for i in range(H):
	ans.append(A[i*W:(i+1)*W])

for i in range(H):
	if i%2:
		ans[i] = ans[i][::-1]

for i in range(H):
	print(*ans[i])

Solution (Python):
import itertools
h,w=map(int,input().split())
n=int(input())
a=list(map(int,input().split()))

l=list(itertools.chain.from_iterable([[i+1]*a[i] for i in range(n)]))
c=0
for i in range(h):
    if c%2==0:
      print(*l[i*w:(i+1)*w])
    else:
      print(*(l[(i+1)*w-1:i*w-1:-1]))
    c+=1

Solution (Python):
H,W = map(int,input().split())
N = int(input())
A = list(map(int,input().split()))
check = []

for i,v in enumerate(A):
    check += [i + 1] * v

for i in range(H):
    if i % 2 ==0:
        print(*check[i*W:(i+1)*W])
    else:
        print(*check[i*W:(i+1)*W][::-1])

Solution (Python):
def readinput():
    h,w=list(map(int,input().split()))
    n=int(input())
    a=list(map(int,input().split()))
    return h,w,n,a

def main(h,w,n,a):
    cmat=[]
    for _ in range(h):
        cmat.append([0]*w)
    i=0
    j=0
    jstep=1
    for c in range(n):
        k=a[c]
        while k>0: 
            cmat[i][j]=c+1
            if (j==0 and jstep==-1) or (j==w-1 and jstep==1):
                i=i+1
                jstep=jstep*(-1)
            else:
                j=j+jstep
            k-=1
    return cmat

if __name__=='__main__':
    h,w,n,a=readinput()
    ans=main(h,w,n,a)
    for i in range(h):
        print(' '.join(map(str,ans[i])))

Solution (Python):
H,W = map(int,input().split())

N = int(input())

a = list(map(int,input().split()))

res = []
j = 0
st = 0 

for i in range(H):
    j = 0
    while j < W:
        while 0 < a[st] and j < W:
            res.append(str(st+1))
            j += 1
            a[st] -= 1
        if a[st] == 0:
            st += 1
    if i % 2 == 1:
        res.reverse()
    print(' '.join(res))
    res = []

Solution (Python):
h,w,n,*a = map(int,open(0).read().split())
b = []
for i in range(len(a)):
  b += [i+1]*a[i]
for i in range(h):
  if i % 2 == 0:
    print(*b[i*w:(i+1)*w])
  else:
    print(*b[i*w:(i+1)*w][::-1])

Solution (Python):
H, W = map(int, input().split())
N = int(input())
A = list(map(int, input().split()))

Ans = [[0] * W for _ in range(H)]

num = 1
cnt = A[0]
for i in range(H):
    for j in range(W):
        if cnt == 0:
            num += 1
            cnt = A[num-1]
        if i % 2 == 0:
            Ans[i][j] = num
            cnt -= 1
        else:
            k = W - j - 1
            Ans[i][k] = num
            cnt -= 1

for A in Ans:
    for i, a in enumerate(A):
        if i == W - 1:
            print(a)
        else:
            print(a, end=' ')

Solution (Python):
import math

H, W = map(int, input().split())
N = int(input())
a = list(map(int, input().split()))

res = [[0]*W for i in range(H)]
cur = [0, 0]

for i, ai in enumerate(a):
    while ai > 0:
        res[cur[0]][cur[1]] = (i + 1)
        if cur[1] % 2 == 0:
            if cur[0] == (H-1):
                cur[1] += 1
            else:
                cur[0] += 1
        else:
            if cur[0] == 0:
                cur[1] += 1
            else:
                cur[0] -= 1
        ai -= 1

str_n = int(math.log10(N)) + 2

for i in range(H):
    for j in range(W):
        print(str(res[i][j]).ljust(str_n), end="")
    print("")

Solution (Python):
h, w = map(int, input().split())
n = int(input())
A = list(map(int, input().split()))
grid = []
for i, num in enumerate(A, 1):
    grid += [i]*num


def split_list(l, n):
    for i in range(0, len(l), n):
        yield l[i:i+n]


ans = list(split_list(grid, w))
for i in range(h):
    if i % 2 == 0:
        print(*ans[i])
    else:
        print(*ans[i][::-1])

Solution (Python):
h,w=map(int,input().split())
n=int(input())
a=list(map(int,input().split()))

from collections import deque

d=deque([])
for i in range(n):
    number=i+1
    cnt=a[i]
    for _ in range(cnt):
        d.append(number)

ans=[[0]*w for _ in range(h)]

flg_h=0
i=0
j=0
while d:
    v=d.popleft()
    ans[i][j]=v
    if flg_h==0:
        if i<h-1:
            i+=1
        else:
            j+=1
            flg_h=1
    elif flg_h==1:
        if i>0:
            i-=1
        else:
            j+=1
            flg_h=0

for i in range(h):
    print(*ans[i])

Solution (Python):
from collections import deque

H, W = map(int, input().split())
N = int(input())

tmp = 0
rlt = []
h = deque([])

As = list(map(int, input().split()))

for k,i in enumerate(As):
  for _ in range(i):
    tmp += 1
    if ((tmp - 1) // W) % 2 == 0:
      h.append(k+1)
    else:
      h.appendleft(k+1)
    if tmp % W == 0:
      rlt.append(h)
      h = deque([])

for q in rlt:
  print(" ".join(map(str, q)))

Solution (Python):
H,W=list(map(int,input().split()))
N=int(input())
data=list(map(int,input().split()))
ans=[[0]*W for i in range(H)]
n_w,n_h=0,0
color=1
dire=1
for i in data:
   while i>0:
      if dire==1:
         i-=1
         ans[n_h][n_w]=color
         if n_w==W-1:
            dire=-1
            n_h+=1
         else:
            ans[n_h][n_w]=color
            n_w+=1
      else:
         i-=1
         ans[n_h][n_w]=color
         if n_w==0:
            dire=1
            n_h+=1
         else:
            ans[n_h][n_w]=color
            n_w-=1
   color+=1
for i in ans:
   print(" ".join(map(str,i)))

Solution (Python):
h, w, n, *aa = map(int, open(0).read().split())

line = list()

order = False

for a, i in zip(aa, range(1, len(aa)+1)):
    if order:
        line = line + [i]*a
    else:
        line = [i]*a + line
        
    while len(line) >= w:

        if order:
            p = line[:w]
            line = line[w:]
        else:
            p = line[-w:]
            line = line[:-w]
        order = not order
        print(*p)

Solution (Python):
h, w = map(int,input().split())
n = int(input())
a = list(map(int,input().split()))

color = []
for i in range(n):
    color += [i+1]*a[i]
    


ans = [[0]*w for _ in range(h)]

now = 0
for row in range(h):
    for col in range(w):
        if row%2 == 0:
            ans[row][col] = color[now]
        else:
            ans[row][-(col+1)] = color[now]
        now += 1
        
for r in ans:
    print(*r)

Solution (Python):
def main():
    
    H, W = map(int, input().split())
    _ = input()
    A = tuple(map(int, input().split()))

    
    color_seq = [0] * (H*W)
    now = 0
    for i, num in enumerate(A):
        for j in range(num):
            color_seq[now+j] = i+1
        now += num

    
    field = [[0]*W for _ in range(H)]
    for i in range(H):
        if i%2 == 0:
            for j in range(W):
                field[i][j] = color_seq[W*i + j]
        else:
            for j in range(W):
                field[i][W-1-j] = color_seq[W*i + j]
    
    
    for i in range(H):
        print(*field[i], sep=" ")

if __name__ == "__main__":
    main()

Solution (Python):
import sys
from collections import deque

H, W = map(int, input().split())
N = int(input())
A = list(map(int, sys.stdin.readline().rsplit()))

B = deque()
for i, a in enumerate(A):
    for j in range(a):
        B.append(i + 1)

res = [[-1] * W for _ in range(H)]
for i in range(H):
    if i % 2 == 0:
        for j in range(W):
            b = B.popleft()
            res[i][j] = b
    else:
        for j in reversed(range(W)):
            b = B.popleft()
            res[i][j] = b

for row in res:
    print(*row)

Solution (Python):
H, W = map(int, input().split())
N = int(input())
A = list(map(int, input().split()))

A = [0] + A


from collections import deque

i = 1
for h in range(H):
    C = deque([])
    for w in range(W):
        if h%2 == 0:
            C.append(str(i))
        else:
            C.appendleft(str(i))
        A[i] -= 1
        if A[i] == 0:
            i += 1
            
    print(' '.join(C))

Solution (Python):
from collections import deque
h,w = map(int, input().split())
n = int(input())
A = list(map(int, input().split()))

ans = deque()
for i,a in zip(list(range(1,n+1)), A):
    for _ in range(a):
        ans.append(i)
check = 0
for i in range(h):
    a = []
    for _ in range(w):
      a.append(ans.popleft())
    if check%2==0: print(" ".join(map(str,a)))
    else:print(" ".join(map(str,a[::-1])))
    check += 1

Solution (Python):
from collections import deque
from itertools import accumulate
from bisect import bisect_left
H, W = map(int, input().split())
N = int(input())
a = [int(i) for i in input().split()]
m = list(accumulate(a))

grid = []
for i in range(H+2):
    if i == 0 or i == H+1:
        grid.append('
    else:
        grid.append('
ans = [[0 for i in range(W+2)] for j in range(H+2)]
st = deque([])
st.append((1,1))
visited = [[0 for i in range(W+2)] for j in range(H+2)]
cur = 0
visited[1][1] = 1
while st:
    x, y = st.pop()
    
    cur += 1
    ans[y][x] = cur
    if grid[y+1][x] == '.'and visited[y+1][x] == 0:
        st.append((x, y+1))
        visited[y+1][x] = 1
    elif grid[y][x+1] == '.'and visited[y][x+1] == 0:
        st.append((x+1, y))
        visited[y][x+1] = 1
    elif grid[y-1][x] == '.'and visited[y-1][x] == 0:
        st.append((x, y-1))
        visited[y-1][x] = 1
    elif grid[y][x-1] == '.'and visited[y][x-1] == 0:
        st.append((x-1, y))
        visited[y][x-1] = 1
cur = 1
a = [[0 for i in range(W)] for j in range(H)]
for i in range(1,H+1):
    ls = []
    for j, x in enumerate(ans[i][1:-1], 0):
        a[i-1][j] = bisect_left(m, x)+1
for i in range(H):
    print(*a[i])

Solution (Python):
h,w=map(int,input().split())
n=int(input())
a=list(map(int, input().split()))

ans=[[0]*w for _ in range(h)]
color=0
cnt=0
for i in range(h):
    for j in range(w):
        if i%2==0:
            cnt+=1
            ans[i][j]=color+1
        else:
            cnt+=1
            ans[i][w-j-1]=color+1
        if cnt==a[color]:
            color+=1
            cnt=0

for ans_i in ans:
    print(*ans_i)

Solution (Python):
h,w=map(int,input().split())
n=int(input())
a=list(map(int,input().split()))
colors=[]
for i in range(n):
    for _ in range(a[i]):
        colors.append(i+1)
ans=[[0 for _ in range(w)] for _ in range(h)]
temp=0
for i in range(h):
    for j in range(w):
        if i%2!=0:
            j = w-j-1
        ans[i][j]=colors[temp]
        temp+=1

for i in range(h):
    for j in range(w):
        print(ans[i][j], end=" ")
    print()

Solution (Python):
H, W = map(int, input().split())
N = int(input())
a = list(map(int, input().split()))

ans_list = []
ans = []

for i,j in enumerate(a):
    tmp = [str(i+1)] * j
    ans_list.extend(tmp)

for _ in range(H//2):
    ans.append(ans_list[:W])
    ans_list = ans_list[W:]
    ans.append(list(reversed(ans_list[:W])))
    ans_list = ans_list[W:]

if H % 2 == 1:
    ans.append(ans_list)

for i in range(H):
    print(' '.join(ans[i]))

Solution (Python):
h, w = map(int, input().split())
n = int(input())
arr = [0] + list(map(int, input().split()))

res = []
for i in range(1, n+1):
    res.extend([i] * arr[i])

tmp = 0
for i in range(h):
    s = res[tmp:tmp+w]
    if i % 2 == 1:
        s.reverse()
    print(*s)
    tmp += w

Solution (Python):

from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

def main():
    H, W = map(int, input().split())
    N = int(input())
    a = list(map(int, input().split()))
    ans = [[None]*W for _ in range(H)]
    now = 0
    for i in range(N):
        while a[i] > 0:
            
            ans[now//W][now%W]=(i+1)
            now += 1
            a[i] -= 1
    for i in range(H):
        if i % 2 == 1:
            print(*ans[i][::-1])
        else:
            print(*ans[i])

if __name__ == "__main__":
    main()

Solution (Python):
h, w = map(int, input().split())
n = int(input())
color = list(map(int, input().split()))
colors = []
for c, num in enumerate(color):
    colors += [c+1]*num
mat = [[0]*w for _ in range(h)]
color_idx = 0

for row in range(h):
    if row %2 == 0:
        for col in range(w):
            mat[row][col] = colors[color_idx]
            color_idx += 1
    else:
        for col in range(w-1, -1, -1):
            mat[row][col] = colors[color_idx]
            color_idx += 1

for row in mat:
    print(' '.join(map(str, row)))

Solution (Python):
H, W = map(int, input().split())
N = int(input())
A = list(map(int, input().split()))
colors = []
for i in range(1, N + 1):
    colors += [i] * A[i - 1]
for i in range(H):
    if i % 2 == 0:
        print(*colors[i * W : (i + 1) * W])
    else:
        print(*colors[i * W : (i + 1) * W][::-1])

Solution (Python):
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))



H,W = LI()
N = I()
a = LI()

c = [[0]*W for _ in range(H)]
now = 0
for i in range(H):
    if i%2 == 0:
        for j in range(W):
            c[i][j] = now + 1
            a[now] -= 1
            if a[now] == 0:
                now += 1
    else:
        for j in range(W)[::-1]:
            c[i][j] = now + 1
            a[now] -= 1
            if a[now] == 0:
                now += 1

for i in range(H):
    print(*c[i])

Solution (Python):
H, W = map(int,input().split())
N = int(input())
al = list(map(int,input().split()))

ans = []
num = 1
for i in range(N):
    for _ in range(al[i]):
        ans.append(i+1)


left = 0

for i in range(H):
    if i % 2 == 0:
        row = ans[left:left+W]
    else:
        row = ans[left:left+W][::-1]
    print(*row)
    left += W

Solution (Python):
from collections import deque
h,w = map(int,input().split())
n = int(input())
A = list(map(int,input().split()))
dx = [0,1,0,-1]
dy = [1,0,-1,0]
ans = [["." for _ in range(w)]for _ in range(h)]
dq = deque([(0,0)])
i = 0
cnt = 0
while i != n:
    p,q = dq.pop()
    ans[q][p] = str(i+1)
    cnt += 1
    for j in range(4):
        nx = p + dx[j]
        ny = q + dy[j]
        if 0<=nx<=w-1 and 0<=ny<=h-1 and ans[ny][nx] == ".":
            dq.append((nx,ny))
            break
    if cnt == A[i]:
        cnt = 0
        i += 1
for i in ans:
    print(" ".join(i))

Solution (Python):
h,w=map(int,input().split())
n=int(input())
a=list(map(int,input().split()))
l=[]
for i in range(n):
 for j in range(a[i]):
  l.append(i+1)
for i in range(h):
 c=l[i*w:i*w+w]
 if i%2==0:
  print(*c)
 else:
  print(*c[::-1])

Solution (Python):
h,w = map(int,input().split())
n = int(input())
a = (list(map(int,input().split())))
l = []
for i in range(n):
    s = i+1
    t = a[i]
    
    for j in range(t):
        l.append(s)
for i in range(h):
    a = l[w*i:w*i+w]
    if i%2==0:
        print(*a)
    else:
        print(*a[::-1])

Solution (Python):
def solve():
    H,W = map(int, input().split())
    N = int(input())
    A = map(int, input().split())
    i,j = 0,0
    delta = 1
    ret = [[0]*W for _ in range(H)]
    for k,a in enumerate(A):
        while a > 0:
            ret[i][j] = k+1
            a -= 1
            if 0 <= j+delta < W:
                j += delta
            else:
                i += 1
                delta = delta*(-1)
                
    print('\n'.join([' '.join(map(str, r)) for r in ret]))
    
solve()

Solution (Python):
import sys

input = sys.stdin.readline


def update_coord(H, W, i, j):
    
    if i % 2 == 0:
        if j == W - 1:
            i += 1
        else:
            j += 1
    
    else:
        if j == 0:
            i += 1
        else:
            j -= 1
    return i, j


def main():
    H, W = map(int, input().split())
    N = int(input())
    a = list(map(int, input().split()))

    ans = [[0] * W for _ in range(H)]
    i, j = 0, 0
    for k, aa in enumerate(a):
        for _ in range(aa):
            ans[i][j] = k + 1
            i, j = update_coord(H, W, i, j)

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


if __name__ == "__main__":
    main()

Solution (Python):
import itertools as it

H, W = map(int, input().split())

N = int(input())

a = map(int, input().split())

grid = [[-1]*W for _ in range(H)]

h = 0
w = 0
rotation = it.cycle([(0, 1), (1, 0), (0, -1), (-1, 0)])
dy, dx = next(rotation)
for j, x in enumerate(a, 1):
    for _ in range(x):
        grid[h][w] = str(j)
        y = h + dy
        x = w + dx
        if not(0 <= y < H and 0 <= x < W) or grid[y][x] != -1:
            dy, dx = next(rotation)
            h += dy
            w += dx
        else:
            h = y
            w = x

for row in grid:
    print(" ".join(row))

Solution (Python):
h,w = map(int,input().split())
n = int(input())
a = list(map(int,input().split()))
s = ''
for i in range(1,n+1):
  s += (str(i)+' ')*a[i-1]
k = list(s.split())
for j in range(h):
  ans = k[j*w:(j+1)*w]
  if j%2 == 0:
    print(' '.join(ans))
  else:
    print(' '.join(ans[::-1]))

Solution (Python):


H, W = map(int, input().split())
N = int(input())
A = list(int(a) for a in input().split())
color = []
for i, a in enumerate(A, 1):
    color.extend([i] * a)

grid = [[0] * W for _ in range(H)]

k = 0
for i in range(H):
    for j in range(W):
        if i%2:
            j = (W - 1) - j
        grid[i][j] = color[k]
        k += 1

for i in range(H):
    print(' '.join(map(str, grid[i])))

Solution (Python):
from statistics import median


from fractions import gcd
from itertools import combinations 
from collections import deque
from collections import defaultdict
import bisect










import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
h,w = readInts()
n = I()
a = readInts()
i = 0
for j in range(h):
    row = []
    for _ in range(w):
        row += [i+1]
        a[i] -= 1
        if a[i] == 0:
            i += 1
    if j % 2 == 1:
        row = reversed(row)
    print(*row)

Solution (Python):

"""
Created on Fri Jun  5 09:50:00 2020

@author: NEC-PCuser
"""

H, W = map(int, input().split())
N = int(input())
a = list(map(int, input().split()))

gazo = [[0] * W  for i in range(H)]

count = 0
start_H = 0
start_W = 0
direction = 0
for i in range(len(a)):
    for j in range(a[i]):
        gazo[start_H][start_W] = i + 1
        if (start_W < W - 1 and direction == 0):
            start_W += 1
        elif (start_W == W - 1 and direction == 0):
            start_H += 1
            direction = 1
        elif (start_W > 0 and direction == 1):
            start_W -= 1
        elif (start_W == 0 and direction == 1):
            start_H += 1
            direction = 0
    
for i in range(len(gazo)):
    print(' '.join(map(str, gazo[i])))

Solution (Python):
h,w,n,*a=map(int,open(0).read().split())
b=[]
for i in range(n):
    b+=[i+1]*a[i]
for i in range(0,h*w,w):
    if i//w%2==1:
        print(*b[i:i+w])
    else:
        print(*b[i:i+w][::-1])

Solution (Python):
h, w = map(int, input().split())
n = int(input())
li = list(map(int, input().split()))
line = 1
L =[]
for i, a in enumerate(li, 1):
    while a > 0:
        L.append(i)
        a -= 1
        if len(L) == w:
            if line % 2:
                print(*L)
            else:
                print(*reversed(L))
            L = []
            line += 1

Solution (Python):
from collections import deque
import itertools

H , W = map(int,input().split())
N = int(input())
a = list(map(int,input().split()))
acu = [0] + list(itertools.accumulate(a))
ans = [[0] * W for _ in range(H)]
cnt = 1
jdg = 1

for h in range(H):
    if h % 2 == 1:
        for w in range(W):
            
            ans[h][w] = jdg
            if cnt >= acu[jdg]:
                jdg += 1
            cnt += 1
    if h % 2 == 0:
        for w in reversed(range(W)):
            
            ans[h][w] = jdg
            if cnt >= acu[jdg]:
                jdg += 1
            cnt += 1
for i in range(H):
    print(*ans[i])

Solution (Python):
h, w = map(int, input().split())
maze = []
for _ in range(h):
    l = [None] * w
    maze.append(l)
n = int(input())
dat = list(map(int, input().split()))

curcol = 0
curpasted = 0

for hh in range(h):
    if hh % 2 == 0:
        for ww in range(0, w):
            maze[hh][ww] = curcol + 1
            curpasted += 1
            if curpasted >= dat[curcol]:
                curcol += 1
                curpasted = 0
    else:
        for ww in range(w-1, -1, -1):
            maze[hh][ww] = curcol + 1
            curpasted += 1
            if curpasted >= dat[curcol]:
                curcol += 1
                curpasted = 0
from pprint import pprint
for i in range(h):
    print(" ".join(list(map(str, maze[i]))))

Solution (Python):
h,w=map(int,input().split())
n=int(input())
a=list(map(int,input().split()))
p=list()
for i in range(n):
    p.extend([i+1 for j in range(a[i])])

for i in range(h):
    if i%2==0:
        print(*p[i*w:i*w+w])
    else:
        print(*p[i*w:i*w+w][::-1])

Solution (Python):
import numpy as np

H, W = map(int, input().split())
N = int(input())
a = list(map(int, input().split()))
ans = []

for i, v in enumerate(a):
    for _ in range(v):
        ans.append(i+1)
ans = np.array(ans).reshape(H, W)

for i in range(H):
    if i%2 == 1:
        ans[i] = ans[i][::-1]

for j in ans:
    print(*list(j))

Solution (Python):
import numpy as np
h, w = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))

arr = []
for i in range(1, n + 1):
    for j in range(a[i - 1]):
        arr.append(i)

arr = np.array(arr)
arr = arr.reshape(h, -1)

for i in range(h):
    if i % 2 == 0:
        print(*arr[i])
    else:
        print(*arr[i][::-1])

Solution (Python):
import sys


import math
import collections

import bisect
import itertools
import fractions

import copy
import heapq
import decimal

import queue

sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))





def main():
    h, w = ns()
    n = ni()
    a = na()

    mat = [[-1 for i in range(w)] for j in range(h)]

    toLeft = True
    idx = 0
    for i in range(h):
        toLeft = not toLeft
        for j in range(w):
            a[idx] -= 1
            dirc = -1 if toLeft else 1
            adj = -1 if toLeft else 0
            mat[i][dirc * j + adj] = idx + 1
            if a[idx] == 0:
                idx += 1

    for m in mat:
        print(*m, sep=" ")


if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline


def read():
    H, W = map(int, input().strip().split())
    N = int(input().strip())
    A = list(map(int, input().strip().split()))
    return H, W, N, A


def solve(H, W, N, A):
    X = []
    for i in range(N):
        X.extend([i+1] * A[i])
    for i in range(H):
        if i % 2 == 1:
            line = X[W*i:W*(i+1)][::-1]
        else:
            line = X[W*i:W*(i+1)]
        print(' '.join(map(str, line)))


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))

Solution (Python):
H,W = map(int,input().split())
N=int(input())
a=list(map(int,input().split()))
acnt=cnt=0
Hcnt=Wcnt=0
ans = [[str(-1)]*W for i in range(H)]

for i in range(W*H):
    ans[Hcnt][Wcnt]=str(acnt+1)
    if Hcnt % 2 ==0:
        if Wcnt == W-1:
            Hcnt+=1
        else:
            Wcnt+=1
    else:
        if Wcnt == 0:
            Hcnt+=1
        else:
            Wcnt-=1

    cnt+=1
    if cnt == a[acnt]:
        cnt=0
        acnt+=1

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

Solution (Python):
from sys import stdin

def main():
    input = stdin.readline
    h, w = map(int, input().split())
    n = int(input())
    a = list(map(int, input().split()))
    m = [[0] * w for _ in range(h)]
    x, y, LR, v = 0, 0, 1, 0
    for n in a:
        v += 1
        for _ in range(n):
            m[y][x] = str(v)
            x += LR
            if x == w:
                y += 1
                x = w - 1
                LR = -1
            elif x < 0:
                y += 1
                x = 0
                LR = 1
    for i in range(h):
        print(" ".join(m[i]))
main()

Solution (Python):
from sys import stdin

def main():
    input = stdin.readline
    h, w = map(int, input().split())
    n = int(input())
    a = list(map(int, input().split()))
    m = [[0] * w for _ in range(h)]
    x, y, LR, v = 0, 0, 1, 0
    for n in a:
        v += 1
        for _ in range(n):
            m[y][x] = str(v)
            x += LR
            if x == w:
                y += 1
                x = w - 1
                LR = -1
            elif x < 0:
                y += 1
                x = 0
                LR = 1
    for i in range(h):
        print(" ".join(m[i]))
main()

Solution (Python):
import math
import bisect
from queue import Queue
from collections import deque

h, w = [int(i) for i in input().split()]
n = int(input())
a = [int(i) for i in input().split()]

table = [[0 for _ in range(w)] for _ in range(h)]

x = 0
y = 0
z = 0
s = 0


for i in range(h*w):
    table[y][x] = z + 1

    if(s < a[z]-1):
        s += 1
    else:
        s = 0
        z += 1

    if(y % 2 == 0):
        if(x == w-1):
            y += 1
        else:
            x += 1
    else:
        if(x == 0):
            y += 1
        else:
            x -= 1

for i in range(h):
    L = [str(j) for j in table[i]]
    L = " ".join(L)
    print(L)

Solution (Python):
h, w = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))

colors = [[0] * w for _ in range(h)]

i = j = 0
foward = True
for k in range(len(a)):
    num = a[k]
    while num > 0:
        colors[i][j] = k + 1
        if foward is True:
            j += 1
            if j == len(colors[0]):
                j -= 1
                i += 1
                foward = False
        else:
            j -= 1
            if j == -1:
                j += 1
                i += 1
                foward = True
        num -= 1

for i in range(len(colors)):
    for j in range(len(colors[0])):
        print(colors[i][j], end=' ')
    print()

Solution (Python):
h,w = map(int,input().split())
n = int(input())
a = list(map(int,input().split()))
i = 0
ans = [[0 for _ in range(w)] for _ in range(h)]
for j in range(n):
  for _ in range(a[j]):
    hi = i//w
    if hi%2 == 0:
      ans[hi][i%w] = j+1
    else:
      ans[hi][w-1-i%w] = j+1
    i += 1
for ansi in ans:
  for j in range(w-1):
    print(ansi[j],end=" ")
  print(ansi[w-1])

Solution (Python):
h, w = map(int, input().split())
n = int(input())
a = [0] + list(map(int, input().split()))

ans = [[] for _ in range(h)]
k = 1
for i in range(h):
    row = []
    for j in range(w):
        if not a[k]: k += 1
        row.append(k)
        a[k] -= 1
    if i % 2 == 1:
        ans[i] = list(reversed(row))
    else:
        ans[i] = row

for i in range(h):
    print(*ans[i])

Solution (Python):
H,W = map(int,input().split())
N = int(input())
A = list(map(int,input().split()))

ans = [[None] * W for i in range(H)]
AA = []
for i, a in enumerate(A):
    AA.append((a,i))
AA.sort(reverse=True)
now_num = 0
now_row = 0
now_col = 0
for i, a in enumerate(AA):
    num = a[0]
    while num > 0:
        flg = False
        for j in range(now_col, W):
            
            if num != 0:
                num -= 1
                ans[now_row][j] = str(a[1]+1)
            else:
                flg = True
                break
        if flg:
            now_col = j
            break
        else:
            now_col = 0
            now_row += 1

for i in range(1,H,2):
    ans[i] = ans[i][::-1]

for i in range(H):
    print(' '.join(ans[i]))

Solution (Python):

h,w=map(int,input().split() )
n=int(input())
a=list(map(int,input().split()))


import numpy as np

lis=[]
for i in range(n):
    lis.extend([i+1]*a[i])

lis = np.array(lis).reshape((h,w))

for i in range(1,h,2):
    lis[i] = lis[i][::-1]

for i in range(h):
    print(*lis[i])

Solution (Python):
import sys

readline = sys.stdin.readline

def main():
    H, W = map(int, readline().split())
    N = int(readline())
    a = list(map(int, readline().split()))

    ans = [[0] * W for _ in range(H)]
    h = 0; w = 0
    popflag = True
    backflag = False
    for _ in range(H*W):
        if popflag:
            tmp = a.pop()
            tmplist = [len(a)+1]*tmp
            popflag = False
        if backflag:
            ans[h][w] = tmplist.pop()
            if w-1 == -1:
                backflag = False
                h += 1
            else:
                w -= 1
        else:
            ans[h][w] = tmplist.pop()
            if w+1 == W:
                backflag = True
                h += 1
            else:
                w += 1
        if len(tmplist) == 0:
            popflag = True
    
    for i in range(H):
        print(*ans[i])

if __name__ == "__main__":
    main()

Solution (Python):
H, W = map(int, input().split())
N = int(input())
a = list(map(int, input().split()))

l = []

for i in range(N):
    l += [i+1]*a[i]

for i in range(H):
    row = l[W*i:W*i+W]
    if i%2:
        row = row[::-1]

    for v in row:
        print(v, end=' ')
    print()