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()))

        


def main():
    Ans = [[0]*W for _ in range(H)]
    cnt = 0
    for i in range(N):
        a = A[i]
        for _ in range(a):
            Ans[cnt // W][cnt%W] = i + 1
            cnt += 1
    
    for j in range(0,H,2):
        Ans[j] = Ans[j][::-1]
    for ans in Ans:
        print(*ans, sep =" ")
    return
main()

Solution (Python):
def main():
    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:
            path += [[i, j] for j in range(W)]
        else:
            path += [[i, j] for j in range(W-1, -1, -1)]

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

    k = 0
    for i, j in path:
        ans[i][j] = B[k]
        k += 1
    return ans


if __name__ == '__main__':
    output = main()
    for i in range(len(output)):
        print(" ".join(map(str, output[i])))

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LIM(): return list(map(lambda x:int(x) - 1, sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def LIRM(n): return [LIM() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
mod = 1000000007


h, w = LI()
n = I()
L = LI()


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


k = 0
cnt = 0
for y in range(h):
    if not y % 2:
        for x in range(w):
            ans[y][x] = k + 1
            cnt += 1
            if cnt >= L[k]:
                k += 1
                cnt = 0
    else:
        for x in range(w-1, -1, -1):
            ans[y][x] = k + 1
            cnt += 1
            if cnt >= L[k]:
                k += 1
                cnt = 0


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

Solution (Python):
h,w=map(int,input().split())
n=int(input())
a=[int(i) for i in input().split()]
f=0
d=[]
for i in range(n):
  for j in range(a[i]):
    d.append(i+1)
    if len(d)==w and f:
      print(*d[::-1])
      f=0
      d=[]
    elif len(d)==w:
      print(*d)
      f=1
      d=[]

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

Solution (Python):
def main():
    H, W = (int(i) for i in input().split())
    N = int(input())
    a = [int(i) for i in input().split()]
    a_idx = [[i+1,cnt] for i,cnt in enumerate(a)]
    a_idx.sort(key=lambda x:x[1],reverse=True)
    a_ins = []
    for val,cnt in a_idx:
        a_ins.extend([val]*cnt)

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

    for val in c:
        print(*val)

if __name__ == "__main__":
    main()

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

tmp = []

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

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

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


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

Solution (Python):

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

HW = [[0 for j in range(W)] for i in range(H)]


n = 1
c = a[0]
for i in range(H):
    for j in range(W):
        HW[i][j] = n
        c -= 1
        if c == 0 and (i != H-1 or j != W-1):
            n += 1
            c = a[n-1]

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

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

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

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

    num = 1
    rest = a[0]
    for y in range(H):
        if y&1:
            for x in range(W-1, -1, -1):
                ans[y][x] = num
                rest -= 1
                if rest == 0:
                    rest = a[num]
                    num += 1
        else:
            for x in range(W):
                ans[y][x] = num
                rest -= 1
                if rest == 0:
                    rest = a[num]
                    num += 1
    
    for y in range(H):
        for x in range(W-1):
            print(ans[y][x], end=" ")
        print(ans[y][-1])
                    

if __name__ == "__main__":
    main()

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

color = []
for i,c in enumerate(A):
  color.extend([i+1]*c)

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

for i in range(H*W):
  x = i//H
  y = i%H if x%2 == 0 else (H-1)-i%H
  board[y][x] = color[i]

for y in range(H):
  print(*board[y])

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

Solution (Python):



def main():
    h, w = map(int, input().split())
    n = int(input())
    a = list(map(int, input().split()))
    i = 0
    ans = [[0 for _ in range(w)] for _ in range(h)]

    
    for hi in range(h):
        for wi in range(w):
            if a[i] == 0:
                i += 1

            ans[hi][wi] = i + 1
            a[i] -= 1

    
    for index, a in enumerate(ans):
        if index % 2 == 1:
            a = a[::-1]

        print(' '.join(map(str, a)))


if __name__ == '__main__':
    main()

Solution (Python):



def main():
    h, w = map(int, input().split())
    n = int(input())
    a = list(map(int, input().split()))
    i = 0
    ans = [[0 for _ in range(w)] for _ in range(h)]

    
    for hi in range(h):
        for wi in range(w):
            if a[i] >= 1:
                ans[hi][wi] = i + 1
                a[i] -= 1
            else:
                i += 1
                ans[hi][wi] = i + 1
                a[i] -= 1

    
    for index, a in enumerate(ans):
        if index % 2 == 1:
            a = a[::-1]

        print(' '.join(map(str, a)))


if __name__ == '__main__':
    main()

Solution (Python):
h,w = map(int,input().split())
n = int(input())
a = list(map(int,input().split()))
ans = [-1 for i in range(h*w)]
count = 0
for i,x in enumerate(a):
    for j in range(x):
        ans[count] = i+1
        count += 1

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

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)]

h = 0
w = 0
total = 0
flag = 1
for i in range(len(a)):
    while a[i] > 0:
        C[h][w] = i + 1
        a[i] -= 1
        if flag == 1:
            if w != W - 1:
                w += 1
            else:
                h += 1
                flag = -1
        else:
            if w != 0:
                w -= 1
            else:
                h += 1
                flag = 1

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

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)]

clr = 1
pl = [0, 0]
v = 1

for i in range(h*w):
    
    ANS[pl[0]][pl[1]] = clr
    A[clr-1] -= 1
    if A[clr-1] == 0:
        clr += 1
    pl[1] += v
    if pl[1] < 0:
        pl[0] += 1
        pl[1] = 0
        v = 1
    elif pl[1] >= w:
        pl[0] += 1
        pl[1] = w-1
        v = -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(j) for j in input().split()]
ans=[[0 for i in range(w)] for j in range(h)]
i,j=0,0
for p in range(n):
    for q in range(a[p]):
        ans[i][j]=p+1
        if j==w-1 and i%2==0:
            i+=1
        elif j==0 and i%2==1:
            i+=1
        elif i%2==0:
            j+=1
        elif i%2==1:
            j-=1

for i in ans:
    print(*i)

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007


def A():
    n, m = LI()
    print((n-1)*(m-1))
    return


def B():
    s = S()
    print(s[0]+str(len(s)-2)+s[-1])
    return


def C():
    II()
    a = LI()
    a1 = 0
    a2 = 0
    a3 = 0
    for ai in a:
        if not ai % 4:
            a3 += 1
            continue
        if not ai % 2:
            a2 += 1
            continue
        a1 += 1
    if a2:
        print(["No", "Yes"][a3 >= a1])
        return
    print(["No", "Yes"][a3 >= a1 - 1])
    return
"""
    n, m = LI()
    x = -1
    y = -1
    z = -1
    for i in range(n + 1):
        a = m - 9000 * i - 1000 * n
        if a % 4000 == 0 and a >= 0 and n - i - a / 4000 >= 0:
            y = a / 4000
            z = n - i - y
            x = i
            break
    print(int(x),int(y),int(z))
"""


def D():
    move = [[1, 0], [0, 1], [-1, 0], [0, -1]]
    h, w = LI()
    II()
    a = LI()
    ans = [[None] * w for i in range(h)]
    x = 0
    y = 0
    diarect = 0
    for num, ai in enumerate(a, start=1):
        for _ in range(ai):
            ans[y][x] = num
            x += move[diarect][0]
            y += move[diarect][1]
            if 0 <= x < w and 0 <= y < h:
                if ans[y][x]:
                    x -= move[diarect][0]
                    y -= move[diarect][1]
                    diarect += 1
                    diarect %= 4
                    x += move[diarect][0]
                    y += move[diarect][1]
                continue
            x -= move[diarect][0]
            y -= move[diarect][1]
            diarect += 1
            diarect %= 4
            x += move[diarect][0]
            y += move[diarect][1]
            
    for a in ans:
        print(" ".join(map(str, a)))
    return


if __name__ == '__main__':
    D()

Solution (Python):
H, W = map(int, input().split())
N = int(input())
A = [int(s) for s in input().split()]
c = [[0] * W for _ in range(H)]
for y in range(H):
    f = reversed if y & 1 else lambda x: x
    for x in f(range(W)):
        A[-1] -= 1
        c[y][x] = len(A)
        if A[-1] == 0:
            A.pop()
for ci in c:
    print(*ci)

Solution (Python):
H, W = map(int, input().split())
N = int(input())
A = [int(s) for s in input().split()]
y = x = 0
d = "r"
c = [[0] * W for _ in range(H)]
while 0 <= y < H and 0 <= x < W and c[y][x] == 0:
    A[-1] -= 1
    c[y][x] = len(A)
    if A[-1] == 0:
        A.pop()
    if d == "r":
        if x < W - 1 and c[y][x + 1] == 0:
            x += 1
        else:
            d = "d"
            y += 1
    elif d == "d":
        if y < H - 1 and c[y + 1][x] == 0:
            y += 1
        else:
            d = "l"
            x -= 1
    elif d == "l":
        if 0 < x and c[y][x - 1] == 0:
            x -= 1
        else:
            d = "u"
            y -= 1
    elif d == "u":
        if 0 < y and c[y - 1][x] == 0:
            y -= 1
        else:
            d = "r"
            x += 1
for ci in c:
    print(*ci)

Solution (Python):
H, W = map(int, input().split())
N = int(input())
As = list(map(int, input().split()))
L = []
for i in range(N):
  A = As[i]
  L = L + [i+1]*A
for h in range(H):
  l = L[h*W:(h+1)*W]
  if h%2 == 1:
    l.reverse()
  print(" ".join(map(str, l)))

Solution (Python):

def main():
    """
    move left to right in odd rows and vice versa in even rows
    1 s--->
    2 <----
    3 --->d
    """
    H, W = tuple(map(int, input().split()))
    N = int(input())
    A = tuple(map(int, input().split()))
    F = []  
    for color, cnt in enumerate(A, 1):
        F += [color] * cnt
    for p, i in enumerate(range(1, H + 1), 1):
        if p & 1:  
            print(*F[(i - 1) * W : i * W])
        else:
            print(*F[(i - 1) * W : i * W][::-1])


if __name__ == "__main__":
    main()

Solution (Python):
H, W = map(int, input().split())
N = int(input())
a = []
for i, ai in enumerate(map(int, input().split())) :
    for _ in range(ai) :
        a.append(i+1)
a = a[::-1]

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

d = ((1, 0), (0, 1), (-1, 0), (0, -1))
di = 0
dx, dy = d[0]
x, y = 0, 0
for i in range(H * W) :
    c[y][x] = a.pop()
    if not (0 <= x + dx < W) or not (0 <= y + dy < H) or c[y+dy][x+dx] != 0 :
        di = (di + 1) % 4
        
    dx, dy = d[di]
    x += dx
    y += dy    
        
for i in range(H) :
    print(*c[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)

ans = []
for i in range(0, h*w, w):
    if (i // w) % 2 == 0:
        ans.append(b[i:i+w])
    else:
        ans.append(list(reversed(b[i:i+w])))


print('\n'.join([' '.join([str(aij) for aij in ai]) for ai in ans]))

Solution (Python):
h , w = map(int,input().split())
n = int(input())
a = tuple(map(int,input().split()))
line = []
for i in range (n) :
    line += [i+1] * a[i]
    
    
for j in range (h):
    p = j*w
    ans = line[p:p+w]
    
    if j % 2 == 1:
        ans = list(reversed(ans))
    for k in range(w-1) :
        print(ans[k],end=" ")
    print(ans[-1])

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

Solution (Python):

import sys
INF = float("inf")


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

    Y, X = 0, 0
    for i, av in enumerate(a):
        for j in range(av):
            ans[Y][X] = i+1
            if Y % 2 == 0 and X == W-1:
                Y += 1
            elif Y % 2 == 1 and X == 0:
                Y += 1
            elif Y % 2 == 0:
                X += 1
            else:
                X -= 1
    for i in range(H):
        print(*ans[i], sep=" ")

    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,n,*a=map(int,open(0).read().split());l=[];r=range
for i in r(n):l+=[i+1]*a[i]
for i in r(h):print(*l[w*i:w*i+w][::1-i%2*2])

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[w*i:w*i+w][::1-i%2*2])

Solution (Python):
import math
import copy
from operator import mul
from functools import reduce
from collections import defaultdict
from collections import Counter
from collections import deque

from itertools import product

from itertools import permutations

from itertools import combinations

from itertools import accumulate
from bisect import bisect_left, bisect_right

import re







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


dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]
 
def i_inpl(): return int(input())
def l_inpl(): return list(map(int, input().split()))
def line_inpl(x): return [i_inpl() for _ in range(x)]

INF = int(1e50)
MOD = int(1e9)+7 


def create_grid(H, W, value = 0):
    return [[ value for _ in range(W)] for _ in range(H)]



def main():
    H, W = l_inpl()
    N = i_inpl()
    a = l_inpl()

    t = [ [i+1]*ai for i, ai in enumerate(a)]
    from itertools import chain
    t = list(chain.from_iterable(t))

    field = create_grid(H, W)
    for i in range(H):
        for j in range(W):
            ind = W*i+j
            if i % 2 == 0:
                field[i][j] = str(t[ind])
            else:
                field[i][W-j-1] = str(t[ind])

    ans = ""
    for fi in field:
        ans += " ".join(fi) + "\n"
    print(ans[:-1])

if __name__ == "__main__":
    main()

Solution (Python):



import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7

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

ans = [[None]*w for _ in range(h)]
for i in range(h):
    if i % 2:
        for j in range(w)[::-1]:
            for k in range(n):
                if a[k] > 0:
                    ans[i][j] = k+1
                    a[k] -= 1
                    break
    else:
        for j in range(w):
            for k in range(n):
                if a[k] > 0:
                    ans[i][j] = k+1
                    a[k] -= 1
                    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()))
ans = [[0]*W for _ in range(H)]
h = 0
nums= []
for i,ai in enumerate(a):
    for _ in range(ai):
        nums.append(i+1)

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

for a in ans:
    print(" ".join(map(str,a)))

Solution (Python):

import sys
import copy
import collections
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import math
import itertools
 




 
def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
 
def main():
    N,W = inputMap()
    N = inputInt()
    A = inputList()
    
    ans = []
    retu = 0
    tmp = []
    for i, val in enumerate(A):
        for j in range(val):
            tmp.append(i+1)
            if len(tmp) == W:
                if retu == 0:
                    ans.append(tmp)
                    tmp = []
                    retu += 1
                elif retu % 2 == 1:
                    ans.append(reversed(tmp))
                    tmp = []
                    retu += 1
                elif retu % 2 == 0:
                    ans.append(tmp)
                    tmp = []
                    retu += 1
                    
    for i, val in enumerate(ans):
        print(*val)
    
if __name__ == "__main__":
	main()

Solution (Python):
import bisect

h, w = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))
b = [0]
for i in range(n):
    b.append(b[-1] + a[i])
count = 0
for i in range(h):
    temp = []
    for j in range(w):
        temp.append(str(bisect.bisect(b, count)))
        count += 1
    if i % 2 == 1:
        temp.reverse()
    print(" ".join(temp))

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

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

Solution (Python):



import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline 

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

def pos(i):
    a,b = divmod(i,w)
    if a%2: return (a,w-1-b)
    else: return (a,b)

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

idx = 0
for i,ai in enumerate(a):
    for j in range(ai):
        x,y = pos(idx)
        b[x][y] = i+1
        idx += 1

for i in b:
    print(*i)

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

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

cur = 1
way = 1
r, c = 0, 0
flg = True
while cur <= n:
    ans[r][c] = cur
    a[cur] -= 1
    if a[cur] == 0:
        cur += 1

    if (w == 1) or ((c == 0 or c == w - 1) and (not flg)):
        r += 1
        way *= -1
        flg = True
    else:
        c += way
        flg = False
    

for aa in ans:
    print(*aa)

Solution (Python):
h,w=map(int,input().split())
n=int(input())
l=list(map(int,input().split()))
l1=[]
l2=[]
ans=[[0]*w for _ in range(h)]
for i in range(n):
    for j in range(l[i]):
        l1.append(i+1)

flag=1
for i in range(h):
    for j in range(w):
        if flag==1:
            l2.append([i,j])
        else:
            l2.append([i,w-j-1])
    flag*=(-1)
for i in range(h*w):
        ans[l2[i][0]][l2[i][1]]=l1[i]
for i in ans:
    print(*i)

Solution (Python):
import sys
input=sys.stdin.readline
h,w=map(int,input().split())
n=int(input())
a_list=list(map(int,input().split()))
ans=[[0]*w for i in range(h)]
count=0
for i in range(n):
    a=a_list[i]
    p_q=count//w
    p_r=count%w
    count=a+count
    n_q=count//w
    n_r=count%w
    if p_q==n_q:
        for j in range(p_r,n_r):
            ans[p_q][j]=i+1
    else:
        for j in range(p_r,w):
            ans[p_q][j]=i+1
        for k in range(p_q+1,n_q):
            for j in range(w):
                ans[k][j]=i+1
        for j in range(n_r):
            ans[n_q][j]=i+1
for i in range(h):
    if i%2==0:
        for j in range(w):
            print(ans[i][j],end='')
            print(' ',end='')
        print('')
    else:
        for j in range(w-1,-1,-1):
            print(ans[i][j],end='')
            print(' ',end='')
        print('')

Solution (Python):
import sys


fin = sys.stdin.readline
H, W = [int(elem) for elem in fin().split()]
N = int(fin())
a_list = tuple(int(elem) for elem in fin().split())
grid = [[-1] * W for _ in range(H)]
cur_row, cur_col = 0, 0
is_moving_forward = True
for color, a in enumerate(a_list, start=1):
    for _ in range(a):
        grid[cur_row][cur_col] = color
        if cur_col == W - 1 and is_moving_forward:
            cur_row += 1
            is_moving_forward = False
        elif cur_col == 0 and not is_moving_forward:
            cur_row += 1
            is_moving_forward = True
        else:
            cur_col += 1 if is_moving_forward else -1

for row in grid:
    print(*row)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

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

cnt = 0
ans = [[-1]*W for _ in range(H)]
tmp = a.pop(0)
color = 1
flag = 0
for i in range(H):
	for j in range(W):
		if i % 2 == 1:
			j = W - j - 1
		ans[i][j] = color
		cnt += 1
		if cnt >= tmp:
			if not a:
				flag = 1
				break
			cnt = 0
			tmp = a.pop(0)
			color += 1
	if flag:
		break


for i in ans:
	print(" ".join([str(x) for x in i]))

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

Solution (Python):
h, w = map(int, input().split())
n = int(input())
A = list(map(int, input().split()))
C = [[0] * w for _ in range(h)]
i = 0
j = 0
for k, a in enumerate(A):
    for _ in range(a):
        C[i][j] = k + 1

        if i % 2 == 0:
            if j < w - 1:
                j += 1
            else:
                i += 1
        else:
            if j > 0:
                j -= 1
            else:
                i += 1
for row in C:
    print(*row)

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

    ct = 0

    
    init = 0
    for color, a_i in enumerate(a, 1):
        for _ in range(a_i):
            if init == 0:
                h = 0
                w = 0
                ans[h][w] = color
                init = 1
                continue

            if 1 <= h and ans[h - 1][w] == 0:
                h -= 1
            elif w + 1 < W and ans[h][w+1] == 0:
                w += 1
            elif h + 1 < H and ans[h + 1][w] == 0:
                h += 1
            elif 1 <= w and ans[h][w-1] == 0:
                w -= 1

            ans[h][w] = color

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


if __name__ == "__main__":
    main()

Solution (Python):
H, W = map(int, input().split())
N = int(input())
tmp = list(map(int, input().split()))
A = []
for i, a in enumerate(tmp, start=1):
    A.extend([i] * a)
y, x = 0, 0
dy, dx = [0, 1, 0, -1], [1, 0, -1, 0]
k = 0
f = [[None] * W for i in range(H)]
for a in A:
    f[y][x] = a
    ny, nx = y + dy[k], x + dx[k]
    if ny < 0 or ny == H or nx < 0 or nx == W or f[ny][nx] != None:
        k = (k + 1) % 4
    y, x = y + dy[k], x + dx[k]
for s in f:
    print(*s)

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

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

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

ret = [0] * (h*w)

index = 0
color = 1
for a in alist:
    for i in range(a):
        ret[index+i] = color
    color += 1
    index += a

for i in range(h):
    if i%2 == 0:
        for c in ret[i*w:(i+1)*w]:
            print(str(c) + " ", end="")
    else:
        for c in ret[i*w:(i+1)*w][::-1]:
            print(str(c) + " ", end="")

    print()

Solution (Python):
def fill(v,cnt,num):
    global r
    global c

    
    
    
    while cnt:
        if r%2==0:
            while c<w:
                if cnt==0:
                    return
                v[r][c] = num
                cnt-=1
                c+=1
            c-=1
            r+=1
        else:
            while c>=0:
                if cnt==0:
                    return
                v[r][c]=num
                cnt-=1
                c-=1
            c+=1
            r+=1
    return

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)]
r,c = 0,0
for i in range(n):
    fill(ans,a[i],i+1)
for i in range(h):
    for j in range(w):
        print(ans[i][j],end=' ')
    print('')

Solution (Python):
def main():
    H, W = map(int, input().split())
    N = int(input())
    A = list(map(int, input().split()))
    ans = [[0] * W for _ in range(H)]
    i = 0
    j = 0
    d = 1
    for n in range(N):
        for _ in range(A[n]):
            ans[i][j] = str(n + 1)
            if (0 > d and j == 0) or (d > 0 and j == W - 1):
                i += 1
                d *= -1
                continue
            j += d
    for r in ans:
        print(' '.join(r))


main()

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

D = [[""] * W for _ in range(H)]
cnt = 0
i, j = 0, 0
while i < H:
  while j < W:
    if i % 2 == 1:
      k = W - j - 1
    else:
      k = j
    D[i][k] = str(cnt + 1)
    a[cnt] -= 1
    if a[cnt] == 0:
      cnt += 1
    j += 1
  j = 0
  i += 1

for k in D:
  print(" ".join(k))

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
MOD = 1000000007
def POW(x, y): return pow(x, y, MOD)
def INV(x, m=MOD): return pow(x, m - 2, m)
def DIV(x, y, m=MOD): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(input())

H,W=LI()
N=II()
INL=LI()

row = [0]*W
items = list(enumerate(INL, 1))

cur = 0
cnt = 0
for i in range(H):
    rn = range(W) if i%2 == 0 else reversed(range(W))
    for j in rn:
        row[j] = items[cur][0]
        cnt += 1
        
        if cnt >= items[cur][1]:
            cnt = 0
            cur += 1
    print(*row)

Solution (Python):
H,W = map(int,input().split())
N = int(input())
a = list(map(int,input().split()))
c = []
for i in range(N):
    c += [str(i+1)]*a[i]
for i in range(H):
    ans,c = c[:W],c[W:]
    if i%2==1:
        ans = ans[::-1]
    ans = ' '.join(ans)
    print(ans)