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):
def listToString(_list, split=""):
    maped_list = map(str, _list)  
    mojiretu = split.join(maped_list)
    return mojiretu


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


tile = [[0 for _ in range(w+2)] for _ in range(h+2)]
visited = [[False for _ in range(w+2)] for _ in range(h+2)]
for i in range(len(visited)):
    for j in range(len(visited[0])):
        if i == 0 or i == len(visited)-1 or j == 0 or j == len(visited[0])-1:
            visited[i][j] = True
color = [0 for _ in range(h*w)]
counter = 0
for i in range(len(a)):
    color[counter:counter+a[i]] = [i + 1 for _ in range(a[i])]
    counter += a[i]

dir = "r" 
x, y = [1, 1]
while True:
    if len(color) == 0:
        break
    _color = color.pop()
    tile[x][y] = _color
    visited[x][y] = True
    if dir == "r":
        if not visited[x+1][y]:
            x += 1
        else:
            dir = "d"
            y += 1
    elif dir == "d":
        if not visited[x][y+1]:
            y += 1
        else:
            dir = "l"
            x -= 1
    elif dir == "l":
        if not visited[x-1][y]:
            x -= 1
        else:
            dir = "u"
            y -= 1
    elif dir == "u":
        if not visited[x][y-1]:
            y -= 1
        else:
            dir = "r"
            x += 1
for i in range(1, len(tile)-1):
    print(listToString(tile[i][1:-1], " "))

Solution (Python):
import sys
input = sys.stdin.readline
h, w = map(int, input().split())
n = int(input())
A = list(map(int, input().split()))
nums = [[-1]*w for _ in range(h)]
s = [0, 0]
f = [0, 0]
di = 1
j = 0
k = -1

for i in range(n):
    a = A[i]
    i_ = i + 1
    while a:
        if 0 <= k+di and k+di< w:
            nums[j][k+di] = i_
            k += di
        else:
            di = -di
            j += 1
            nums[j][k] = i_
        a -= 1

for i in range(h):
    b = nums[i]
    print(*b)

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

B = [-1]*(H*W)
bi = 0
for i in range(N):
    for _ in range(A[i]):
        B[bi] = i+1
        bi += 1

Ans = []
for h in range(H):
    Ans.append(B[h*W:(h+1)*W])

for h in range(H):
    if h%2==0:
        print(" ".join(map(str,Ans[h])))
    else:
        Ans[h].reverse()
        print(" ".join(map(str,Ans[h])))

Solution (Python):


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

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

grid = [[0] * W for _ in range(H)]
k = 0
for i in range(H):
    for j in range(W):
        grid[i][j] = nums[k]
        k += 1

for i in range(H):
    if i % 2 == 0:
        print(*grid[i])
    else:
        print(*reversed(grid[i]))

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

h, w = 0, 0
G = [[''] * W for i in range(H)]

for i, a in enumerate(A, start=1):
    for ai in range(a):
        G[h][w] = str(i)
        w += 1
        if w == W:
            h += 1
            w = 0

for i in range(H):
    if i % 2 == 0:
        print(' '.join(G[i]))
    else:
        print(' '.join(G[i][::-1]))

Solution (Python):

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

out = [[0 for _ in range(W)] for _ in range(H)]
cnt = 0
for i,a in enumerate(an):
    for j in range(a):
        if (cnt//W)%2  == 0:
            out[cnt//W][cnt%W] = i + 1
        else:
            out[cnt//W][W - cnt%W - 1] = i + 1
        
        cnt += 1

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

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

result = []


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

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

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

result = [[0 for _ in range(w)] for _ in range(h)]
height = 0
width = 0
count = -1

for i in range(1, n + 1):
    a = a_list[i - 1]
    for j in range(a):
        result[height][width] = i
        count += 1
        if width >= 0:
            width += 1
        else:
            width -= 1
        if count % w == w-1:
            height += 1
            if width >= 0:
                width = -1
            else:
                width = 0


for r in result:
    print(*r)

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

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


abc069_d()

Solution (Python):

h, w = map(int, input().split())

n = int(input())
a = input().split()
a = [int(m) for m in a]
s = []
for i in range(h):
    s.append([])
v = 0
for i in range(n):
    for j in range(1, a[i] + 1):
        if len(s[v]) < w:
            s[v].append(i + 1)
        else:
            v += 1
            s[v].append(i + 1)

oore=0
for t in s:
    for i in range(len(t)):
        if oore % 2 == 0:
            print(t[i], "", end = '')
        else:
            print(t[-(i + 1)], "", end = '')
        if i == len(t) - 1:
            print()
            oore += 1

Solution (Python):
H,W=map(int,input().split())
N=int(input())
A=[int(i) for i in input().split()]
List=[]
for i in range(N):
  List=List+[str(i+1)]*A[i]
for h in range(H):
  a=W*h
  b=W*(h+1)
  l=List[a:b]
  if h%2==1:
    l.sort(key=lambda x:-int(x))
  print(" ".join(l))

Solution (Python):
def main():
    H, W = map(int, input().split())
    N = int(input())
    A = list(map(int, input().split()))
    retval = [[0] * W for _ in range(H)]
    index = 0
    for i in range(N):
        count = A[i]
        while count:
            y = int(index / W)
            if y % 2 == 0:
                x = index % W
            else:
                x = (W - 1) - index % W
            retval[y][x] = i+1
            count -= 1
            index += 1
    for row in retval:
        print(" ".join(map(str, row)))


if __name__ == "__main__":
    main()

Solution (Python):
a,b = map(int,input().split(" "))
ar = [[0 for i in range(b)]for j in range(a)]
c = int(input())
br = list(map(int,input().split(" ")))
s = "p"
count = 0
x = 0
y = 0
t = 1
while count != a * b:
    ar[x][y] += t
    br[0] -= 1
    if br[0] == 0:
        del br[0]
        t += 1
    if s == "p":
        y += 1
        if y == b:
            y -= 1
            x += 1
            s = "m"
    else:
        y -= 1
        if y == -1:
            y = 0
            x += 1
            s = "p"
    count += 1
for d in range(a):
    for e in range(b-1):
        print(ar[d][e],end=" ")
    print(ar[d][b-1])

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

ans = []
for n in range(N):
    for i in range(A[n]):
        ans.append(str(n + 1))

w = 0
for h in range(H):
    if h % 2 == 0:
        print(" ".join(ans[w: w + W]))
    else:
        print(" ".join(ans[w: w + W][::-1]))
    w += W

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

h = 0
w = 0

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

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

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

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):
    s_,s = s[:W],s[W:]
    if i % 2 == 1:
        s_ = s_[::-1]
    print(' '.join(s_))

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

C = [[] for i in range(H)]
tempL = 0
remR = W
for i in range(N):
    Aleft = A[i]
    while Aleft > remR:
        if tempL % 2 == 0:
            for j in range(remR):
                C[tempL].append(i+1)
        else:
            for j in range(remR):
                C[tempL] = [i+1] + C[tempL]
        tempL += 1
        Aleft -= remR
        remR = W
    if tempL % 2 == 0:
        for j in range(Aleft):
            C[tempL].append(i+1)
    else:
        for j in range(Aleft):
            C[tempL] = [i+1] + C[tempL]
    if remR == Aleft:
        tempL += 1
        remR = W
    else:
        remR -= Aleft
for i in range(H):
    print(" ".join(map(str, C[i])))

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

aa = []
for i,ai in enumerate(a):
    aa.extend([i+1] * ai)

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

for h in range(H):
    for w in range(W):
        if h % 2 == 0:
            grid[h][w] = aa.pop()
        else:
            grid[h][-w-1] = aa.pop()


for g in grid:
    print(*g)

Solution (Python):
import math
n,m = map(int,input().split())
a = int(input())
lis = list(map(int,input().split()))
ans = []
num = math.ceil(n/2)
li = []
for i in range(a):
  for k in range(lis[i]):
    li.append(str(i+1))
for i in range(n):
  l = []
  for k in range(m):
    if i % 2 == 0:
      l.append(str(li[m * i + k]))
    else:
      l.insert(0,str(li[m * i + k]))
  ans.append(l)
  
for i in range(n):print(" ".join(ans[i]))

Solution (Python):
def ABC069D():
    H, W = map(int, input().split())
    N = int(input())
    a = list(map(int, input().split()))
    ans = []
    for i in range(N):
        for j in range(a[i]):
            ans.append(i + 1)
    printlist = [[0 for i in range(W)] for j in range(H)]
    k = 0
    for i in range(H):
        for j in range(W):
            printlist[i][j] = ans[k]
            k += 1
        if i % 2 != 0:
            printlist[i] = printlist[i][::-1]

    for i in range(H):
        for j in range(W):
            print(printlist[i][j], end = "")
            if j != W:
                print(" ", end = "")
        print()
    print()

ABC069D()

Solution (Python):

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

grid = [[0 for k in range(W)] for l in range(H)]
now = 0
for k in range(H):
    for l in range(W):
        if a[now] == 0:
            now += 1
        if k%2 == 0:
            grid[k][l] = now+1
            a[now] -= 1
        else:
            grid[k][W-l-1] = now+1
            a[now] -= 1

for r in grid:
    print(" ".join(map(str,r)))

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

list2=[]
for k in range(N):
    for l in range(A[k]):
        list2.append(k+1)
for m in range(H):
    for n in range(W):
        if m%2==0:
            list1[m][n]=list2[m*W+n]
        else:
            list1[m][W-n-1]=list2[m*W+n]
for o in range(H):
    ans=str(list1[o][0])
    for p in range(W-1):
        ans=ans+" "+str(list1[o][p+1])
    print(ans)

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

H = 0
W = 0
i = 0
b = []

while H <= h-1:
    if H % 2 == 0:
        b.append(i+1)
        a[i] -= 1
        if a[i] == 0:
            i += 1
        if W == w-1:
            H += 1
        if W < w-1:
            W += 1
    else:
        b.append(i+1)
        a[i] -= 1
        if a[i] == 0:
            i += 1
        if W == 0:
            H += 1
        if W > 0:
            W -= 1

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

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

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

h,w = li()
n = ni()
a = list(li())

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

k = 0
cur = k+1
cnt = a[k]
for hi in range(h):
    for wj in range(w):
        grid[hi][wj] = cur
        cnt -= 1
        
        if cnt == 0:
            k += 1
            if k == n:
                break

            cur = k+1
            cnt = a[k]
            
for hi,row in enumerate(grid):
    if hi%2 == 0:
        print(*row)
    else:
        row = row[::-1]
        print(*row)

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)]
i = 0
for n,a in enumerate(A):
    for _ in range(a):
        y,x = divmod(i,W)
        ans[y][x] = n+1
        i += 1
for i,row in enumerate(ans):
    if i%2:
        print(*row[::-1])
    else:
        print(*row)

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

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

seq = 0
for color, i  in enumerate(a):
    i0 = i
    while i0>0:
        r = seq//w
        if r%2 == 0:
            c = seq%w
        else:
            c = w-seq%w-1
        mat[r][c] = color+1
        i0 -=1
        seq += 1
for l in mat:
    print(*l, sep=' ')

Solution (Python):



def inpl(): return list(map(int, input().split()))


H, W = inpl()
N = int(input())
A = inpl()
color = 0
ans = []
for h in range(H):
    tmp = []
    if h % 2 == 0:
        for w in range(W):
            A[color] -= 1
            tmp.append(color + 1)
            if A[color] == 0:
                color += 1
        ans.append(tmp)

    else:
        for w in range(W)[::-1]:
            A[color] -= 1
            tmp.append(color + 1)
            if A[color] == 0:
                color += 1
        tmp.reverse()
        ans.append(tmp)

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

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

def li(): return [int(x) for x in stdin.readline().split()]
def li_(): return [int(x)-1 for x in stdin.readline().split()]
def lf(): return [float(x) for x in stdin.readline().split()]
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(ns())
def nf(): return float(ns())

h,w = li()
n = ni()
a = li()

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

color = 1
res = a[color-1]

for hi in range(h):
    for wj in range(w):
        if res == 0:
            color += 1
            res = a[color-1]
        
        grid[hi][wj] = color
        res -= 1

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

Solution (Python):
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


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

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

    no = 0
    ans = [[-1] * W for _ in range(H)]
    for y in range(H):
        if y % 2 == 0:
            for x in range(W):
                ans[y][x] = c[no]
                no += 1
        else:
            for x in range(W - 1, -1, -1):
                ans[y][x] = c[no]
                no += 1

    for line in ans:
        print(*line, sep=" ")


if __name__ == '__main__':
    main()

Solution (Python):
H, W = map(int, input().split())
N = int(input())
a = [int(ai) for ai in input().split()]
b = []
x, y = 0, 0
forward = True
for i in range(N):
    for _ in range(a[i]):
        b.append(i + 1)
forward = True
for i in range(H):
    bi = list(map(str, b[i*W:(i+1)*W]))
    if forward:
        print(" ".join(bi))
        forward = False
    else:
        print(" ".join(bi[::-1]))
        forward = True

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

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

nl = []

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



for i in range(H):
  work = nl[i*W:(i+1)*W]
  if i%2 == 1:
    work.reverse()
  print(" ".join(work))

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

nl = []

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



for i in range(H):
  work = nl[i*W:(i+1)*W]
  if i%2 == 1:
    work.reverse()
  print(" ".join(work))

Solution (Python):
h,w=map(int,input().split())
n=int(input())
a=map(int,input().split())
l=[["" for _ in range(w)] for _ in range(h)]

m=[]
for i,j in enumerate(a,start=1):
  m.extend([str(i)]*j)

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

for i in l:
  print(" ".join(i))

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

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

    print(' '.join(map(str, ans[y])))

Solution (Python):
def d_grid_coloring(H, W, N, A):
    
    

    
    tmp = []
    for i, a in enumerate(A):
        tmp.extend([str(i + 1)] * a)
    
    tmp = [tmp[i:i + W] for i in range(0, len(tmp), W)]
    
    tmp = [row if i % 2 == 0 else row[::-1] for i, row in enumerate(tmp)]

    
    ans = []
    for s in tmp:
        ans.append(' '.join(s))
    ans = '\n'.join(ans)
    return ans

H,W = [int(i) for i in input().split()]
N = int(input())
A = [int(i) for i in input().split()]
print(d_grid_coloring(H, W, N, A))

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

Solution (Python):
import sys
import math
import collections
import itertools
import array
import inspect


sys.setrecursionlimit(1000000)



def chkprint(*args):
    names = {
        id(v): k
        for k, v in inspect.currentframe().f_back.f_locals.items()
    }
    print(', '.join(
        names.get(id(arg), '???') + ' = ' + repr(arg) for arg in args))



def to_bin(x):
    return bin(x)[2:]


def li_input():
    return [int(_) for _ in input().split()]


def gcd(n, m):
    if n % m == 0:
        return m
    else:
        return gcd(m, n % m)


def gcd_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = gcd(v, L[i])

    return v


def lcm(n, m):
    return (n * m) // gcd(n, m)


def lcm_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = lcm(v, L[i])

    return v



def wfs_d(D, N, K):
    """
    D: 隣接行列(距離付き)
    N: ノード数
    K: 始点ノード
    """

    dfk = [-1] * (N + 1)
    dfk[K] = 0

    cps = [(K, 0)]
    r = [False] * (N + 1)
    r[K] = True
    while len(cps) != 0:
        n_cps = []
        for cp, cd in cps:
            for i, dfcp in enumerate(D[cp]):
                if dfcp != -1 and not r[i]:
                    dfk[i] = cd + dfcp
                    n_cps.append((i, cd + dfcp))
                    r[i] = True

        cps = n_cps[:]

    return dfk



def dfs_d(v, pre, dist):
    """
    v:  現在のノード
    pre: １つ前のノード
    dist: 現在の距離

    以下は別途用意する
    D: 隣接リスト(行列ではない)
    D_dfs_d: dfs_d関数で用いる，始点ノードから見た距離リスト
    """

    global D
    global D_dfs_d

    D_dfs_d[v] = dist

    for next_v, d in D[v]:
        if next_v != pre:
            dfs_d(next_v, v, dist + d)

    return




dp = None


def main():
    H, W = li_input()
    N = int(input())
    A = li_input()

    CS = [None]
    cs = 0
    for a in A:
        cs += a
        CS.append(cs)

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

    B_idx = 0
    c_color = 1

    for i in range(H):
        if i % 2 == 0:
            rng = range(W)
        else:
            rng = range(W - 1, -1, -1)

        for j in rng:
            if B_idx == CS[c_color]:
                c_color += 1

            B[i][j] = c_color
            B_idx += 1

    for row in B:
        print(" ".join(list(map(str, row))))


main()

Solution (Python):
H, W = map( int, input().split())
N = int( input())
A = list( map( int, input().split()))
reverse = 0
ANS = [ [] for _ in range(H)]
gyou = 0
cnt = 0
for i in range(N):
    for j in range(A[i]):
        cnt += 1
        if reverse == 0:
            ANS[gyou].append(i+1)
        else:
            ANS[gyou].insert(0, i+1)
        if cnt == W:
            cnt = 0
            gyou += 1
            reverse = (reverse+1)%2
for i in range(H):
    for j in range(W-1):
        print( str(ANS[i][j]), end = ' ')
    print(ANS[i][W-1])

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

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

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

c = [None] * H * W
j = 0
for i in range(N):
    for k in range(j, j + a[i]):
        c[k] = i + 1
    j += a[i]

ans = [[None for _ in range(W)] for _ in range(H)]
for i in range(H):
    for j in range(W):
        ans[i][j] = c[i * W + j]
    if i % 2 == 1:
        ans[i] = ans[i][::-1]

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

Solution (Python):
H, W = map(int, raw_input().split())
N, = map(int, raw_input().split())
A = map(int, raw_input().split())
s = []
for i in range(N):
  s += [str(i+1)]*A[i]
for y in range(H):
  print " ".join(s[W*y:W*(y+1)][::1-((y%2)*2)])

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

r = 1
l = []
for i, a in enumerate(A):
    l += [i + 1] * a
    while len(l) >= W:
        t, l = l[:W], l[W:]
        print(*t[::r])
        r *= -1

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]*w for _ in range(h)]
x = 0
y = 0
d = (0, 0)
i = 0
j = 0

def printC():
    for _y in range(h):
        print(' '.join(map(str, C[_y])))

while(True):
    nx = x + d[0]
    ny = y + d[1]
    
    
    
    if d == (1, 0):
        if nx >= w or C[ny][nx] > 0:
            
            d = (0, 1)
            continue
    elif d == (0, 1):
        if ny >= h or C[ny][nx] > 0:
            
            d = (-1, 0)
            continue
    elif d == (-1, 0):
        if nx < 0 or C[ny][nx] > 0:
            
            d = (0, -1)
            continue
    elif d == (0, -1):
        if ny < 0 or C[ny][nx] > 0:
            
            d = (1, 0)
            continue
    elif d == (0, 0):
        d = (1, 0)
    x = nx
    y = ny
    C[y][x] = i+1
    
    j += 1
    if j >= A[i]:
        i += 1
        if i >= n:
            break
        j = 0
printC()

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

r = False
l = []
for i, a in enumerate(A):
    l += [i + 1] * a
    while len(l) >= W:
        t = l[:W]
        if not r:
            print(*t)
        else:
            print(*list(reversed(t)))
        l = l[W:]        
        r = not r

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(H, W, N, A):

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

    for y in range(H):
        tmp = []
        for x in range(W):
            if y % 2 == 0:
                tmp.append(str(ans[y*W + x % W]))
            else:
                tmp.append(str(ans[y*W + W - x % W - 1]))
        print(' '.join(tmp))


def main():
    H, W = read_int_n()
    N = read_int()
    A = read_int_n()

    
    
    
    
    
    
    

    
    slv(H, W, N, A)


if __name__ == '__main__':
    main()

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

L = []
idx = 1
C = []

for a in A:
    for i in range(a):
        C.append(idx)
    idx += 1
idx = 0
for h in range(H):
    L.append([])
    for w in range(W):
        L[-1].append(str(C[idx]))
        idx += 1
    if h % 2 == 1:
        L[-1] = [e for e in reversed(L[-1])]

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

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

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

x, y, d = 0, 0, 1
for c, a in enumerate(A):
    for i in range(a):
        rows[y][x] = c + 1
        x += d
        if x < 0 or x >= W:
            d = -d
            x += d
            y += 1

for row in rows:
    print(*row)

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 k in range(h)]

noww = 0
nowh = 0
check = 1 
for i in range(n):
	while a[i] != 0:
		if check == 1 and noww+a[i] > w:
			for k in range(w-noww):
				ans[nowh][noww+k] = i+1
			a[i] = a[i]-(w-noww)
			noww = w
			nowh += 1
			check = check*(-1)
		elif check == -1 and noww-a[i] < 0:
			for k in range(noww):
				ans[nowh][noww-k-1] = i+1
			a[i] = a[i]-(noww)
			nowh += 1
			noww = 0
			check = check*(-1)
		elif check == 1:
			for k in range(a[i]):
				ans[nowh][noww+k] = i+1
			noww = noww+a[i]
			a[i] = 0
			if noww == w:
				nowh += 1
				check = check*(-1)
		else:
			for k in range(a[i]):
				ans[nowh][noww-k-1] = i+1
			noww = noww-a[i]
			a[i] = 0
			if noww == 0:
				nowh += 1
				check = check*(-1)
for i in range(h):
	for k in range(w):
		if k != w-1:
			print(ans[i][k],end=" ")
		else:
			print(ans[i][k])

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

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

Solution (Python):
h, w = map(int, input().split())
n = int(input())
temp = list(map(int, input().split()))
l = []
for i in range(n):
  l += [i+1]*(temp[i])
l = sorted(l)
maze = []
m = 0
for i in range(h):
  maze.append([0]*w)

for i in range(h):
  if i % 2 == 0:
    for j in range(w):
      maze[i][j] = l[m]
      m += 1
  elif not i % 2 == 0:
    for k in range(w-1, -1, -1):
      maze[i][k] = l[m]
      m += 1

for i in maze:
  print(' '.join(map(str, i)))

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