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):
h, w = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))

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

ans = [[0 for i in range(w)] for j in range(h)]

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

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

Solution (Python):

from collections import deque
H, W = map(int, input().split())
N = int(input())
A = list(map(int, input().split()))

ans_list = [[0] * W for _ in range(H)]
ans = 0
cnt = 0
for h in range(H):
    if h % 2 == 0:
        for w in range(W):
            if cnt == A[ans]:
                cnt = 0
                ans += 1
            ans_list[h][w] = ans + 1
            cnt += 1
    else:
        for w in range(W-1, -1, -1):
            if cnt == A[ans]:
                cnt = 0
                ans += 1
            ans_list[h][w] = ans + 1
            cnt += 1

for a in ans_list:
    print(*a)

Solution (Python):
H, W = map(int, input().split())
N = int(input())
cells = []
for idx, a in enumerate(map(int, input().split())):
    cells.extend([idx + 1 for _ in range(a)])

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

Solution (Python):
def printans(x):
  for h in range(1, H+1):
    for w in range(1, W+1):
      if w > 1:
        print(' ', end='')
      print(x[h][w], end='')
    print()
		
H, W = map(int, input().split())
N = int(input())
a = list(map(int, input().split()))
a.insert(0, -1)

g = [[-1] * (W+2) for x in range(H+2)]
v = 1
for h in range(1, H+1):
  if h%2 != 0:
    for w in range(1, W+1):
      g[h][w] = v
      a[v] -= 1
      if a[v] == 0:
        v += 1
  else:
    for w in range(W, 0, -1):
      g[h][w] = v
      a[v] -= 1
      if a[v] == 0:
        v += 1

printans(g)

Solution (Python):
def printans(x):
	for h in range(1, H+1):
		for w in range(1, W+1):
			if w > 1:
				print(' ', end='')
			print(x[h][w], end='')
		print()
		
H, W = map(int, input().split())
N = int(input())
a = list(map(int, input().split()))
a.insert(0, -1)

g = [[-1] * (W+2) for x in range(H+2)]
v = 1
for h in range(1, H+1):
	if h%2 != 0:
		for w in range(1, W+1):
			g[h][w] = v
			a[v] -= 1
			if a[v] == 0:
				v += 1
	else:
		for w in range(W, 0, -1):
			g[h][w] = v
			a[v] -= 1
			if a[v] == 0:
				v += 1
				
printans(g)

Solution (Python):
def printans(x):
	for h in range(1, H+1):
		for w in range(1, W+1):
			if w > 1:
				print(' ', end='')
			print(x[h][w], end='')
		print()
		
H, W = map(int, input().split())
N = int(input())
a = list(map(int, input().split()))
a.insert(0, -1)

g = [[-1] * (W+2) for x in range(H+2)]
v = 1
for h in range(1, H+1):
	if h%2 != 0:
		for w in range(1, W+1):
			g[h][w] = v
			a[v] -= 1
			if a[v] == 0:
				v += 1
	else:
		for w in range(W, 0, -1):
			g[h][w] = v
			a[v] -= 1
			if a[v] == 0:
				v += 1
				
printans(g)

Solution (Python):
H, W = map(int, input().split())
N = int(input())
a = list(map(int, input().split()))
answer = [[0] * W] * H

input_list = []
for i in range(1, N+1):
    for _ in range(a[i-1]):
        input_list.append(i)

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

Solution (Python):
h,w = map(int,input().split())
n = int(input())
a = [int(i) for i in input().split()]
ans = [[0 for _ in range(w)] for _ in range(h)]
cnt = 0
idx = 0
color = 1

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

for i in range(h):
    for j in range(w):
        if j != w-1:
            print(ans[i][j],end=" ")
        else:
            print(ans[i][j])

Solution (Python):
H, W = map(int, input().split())
N = int(input())
A = list(map(int, input().split()))
L = sum([[i]*a for i, a in enumerate(A, 1)], [])
for i in range(H):
    inv = (-1)**(i&1)
    print(*L[i*W:(i+1)*W][::inv])

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

x = 0
for i in range(H):
    ans = []
    for j in range(W):
        ans.append(x+1)
        A[x] -= 1
        if A[x] == 0:
            x += 1
    if i % 2 == 0:
        print(*ans)
    else:
        print(*ans[::-1])

Solution (Python):
H,W=map(int,input().split())
N=int(input())
A=list(map(int,input().split()))
A_dic={i+1:A[i] for i in range(N)}
Squares=[[0 for i in range(W)] for j in range(H)]

step_w=1
w,h,cnt=0,0,0
for k,v in A_dic.items():
    for i in range(v):
        cnt+=1
        Squares[h][w]=str(k)
        if cnt%W==0:
            h+=1
            step_w*=(-1)
        else:
            w+=step_w

for l in Squares:
    print(' '.join(l))

Solution (Python):
h,w = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))
now = 0
for i in range(h):
  ans = []
  res = w
  while a[now] < res:
    ans += [now+1 for i in range(a[now])]
    res -= a[now]
    now += 1
  else:
    ans += [now+1 for i in range(res)]
    a[now] -= res
  if i % 2:
    ans.reverse()
  print(*ans)

Solution (Python):
import sys
input = sys.stdin.readline
H,W=map(int,input().split())
n=int(input())
ans=[[0]*W for _ in range(H)]

A=list(map(int,input().split()))
cnt=0
for a,num in enumerate(A):
    
    for i in range(num):
        h=cnt//W
        w=cnt %W
        if h%2==1:
            w=w*(-1)-1
        ans[h][w]=a+1
        cnt+=1
for a in ans:
    print(*a)

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

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

    else:
        for j in range(h):
            if a[ind] == 0:
                ind += 1
            ans[j][i] = ind+1
            a[ind] -= 1

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

Solution (Python):
H, W, N, *a = map(int, open(0).read().split())
l = []
for i in range(N):
  l += [i+1]*a[i]
for i in range(H):
  print(*l[i*W:(i+1)*W][::1-i%2*2])

Solution (Python):
def d_grid_coloring():
    H, W = [int(i) for i in input().split()]
    N = int(input())
    A = [int(i) for i in input().split()]

    
    
    
    tmp = []
    for k, a in enumerate(A, 1):
        tmp.extend([k] * a)
    ans = [tmp[r * W:(r + 1) * W][::(-1)**r] for r in range(H)]
    return '\n'.join(' '.join(map(str, row)) for row in ans)

print(d_grid_coloring())

Solution (Python):
H, W = map(int, input().split())
N = int(input())
alist = [[a, i+1] for i, a in enumerate(map(int, input().split()))]
alist.sort()
drow_Order = []
for x in alist:
  for a in range(x[0]):
    drow_Order += [x[1]]
ans = ''
aaa = 0
for row in range(H):
  if row%2==0:
    ans = map(str, drow_Order[row*W:(row+1)*W])
  else:
    ans = map(str, reversed(drow_Order[row*W:(row+1)*W]))
  ans = ','.join(ans)
  print(ans.replace(',', ' '))

Solution (Python):



H, W = map(int, input().split())
N = int(input())
alist = [[a, i+1] for i, a in enumerate(map(int, input().split()))]
alist.sort()
drow_Order = []
for x in alist:
  for a in range(x[0]):
    drow_Order += [x[1]]
ans = ''
aaa = 0
for row in range(H):
  if row%2==0:
    ans = map(str, drow_Order[row*W:(row+1)*W])
  else:
    ans = map(str, reversed(drow_Order[row*W:(row+1)*W]))
  ans = ','.join(ans)
  print(ans.replace(',', ' '))

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

Solution (Python):
H,W=map(int,input().split(' '))
N=int(input())
a=list(map(int,input().split(' ')))
l = []
ans = []
for n,i in enumerate(a):
    for j in range(i):
        l.append(n+1)
for i in range(H):
    tmp = []
    if i%2==0:
        for j in range(W):
           tmp.append(l[i*W+j])
    else:
        for j in range(W-1,-1,-1):
            tmp.append(l[i*W+j])
    ans.append(tmp)
for i in ans:
    for j in i[:-1]:
        print(j,"",end="")
    print(i[-1])

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

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

now = 0
for color, count in enumerate(A, 1):
    for idx in range(now, now+count):
        y = idx // w
        x = idx % w
        if y % 2 == 1:
            x = -x-1
        ans[y][x] = color

    now += count

for line in ans:
    print(*line)

Solution (Python):
H,W = map(int,input().split())
N = int(input())
a = [i for i in map(int,input().split())]

ans = [[0 for i in range(W)] for j in range(H)]
x=0
y=0
color=0
reverse=0

while y<H:
  ans[y][x] = color+1
  a[color]-=1
  if a[color]==0:
    color+=1
  if (reverse==0 and x==W-1) or (reverse==1 and x==0):
    reverse = 1-reverse
    y+=1
  else:
    if reverse==0:
      x+=1
    else:
      x-=1
    
for i in range(H):
  print(" ".join(map(str,ans[i])))

Solution (Python):
H, W = map(int, input().split())
N = int(input())
a = [int(i) for i in input().split()]
ary = [[0]*W for _ in range(H)]
s = 1
h, w = 0, 0
for i, n in enumerate(a):
    while n > 0:
        ary[h][w] = i+1
        w += s
        if w < 0:
            w = 0
            s = 1
            h += 1
        elif w >= W:
            h += 1
            w = W-1
            s = -1
        n -= 1
for a in ary:
    print(*a)

Solution (Python):
H,W = map(int,input().split())
N = int(input())
Data = list(map(int,input().split()))
Ans = [[0]*W for _ in range(H)]

w = 0
h = 0
direction = 1
num = 0
for d in Data:
    num += 1
    for _ in range(d):
        Ans[h][w] = num

        if direction == 1:
            if w == W-1:
                direction = 2
                h += 1
            else:
                w += 1
        else:
            if w == 0:
                direction = 1
                h += 1
            else:
                w -= 1

for h in range(H):
    for w in range(W):
        print( Ans[h][w], end = ' ')
    print()

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

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


B = []
for i, a in enumerate(A):
    for _ in range(a):
        B.append(i + 1)

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

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

Solution (Python):
import sys
bbn=1000000007
	

		
def main():
	h, w = map(int, input().split())
	n = int(input())
	al = list(map(int, input().split()))
	
	al_i=0
	rr=[]
	for hi in range(h):
		ww=[-1]*w
		for wj in range(w):
			ww[wj] = al_i+1
			al[al_i] -= 1
			if al[al_i]==0:
				al_i+=1
		rr.append(ww)
		
	for i, r in enumerate(rr):
		b = r[:] if i % 2 == 0 else r[::-1]
		print(' '.join([str(v) for v in b]))
		
	
	

isTest=False

def pa(v):
	if isTest:
		print(v)

if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
		isTest=True
	else:
		pass
		
			
	ret = main()
	if ret is not None:
		print(ret)

Solution (Python):

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


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


cnt = 0
c = 0
for i in range(H):
    if i % 2 == 0:
        for j in range(W):
            ans[i][j] = c+1
            A[c] -= 1
            if A[c] == 0:
                c += 1
    else:
        for j in reversed(range(W)):
            ans[i][j] = c+1
            A[c] -= 1
            if A[c] == 0:
                c += 1

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 = []
num = 1
count = 0
for i in range(H):
    tmp = []
    if i % 2 == 0:
        for j in range(W):
            tmp.append(str(num))
            count += 1
            if count == a[num - 1]:
                num += 1
                count = 0
        ans.append(tmp)
    else:
        for j in range(W):
            tmp.insert(0, str(num))
            count += 1
            if count == a[num - 1]:
                num += 1
                count = 0
        ans.append(tmp)


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

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

ans = [[0 for w in range(W)] for h in range(H)]

for h in range(H):
    if h%2 == 0:
        for w in range(W):
            ans[h][w] = N
            A[-1] -= 1
            if A[-1] == 0:
                A.pop(-1)
                N -= 1
    else:
        for w in range(1,W+1):
            ans[h][(-1)*w] = N
            A[-1] -= 1
            if A[-1] == 0:
                A.pop(-1)
                N -= 1

for h in range(H):
    ans[h] = map(str,ans[h])
    print(' '.join(ans[h]))

Solution (Python):
import sys
import copy
sys.setrecursionlimit(10**6)
if sys.platform in (['ios','darwin','win32']):
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

def main():
	H, W = MAP()
	N = INT()
	A = MAP()
	
	
	M = [[0] * W for _ in range(H)]
	

	MOVE = [[0,1],[1,0],[0,-1],[-1,0]]

	h, w = 0, 0
	M[0][0] = 1
	A[0] -= 1
	direct = 0
	for i, a in enumerate(A):
		num = i+1
		cnt = a
		if not cnt: continue
		fin = False
		while True:
			for i, move in enumerate(MOVE):
				if i < direct:
					continue
				else:
					direct = 0
					dh, dw = move
				while h+dh < H and h+dh >= 0 and w+dw < W and w+dw >= 0 and M[h+dh][w+dw] == 0:
					h += dh
					w += dw
					M[h][w] = num
					cnt -= 1
					if cnt == 0:
						direct = MOVE.index([dh, dw])
						fin = True
						break
				if fin:	break
			if fin:	break

	for m in M:
		print(' '.join(map(str, m)))

if __name__ == '__main__':
	main()

Solution (Python):
H, W = map(int, input().split())
N = int(input())
A = list(map(int, input().split()))
B = []
for i, a in enumerate(A):
    B.append((a, i+1))

A = sorted(B)
i, j = 0, 0
C = [[0]*W for _ in range(H)]
flag = True
for k in range(N):
    num, color = A[k]
    while num:
        C[i][j] = color
        if flag:
            if j < W-1:
                j += 1
            else:
                i += 1
                flag = False
        else:
            if j > 0:
                j -= 1
            else:
                i += 1
                flag = True
        num -= 1

for k in range(H):
    print(*C[k])

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

hw=[[0 for i in range(w)] for i in range(h)]

k=0
for i in range(h):
    if i%2==0:
        for j in range(w):
            if a[k]==0:
                k+=1
            hw[i][j]=k+1
            a[k]-=1
    else:
        for j in range(w-1,-1,-1):
            if a[k]==0:
                k+=1
            hw[i][j]=k+1
            a[k]-=1
for i in range(h):
    print(" ".join(map(str,hw[i])))

Solution (Python):
import sys

H, W, N, *cnt_color = map(int, sys.stdin.read().split())

def main():
    canvas = [[None] * W for _ in range(H)]

    h = w = 0
    for i in range(N):
        c = cnt_color[i]
        i = str(i+1)
        for _ in range(c):
            canvas[h][w] = i
            if h & 1:
                w -= 1
                if w == -1:
                    h += 1
                    w = 0
            else:
                w += 1
                if w == W:
                    h += 1
                    w = W - 1
    
    for i in range(H):
        yield' '.join(canvas[i])

if __name__ == '__main__':
    ans = main()
    print(*ans, sep='\n')

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

ansli=[]
ans=[]
for i in range(n):
  for j in range(a[i]):
    ansli.append(i+1)

for i in range(h):
  temp=ansli[0+i*w:(i+1)*w]
  if not i%2:
    ans.append(temp)
  else:
    ans.append(temp[::-1])

for i in ans:
  print(*i)

Solution (Python):
from itertools import accumulate
H,W = map(int, input().split(' '))
n = int(input())
a = list(map(int, input().split(' ')))
ac = list(accumulate(a))
ansinv = [[0 for i in range(W)] for i in range(H)]
r = 1
k = 1
for i in range(H*W):
    if k > ac[r-1]:
        r += 1
    x = i//W
    y = i % W
    ansinv[x][y] = r
    k += 1
ans = ansinv
for i in range(H):
    if i % 2:
        ans[i] = ans[i][::-1]
for i in ans:
    print(*i, sep = " ")

Solution (Python):
import numpy as np

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

canvas = np.zeros((H * W, ), dtype=np.object)
idx = 0

for color, a in enumerate(A):
    for _ in range(a):
        canvas[idx] = str(color + 1)
        idx += 1

for r, row in enumerate(canvas.reshape((H, W))):
    print(" ".join(row if not r % 2 else row[::-1]))

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

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

idx = 0
for color, a in enumerate(al):
    for i in range(a):
        yoko = idx % w
        tate = idx // w
        if tate % 2 == 1:
            yoko = w - yoko - 1 
        ansl[tate][yoko] = str(color+1)
        idx += 1

for ans in ansl:
    print(" ".join(ans))

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

Solution (Python):
H, W = map(int, input().split())
N = int(input())
A = list(map(int, input().split()))
M = [[0 for i in range(W)] for j in range(H)]
p = [0, 0]
x = 0
for i in range(N):
  for j in range(A[i]):
    M[p[0]][p[1]] = i+1
    x += 1
    if x % W == 0:
      p[0] += 1
    else:
      if (x // W) % 2 == 0:
        p[1] += 1
      else:
        p[1] -= 1
for i in range(H):
  for j in range(W-1):
    print(M[i][j], end = ' ')
  print(M[i][W-1])

Solution (Python):
H,W=map(int,input().split())
N=int(input())
A=list(map(int,input().split()))
ans0=[]
for i in range(N):
        ans0+=[i+1]*A[i]
ans0=ans0[::-1]

ans1=[[] for i in range(H)]
for i in range(H):
    for j in range(W):
        ans1[i].append(ans0.pop())

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

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

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

na = 0
ia = 0
for h in range(H):
    l = []
    for w in range(W):
        if na==0:
            na = A[ia]
            ia += 1
        l.append(ia)
        na -= 1
    if h%2==1: l = l[::-1]
    print(*l)

Solution (Python):
h,w=map(int,input().split())
n=int(input())
a=[int(i) for i in input().split()]
hw=[[0 for i in range(w)] for j in range(h)]

k=0
for i in range(h):
    if i%2==0:
        for j in range(w):
            if a[k]!=0:
                hw[i][j]=k+1
                a[k]-=1
            else:
                k+=1
                hw[i][j]=k+1
                a[k]-=1
    else:
        for j in range(w-1,-1,-1):
            if a[k]!=0:
                hw[i][j]=k+1
                a[k]-=1
            else:
                k+=1
                hw[i][j]=k+1
                a[k]-=1
for i in range(h):
    for j in range(w):
        if j==w-1:
            print(hw[i][j])
        else:
            print(hw[i][j],end=" ")

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    h,w=MI()
    n=int(input())
    aa=LI()
    i=j=0
    ans=[[0]*w for _ in range(h)]
    for c,a in enumerate(aa,1):
        for _ in range(a):
            ans[i][j]=c
            if i%2:j-=1
            else:j+=1
            if j==-1:
                i+=1
                j=0
            if j==w:
                i+=1
                j=w-1
    for ar in ans:
        print(*ar)
main()

Solution (Python):
import sys
input = sys.stdin.readline
H, W = [int(x) for x in input().strip().split()]
N = int(input())
A = [int(x) for x in input().strip().split()]
M = [[-1] + [0] * (W) + [-1] for _ in range(H+2)]
M[0] = [-1] * (W+2)
M[-1] = [-1] * (W+2)

vec = [(0, 1), (1, 0), (0, -1), (-1, 0)]

k = 0
h, w = 1, 1
for i, a in enumerate(A, start=1):
    for _ in range(a):
        M[h][w] = i
        if M[h+vec[k][0]][w+vec[k][1]] != 0:
            k = (k + 1) % 4
        h += vec[k][0]
        w += vec[k][1]

for m in M[1:len(M)-1]:
    print(*m[1:len(m)-1])

Solution (Python):
H, W = map(int, input().split())
N = int(input())
A = [int(a) for a in input().split()]
X = [[-1] * W for _ in range(H)]
k = 0
c = 0
for i in range(H):
    for j in range(W)[::(-1 if i&1 else 1)]:
        X[i][j] = k+1
        c += 1
        if c >= A[k] and k < N-1:
            k += 1
            c = 0
for x in X:
    print(*x)

Solution (Python):
H, W = map(int, input().split())
N = int(input())
a = list(map(int, input().split()))
ans = [list([0] * W) for _ in range(H)]

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

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

Solution (Python):
H, W = map(int,input().split())
N = int(input())
ans = [[0]*W for _ in range(H)]
A = [int(i) for i in input().split()]
hpos = 0
wpos = 0
apos = 0
cnt = 0
while True:
  while cnt<A[apos]:
    ans[hpos][wpos] = apos+1
    if hpos%2 == 0:
      wpos += 1
      if wpos == W:
        hpos += 1
        wpos = W-1
    else:
      wpos -= 1
      if wpos == -1:
        hpos += 1
        wpos = 0
    cnt += 1
  cnt = 0
  apos += 1
  if hpos == H:
    break
for i in range(H):
  print(*ans[i])

Solution (Python):

import sys

def solve(H: int, W: int, N: int, a: "List[int]"):
    cur_index = 0
    answer_grid = [[0]*W for _ in range(H)]

    for i in range(H):
        if i%2 == 0:
            for j in range(W):
                answer_grid[i][j] = cur_index+1
                a[cur_index] -= 1
                if a[cur_index] == 0:
                    cur_index += 1

        else:
            for j in range(W-1,-1,-1):
                answer_grid[i][j] = cur_index+1
                a[cur_index] -= 1
                if a[cur_index] == 0:
                    cur_index += 1
    for i in range(H):
        print(*answer_grid[i])
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  
    W = int(next(tokens))  
    N = int(next(tokens))  
    a = [int(next(tokens)) for _ in range(N)]  
    solve(H, W, N, a)

if __name__ == '__main__':
    main()

Solution (Python):
import sys

import numpy as np

readline = sys.stdin.buffer.readline


def main():
    H, W = map(int, readline().split())
    N = int(readline())
    A = list(map(int, readline().split()))
    B = np.ones([H * W], dtype=np.int32)

    s = 0
    for i, a in enumerate(A):
        B[s:s + a] = i + 1
        s += a

    B = np.reshape(B, [H, W])
    for i in range(H):
        if i % 2 == 1:
            B[i, :] = B[i, :][::-1]
        print(*B[i, :])


main()

Solution (Python):
H,W = map(int,input().split())
N = int(input())
A = list(map(int,input().split()))
ans = [[0 for _ in range(W)] for _ in range(H)]
S = []
for i,a in enumerate(A):
  S+=[i+1]*a

for i in range(H):
  if i%2==0:
    for j in range(W):
      ans[i][j]=S.pop()
      
  else:
    for j in range(W-1,-1,-1):
      ans[i][j]=S.pop()
      
for a in ans:
  print(' '.join(map(str,a)))

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

color = [[-1]*W for _ in range(H)]
p, q = 0, 0
move_down_flg = 1

for i in range(N):
    for _ in range(a[i]):
        color[p][q] = i + 1
        if move_down_flg == 1 and p < H-1:
            p += 1
        elif move_down_flg == 1 and p == H-1:
            move_down_flg = 0
            q += 1
        elif move_down_flg == 0 and p > 0:
            p -= 1
        else:
            move_down_flg = 1
            q += 1

for x in color:
    print(*x)

Solution (Python):
H, W = map(int, input().split())
N = int(input())
A = [int(x) for x in input().split()]
C = []
for i in range(N):
  C += [str(i + 1)] * A[i]
for j in range(H):
  out = C[:W]
  if j % 2:
    out.reverse()
  print(' '.join(out))
  C = C[W:]

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

c = 0
for i in range(h):
    ans = []
    for j in range(w):
        ans.append(str(c + 1))
        a[c] -= 1
        if a[c] == 0:
            c += 1

    if i % 2 == 0:
        print(' '.join(ans))
    else:
        print(' '.join(ans[::-1]))

Solution (Python):
h, w = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))
colors = []
ans = [[None] * w for _ in range(h)]
dir = True

for i in range(len(a)):
    num = int(a[i])
    for j in range(num):
        colors.append(i+1)



for i in range(h):
    if dir:
        for j in range(w):
            num = colors[0]
            ans[i][j] = num
            del colors[0]
        dir = not dir
    else:
        for j in reversed(range(w)):
            num = colors[0]
            ans[i][j] = num
            del colors[0]
        dir = not dir



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


'''
3 5
5
1 2 3 4 5
'''

Solution (Python):
from sys import stdin
from itertools import groupby
import numpy as np

h,w = [int(x) for x in stdin.readline().rstrip().split()]
n = int(stdin.readline().rstrip())
li = list(map(int,stdin.readline().rstrip().split()))

lin = np.asarray([0]*h*w)
now = 0
point = 0
for i in range(n):
    point += 1
    for j in range(now,li[i]+now):
        lin[j] = point
    now += li[i]

lin = np.reshape(lin,(h,w)).tolist()

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

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

grid=[[0 for i in range(W)] for j in range(H)]
cur=0
curcnt=0
for i in range(H):
  for j in range(W):
    p=j
    if i%2==1:
      p=W-1-j
    grid[i][p]=cur+1
    curcnt+=1
    if curcnt==a[cur]:
      cur+=1
      curcnt=0
      
for i in range(H):
  print(*grid[i])

Solution (Python):
from sys import stdin
H,W = [int(x) for x in stdin.readline().rstrip().split()]
N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().split()]
A = [ind+1 for ind,j in enumerate(A) for i in range(j)]
for i in range(H):
    if i % 2 == 0:
        for j in A[i*W:i*W+W]:
            print(j,end=" ")
    else:
        for j in A[i*W:i*W+W][::-1]:
            print(j,end=" ")
    print("")

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    h,w=map(int,input().split())
    n=int(input())
    A=[0]*(h*w)
    idx=0
    for i,a in enumerate(map(int,input().split())):
        cnt=0
        while(cnt<a):
            A[idx]=i+1
            cnt+=1
            idx+=1

    idx=0
    for i in range(h):
        G=[0]*w
        for j in range(w):
            G[j]=A[idx]
            idx+=1
        if(i&1): G=G[::-1]
        print(*G)
resolve()

Solution (Python):
H,W = map(int, input().split())
N = int(input())
A = [int(a) for a in input().split()]

ans = [[-1]*W for _ in range(H)]
i = 0
j = 0
cnt = 0
for k in range(N):
    for _ in range(A[k]):
        if (j == 0 and i%2 == 1) or (j == W-1 and i%2 == 0):
            ans[i][j] = k+1
            cnt += 1
            i += 1
            continue
        if cnt == H*W:
            break
        ans[i][j] = k+1
        cnt += 1
        if i%2 == 0:
            j += 1
        else:
            j -= 1
            
for i in range(H):
    ans[i] = map(str, ans[i])
    print(" ".join(ans[i]))

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

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

for i in range(H):
    row = b[i * W:(i + 1) * W]
    if i % 2 == 1:
        row.reverse()
    print(' '.join([str(c) for c in row]))