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()))
m = [[None for _ in range(W)] for _ in range(H)]
x, y = 0, 0
for color in range(N):
    for _ in range(a[color]):
        m[x][y] = color + 1
        if x % 2 == 0:
            if y == W - 1:
                x += 1
            else:
                y += 1
        else:
            if y == 0:
                x += 1
            else:
                y -= 1
for i in range(H):
    for j in range(W):
        c = '\n' if j == W - 1 else ' '
        print(m[i][j], end=c)

Solution (Python):

def main():
    high, width = map(int, input().split())
    num = int(input())
    data = list(map(int, input().split()))
    lans = [0 for i in range(high * width)]
    ind = 0
    for i in range(num):
        for j in range(data[i]):
            lans[ind] = i + 1
            ind += 1

    for i in range(high):
        now_data = lans[i * width: i * width + width]
        if i % 2 == 1:
            now_data = now_data[::-1]
        now_data = list(map(str, now_data))
        print(' '.join(now_data))

if __name__ == '__main__':
    main()

Solution (Python):
H,W =map(int,input().split())
N = int(input())
a = list(map(int,input().split()))
M = []
for i in range(N):
    for j in range(a[i]):
        M.append(i+1)
ans = [M[i*W:(i+1)*W] for i in range(H)]
for i in range(1,H,2):
    ans[i].reverse()
for i in ans:
    for j in i:
        print(j,end = " ")
    print()

Solution (Python):
h,w=map(int,input().split())
n=int(input())
arr=list(map(int,input().split()))
ans=[[0]*w for _ in range(h)]
cnt=0
apos=0
for i in range(h*w):
  if (i//w)%2==0:
    th=i//w
    tw=i%w
  else:
    th=i//w
    tw=w-1-i%w
  ans[th][tw]=apos+1
  cnt+=1
  if cnt==arr[apos]:
    apos+=1
    cnt=0
for i in range(h):
  print(*ans[i])

Solution (Python):
h,w=map(int,input().split())
n=int(input())
arr=list(map(int,input().split()))
ans=[[0]*w for _ in range(h)]
hpos=0
wpos=0
apos=0
cnt=0
while 1:
  while cnt<arr[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):
H,W=map(int,input().split())
N=int(input())
A=list(map(int,input().split()))
if W==1:
    tmp=0
    for a in A:
        tmp+=1
        for j in range(a):
            print(tmp)
    quit()
G=[[0]*W for i in range(H)]
i=0
j=0
tmp=0
t=1
for a in A:
    tmp+=1
    for k in range(a):
        G[i][j]=tmp
        if j==W-1:
            if t==0:
                i+=1
                t=1
                dx=0
            elif t==1:
                dx=-1
                t=0
        elif j==0:
            if t==0:
                i+=1
                t=1
                dx=0
            elif t==1:
                dx=1
                t=0
        j+=dx
for k in G:
    print(*k,sep=" ")

Solution (Python):

import sys


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

    idx = 0
    cnt = 0
    for y in range(H):
        for x in range(W):
            if y % 2 == 1:
                i = W - x - 1
            else:
                i = x
            ret[y][i] = idx + 1
            cnt += 1
            if cnt >= a[idx]:
                idx += 1
                cnt = 0

    for j in range(H):
        print(' '.join([str(x) for x in ret[j]]))
    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):
H, W = [int(s) for s in input().split()]
N = int(input())
A = [int(s) for s in input().split()]

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

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

    chunks = list(chunk(a, W))
    for i, c in enumerate(chunks):
        if i % 2 != 0:
            c.reverse()
        print(*c)

if __name__ == '__main__':
    main()

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

ans=[[0 for i in range(w)] for i in range(h)]
ct=0
num=0
for i in range(h):
  for j in range(w):
    if ct<a[num]:
      ans[i][j]=str(num+1)
      ct+=1
    else:
      ct=0
      num+=1
      ans[i][j]=str(num+1)
      ct+=1
for i in range(h):
  if i%2==1:
    ans[i].reverse()
  print(' '.join(ans[i]))

Solution (Python):
H, W = map(int, input().split())
N = int(input())
A = list(map(int, input().split()))
board = []
for i in range(0, H):
	lst = []
	for j in range(0, W):
		lst.append('')
	board.append(lst)

h = 0
w = 0
index = 0
cnt = 0
while index < N:
	board[h][w] = str(index+1)
	cnt += 1
	if cnt == A[index]:
		index += 1
		cnt = 0
	if h % 2 == 0:
		if w+1 == W: h += 1
		else: w += 1
	else:
		if w-1 == -1: h += 1
		else: w -= 1
for i in range(0, H):
	ans = board[i][0]
	for j in range(1, W):
		ans = ans + ' ' + board[i][j]
	print(ans)

Solution (Python):
import collections
H, W = map(int, input().split())
N = int(input())
As = list(map(int, input().split()))
anss = [collections.deque() for _ in range(H)]
state = 0
i = 0
cnt = 0
for j in range(N):
    for k in range(As[j]):
        if cnt == W:
            state += 1
            cnt = 0
            i += 1
        if state % 2 == 0:
            anss[i].append(j+1)
            cnt += 1
        else:
            anss[i].appendleft(j+1)
            cnt += 1
for ans in anss:
    print(*ans)

Solution (Python):


from collections import* 
from fractions import gcd
from functools import* 
from itertools import* 
from operator import mul
from bisect import* 
from heapq import* 
from math import factorial,pi
from copy import deepcopy
import sys

sys.setrecursionlimit(10**6)

def main():
    h,w,n,*a=map(int,open(0).read().split())
    ans=[]
    for k,v in enumerate(a):
        for i in range(v):
            ans.append(k+1)

            if len(ans)%w==0:
                if (len(ans)//w)%2:
                    print(*ans[-w:])
                else:
                    print(*ans[-w:][::-1])
if __name__ == '__main__':
    main()

Solution (Python):











import sys
input=sys.stdin.readline
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [input() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    H, W = MI()
    N = I()
    A = [(int(a),i) for i,a in enumerate(input().split(), start=1)]
    A.sort()

    stack = []
    for a,i in A:
        stack.extend([i] * a)

    ans = [[0] * W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            c = stack.pop()
            if i % 2 == 0:
                ans[i][j] = c
            else:
                ans[i][W - 1 - j] = c
    for a in ans:
        print(*a)


    
if __name__ == '__main__':
    main()

Solution (Python):











import sys
input=sys.stdin.readline
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [input() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    H, W = MI()
    N = I()
    A = [(int(a),i) for i,a in enumerate(input().split(), start=1)]
    A.sort()

    stack = []
    for a,i in A:
        stack.extend([i] * a)

    ans = [[0] * W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            c = stack.pop()
            if i % 2 == 0:
                ans[i][j] = c
            else:
                ans[i][W - 1 - j] = c
    for a in ans:
        print(*a)


    
if __name__ == '__main__':
    main()

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

Solution (Python):
import sys

import numpy as np

sys.setrecursionlimit(100000)
INF = float('inf')

H, W = list(map(int, sys.stdin.readline().split()))
_ = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))

board = np.zeros(H * W, dtype=int)
board[np.array(A).cumsum()[:-1]] = 1
board = (board.cumsum() + 1).reshape(H, W)
for i, row in enumerate(board):
    print(*(row if i % 2 else reversed(row)))

Solution (Python):
import sys

import numpy as np

sys.setrecursionlimit(100000)
INF = float('inf')

H, W = list(map(int, sys.stdin.readline().split()))
_ = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))


def hw(i):
    h = i // W
    w = i % W
    return h, w if h % 2 == 0 else W + ~w


c = 1
board = np.zeros((H, W), dtype=int)
i = 0
for a in A:
    for _ in range(a):
        board[hw(i)] = c
        i += 1
    c += 1
for row in board:
    print(*row)

Solution (Python):
h, w = [ int(v) for v in input().split() ]
n = int(input())
num_list = [ int(v) for v in input().split() ]

grid_list = []
for i in range(n):
	grid_list += [str(i+1)]*num_list[i]

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

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

Solution (Python):
H,W = [ int(it) for it in input().split() ]
N = int(input())
li = [ int(it) for it in input().split() ]
chk = [ [i+1]*li[i] for i in range(len(li)) ]
from itertools import chain
chk = list(chain.from_iterable(chk))

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

x = 0
y = 0
dx = 1
for i in range(H*W):
  mp[y][x] = chk[i]
  x += dx
  if (x==W):
    x = W-1
    y += 1
    dx = -1
  if (x==-1):
    x = 0
    y += 1
    dx = 1
    
    
[ print( " ".join( map(str,line) )  ) for line in mp ]

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

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



for i in range(h):
  l = s[w*i:w*(i+1)]
  if i%2 == 1:
    l.reverse()
  print((" ").join(l))

Solution (Python):
import sys
sys.setrecursionlimit(10**6)

H, W = map(int, input().split())
N = int(input())
a = list(map(int, input().split()))
K = []
for i in range(N):
    for j in range(a[i]):
        K.append(i+1)
maze = [[0] * W for _ in range(H)]

def dfs(x, y, p, i):
    if y < 0 or y >= H:
        return
    if 0 <= x < W:
        maze[y][x] = K[i]

    if x < 0:
        p = 0
        dfs(x+1, y+1, p, i)
        return
    elif x >= W:
        p = 1
        dfs(x-1, y+1, p, i)
        return

    if p == 0:
        dfs(x+1, y, p, i+1)
    else:
        dfs(x-1, y, p, i+1)

    return

dfs(0, 0, 0, 0)

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

Solution (Python):
i=input
m=lambda:map(int,i().split())
H,W=m()
i()
a=[]
for j,A in enumerate(m(),1):a+=[j]*A
for j in range(H):print(*a[j*W:j*W+W][::(-1)**j])

Solution (Python):
H,W=map(int,input().split())
input()
ans=[]
for i,a in enumerate(map(int,input().split()),1):
    ans+=[i]*a
for i in range(H):
    print(" ".join(map(str,ans[i*W:(i+1)*W][::(-1)**i])))

Solution (Python):
H,W = list(map(int,input().split()))
N = int(input())
A = list(map(int,input().split()))
ans = []
for i,a in enumerate(A,1):
    ans += [i]*a
for i in range(H):
    print(" ".join(map(str,ans[i*W:(i+1)*W][::(-1)**i])))

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

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

h, w = 0, 0
for n, x in a:
    for i in range(x):
        data[h][w] = n

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

for x in data:
    for y in x:
        print(y, end=' ')
    print()

Solution (Python):
from functools import reduce
import math

def main():
    
    
    
    

    
    
    
    
    
    

    
    

    
    
    
    
    
    
    
    
    
    INF = float("inf")

    
    MOD = 10**9+7
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    
    
    
    
    H, W = (int(_) for _ in input().split())
    N = int(input().rstrip())
    a = list(int(_) for _ in input().split())
    
    
    

    
    
    

    Grid = []
    for i in range(H):
        Grid.append(list ( -1 for j in range(W)))
        
    a_index = 0
    a_cnt = a[a_index]
    for h in range(H):
        for w in range(W):
            Grid[h][w] = a_index+1
            if a_cnt - 1 <=0:
                a_index +=1
                if(a_index == N):
                    break
                a_cnt = a[a_index]
            else:
                a_cnt -=1
                
    for h in range(H):
        if h % 2 ==1:
            Grid[h] = Grid[h][::-1]
    for h in range(H):
        print(' '.join(str(_) for _ in Grid[h]))
    
if __name__ == '__main__':
    main()

Solution (Python):

import sys
from collections import deque

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


L=[]

cnt=0
flag="r"
q=deque([]) 
for i,a in enumerate(A):
	for u in range(a):
		cnt+=1
		
		if flag=="r":
			q.append(i+1)
		elif flag=="l":
			q.appendleft(i+1)

		if cnt==W:
			cnt=0
			L.append(q)
			q=deque([]) 
			if flag=="r": flag="l"
			elif flag=="l": flag="r"

for u in L:
	for x in u:
		print x,
	print

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

ans = []
for i, a in enumerate(A, start=1):
    ans.extend([i] * a)

ans = [ans[(h * W): ((h + 1) * W)] for h in range(H)]
for i, a in enumerate(ans):
    if i % 2 == 0:
        print(*a, sep=' ')
    else:
        print(*a[::-1], sep=' ')

Solution (Python):
H, W = [int(item) for item in input().split()]
n = int(input())
a = [int(item) for item in input().split()]

arr = [["."] * W for _ in range(H)]
w, h = 0, 0
direction = 0
for c, item in enumerate(a):
    for i in range(item):
        arr[h][w] = c + 1
        if direction == 0 and w < W-1:
            w += 1
        elif direction == 0 and w == W-1:
            h += 1
            direction = 1
        elif direction == 1 and w > 0:
            w -= 1
        elif direction == 1 and w == 0:
            h += 1
            direction = 0

for line in arr:
    for c in line:
        print(c, end=" ")
    print("")
print("")

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

B = []
for i in range(N):
    B += [i + 1] * A[i]
    
for h in range(H):
    if h % 2 == 0:
        print(' '.join(map(str, B[: W])))
        del B[: W]
    else:
        print(' '.join(map(str, B[: W][::-1])))
        del B[: W]

Solution (Python):
from statistics import mean, median,variance,stdev
import numpy as np
import sys
import math
import fractions
import itertools
import copy
from operator import itemgetter

def j(q):
    if q==1: print("YES")
    else:print("NO")
    exit(0)


def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")

def ip():
    return int(input())

h,w= (int(i) for i in input().split())       









n = ip()
a = [int(i) for i in input().split()]
horz = 1
vert = 1
x = 0
y = 0
ans  = [[0 for i in range(w)] for i in range(h)]
pos = 0
for i in range(h*w):
    ans[y][x] = pos+1
    a[pos]-=1
    if a[pos] ==0:
        pos+=1
    if y == h-1 and vert == 1:
        x+=1
        vert = -1
    elif y == 0 and vert == -1:
        x+=1
        vert = 1
    else: y+=vert
for i in range(h):
    for k in range(w):
        if k+1 != w: print(ans[i][k],"",end="")
        else : print(ans[i][k],end="")
    print()

Solution (Python):
a1,a2,a4=0,0,0

for i in range(1):
    
    
    h,w=map(int,input().split())
    n=int(input())
    a=list(map(int,input().split()))
    ser=[]
    for i in range(len(a)):
        for j in range(a[i]):
            ser.append(i+1)

k=1
for i in range(h):
    ans=ser[i*w:(i+1)*w]
    if k==-1:
        ans=ans[::-1]
    print(*ans)
    k*=-1

Solution (Python):
h, w = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))
m = h * w
X = []

for i, ai in enumerate(a):
    X += [i + 1 for _ in range(ai)]

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

Solution (Python):
import sys

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

    B = []
    for i in range(N):
        a = A[i]
        B.append((a, i+1))
    B = sorted(B, key=lambda x: -x[0])

    C = []
    for b in B:
        C += [b[1]] * b[0]

    ans = []
    for i in range(H):
        start = i * W
        end = start + W
        row = C[start:end]
        if i % 2 == 0:
            ans.append(row)
        else:
            ans.append(row[::-1])

    for a in ans:
        print(*a)


if __name__ == '__main__':
    main()

Solution (Python):
def solve(H, W, a):
    grid = []
    for i in range(H):
        grid.append([])
        for j in range(W):
            grid[i].append(0)

    r = 0
    c = 0
    for i in range(len(a)):
        r, c = paint(i+1, a[i], grid, r, c)

    print_grid(grid)


def paint(color, n, grid, r, c):
    for i in range(n):
        dc = 1 if r % 2 == 0 else -1
        dr = 1 if (dc == -1 and c == 0) or (dc == 1 and c == len(grid[0])-1) else 0
        if dr == 1: dc = 0
        grid[r][c] = color
        r += dr
        c += dc
    return r, c


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


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

if __name__ == '__main__':
    main()

Solution (Python):
def grid_coloring(H: int, W: int, N: int, A: list)->list:
    h, w = 0, 0
    dw = 1
    ret = [[''] * W for _ in range(H)]
    for i, a in enumerate(A):
        for _ in range(a):
            ret[h][w] = str(i+1)
            if w + dw == W:
                h += 1
                dw = -1
            elif w + dw == -1:
                h += 1
                dw = 1
            else:
                w += dw
    return ret


if __name__ == "__main__":
    H, W = map(int, input().split())
    N = int(input())
    A = [int(s) for s in input().split()]
    ans = grid_coloring(H, W, N, A)
    print('\n'.join(map(' '.join, ans)))

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

ret = []
iv = 0
for i in range(h):
    s = ""
    for j in range(w):
        v = str(iv+1)
        a[iv] -= 1
        if a[iv] == 0: iv += 1
        if i%2 == 0:
            s = s+" "+v if s != "" else v
        else:
            s = v + " " + s
    print(s)

Solution (Python):
H, W = map(int, input().split())
N = int(input())
A = sorted(enumerate(list(map(int, input().split()))), key=lambda x: x[1])
C = [["0" for _ in range(W)] for _ in range(H)]
w = h = 0
direction = 1
for i, a in A:
    for j in range(a):
        C[h][w] = str(i + 1)
        w += direction
        if w == -1:
            w = 0
            h += 1
            direction = 1
            continue
        if w == W:
            w = W - 1
            h += 1
            direction = -1
for c in C:
    print(" ".join(c))

Solution (Python):
H,W=map(int,input().split())
N=int(input())
A=list(map(int,input().split()))
res=[]
for i in range(N):
    res+=[i+1]*A[i]
for h in range(H):
    line=res[W*h:W*(h+1)]
    line=[str(x) for x in line]
    if h%2==0:
        print(' '.join(line))
    elif h%2==1:
        print(' '.join(line[::-1]))

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

grid = [[-1]*W for _ in range(H)]
order = [-1]*(H*W)
k = 0
for i in range(H):
    if i%2 == 0:
        l = range(W)
    else:
        l = range(W-1,-1,-1)
    for j in l:
        order[k] = (i,j)
        k += 1

acc = 0
for k in range(N):
    for i,j in order[acc:acc+A[k]]:
        grid[i][j] = k+1
    acc += A[k]

for line in grid:
    print(" ".join([str(s) for s in line]))

Solution (Python):
def main():
    H, W = map(int, input().split())
    N = int(input())
    A = list(map(int, input().split()))
    h, w = 0, 0
    ans = [[0]*W for _ in range(H)]
    for i in range(N):
        for k in range(A[i]):
            ans[h][w] = i+1
            if (h % 2 == 1 and w == 0) or (h % 2 == 0 and w == W-1):
                h += 1
            else:
                if h % 2 == 0:
                    w += 1
                else:
                    w -= 1
    for s in ans:
        print(" ".join(map(str, s)))


if __name__ == "__main__":
    main()

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007






"""
n = I()
a = LI()
d = [0,0]
for i in a:
    if i%2:
        d[0] += 1
    if i%4 == 0:
        d[1] += 1
if n % 2:
    d[0] -= 1
d[0] -= d[1]
if d[0] <= 0:
    print("Yes")
else:
    print("No")
"""


h,w = LI()
n = I()
a = LI()
c = [[None for x in range(w)] for y in range(h)]
i = 0
for y in range(h):
    if y%2:
        for x in range(w)[::-1]:
            if not a[i]:
                i += 1
            c[y][x] = i+1
            a[i] -= 1
    else:
        for x in range(w):
            if not a[i]:
                i += 1
            c[y][x] = i+1
            a[i] -= 1
for i in c:
    print(*i)

Solution (Python):
h,w = map(int,input().split())
n = int(input())
a = list(map(int,input().split()))
m = [['']*w for _ in range(h)]
        
i = 0
cnt = 0
for k in range(h):
    for l in range(w):
        if k % 2 == 1:
            m[k][w-l-1] = i+1
        else:
            m[k][l] = i+1  
            
        cnt += 1    
        if cnt == a[i]:
            i += 1
            cnt = 0

for i in m:
    print(*i)

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

Solution (Python):
h,w=map(int,input().split())
n=int(input())
A=list(map(int,input().split()))
C=[[0]*w for _ in range(h)]
prev_h,prev_w=0,-1
for i in range(n):
    j=0
    while j<A[i]:
        if prev_h%2:
            if prev_w:
                C[prev_h][prev_w-1]=i+1
                prev_w-=1
            else:
                C[prev_h+1][prev_w]=i+1
                prev_h+=1
        else:
            if prev_w!=w-1:
                C[prev_h][prev_w+1]=i+1
                prev_w+=1
            else:
                C[prev_h+1][prev_w]=i+1
                prev_h+=1
        j+=1
for i in range(h):
    print(' '.join([str(n) for n in C[i]]))

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

c = [0]*N
n = 0
sign = 1
for h in range(H):
    state = [0]*W
    for w in range(W):
        if c[n]>=a[n]:
            n += 1
        state[w] = str(n+1)
        c[n] += 1
    sign = (sign+1)%2
    if sign:
        state = state[::-1]
    print(' '.join(state))

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

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

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

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

ans=[[None]*W for _ in range(H)]
x,y=0,0
d=1

for (i,j) in enumerate(a,start=1):
    
    for k in range(j):
        ans[y][x]=i
        
        y+=d
        if not 0<=y<H:
            y-=d
            x+=1
            d*=-1

for i in ans:
    print(*i)

Solution (Python):
h, w = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))
b = []
for i in range(n):
    for j in range(a[i]):
        b.append(i + 1)
c = [[0] * w for i in range(h)]
k = 0
flag = 0
i = 0
j = 0
while k < h * w:
    c[i][j] = str(b[k])
    k += 1
    if j == w - 1 and flag == 0:
        i += 1
        flag = 1
    elif j == 0 and flag == 1:
        i += 1
        flag = 0
    elif flag:
        j -= 1
    else:
        j += 1
for i in range(h):
    print(" ".join(c[i]))

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

st = x[0]
m = 1
X=0
for i in range(H):
    s = ""
    for j in range(W):
        if X == st:
            st = x[m]
            m += 1
            X = 0
        if i%2==0:
            s+=str(m)+" "
        else:
            s = str(m)+" "+s
        X +=1
    print(s)

Solution (Python):

def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(input())
def S(): return input()

h,w=IL()
n=I()
a=IL()
c=0
reverse=False
cl=[[-1 for i in range(w)] for j in range(h)]
for i in range(h):
    if reverse:
        for j in range(w):
            cl[i][w-1-j]=c+1
            a[c]-=1
            if a[c]==0:
                c+=1
        reverse=False
    else:
        for j in range(w):
            cl[i][j]=c+1
            a[c]-=1
            if a[c]==0:
                c+=1
        reverse=True

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

Solution (Python):
def solve(H, W, N, As):
    
    field = [[0] * W for _ in range(H)]
    index = 0
    for color, a in enumerate(As):
        color += 1
        for _ in range(a):
            row = index // W
            if row % 2 == 0:
                field[row][index - row * W] = str(color)
            else:
                field[row][W - (index - row * W) - 1] = str(color)
            index += 1
    ans = "\n".join([" ".join(row) for row in field])
    return ans




if __name__ == "__main__":
    H, W = tuple(map(int, input().split(" ")))
    N = int(input())
    As = list(map(int, input().split(" ")))
    print(solve(H, W, N, As))

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

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

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

R = []

for i, a in enumerate(A):
    R += [str(i+1)] * a

for h in range(H):
    if h % 2 == 0:
        print(' '.join(R[W*h:W*(h+1)]))
    else:
        print(' '.join(R[W*h:W*(h+1)][::-1]))

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

path = []
for i in range(H):
    if i % 2 == 0:
        range_ = range(W)
    else:
        range_ = reversed(range(W))
    for j in range_:
        path.append((i, j))

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

ans = [[0]*W for _ in range(H)]
for k, p in enumerate(path):
    i, j = p
    ans[i][j] = c[k]

for elem in ans:
    print(' '.join(map(str, elem)))