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()))
cur = [0,0]
grid = [[0]*w for _ in range(h)]
for i in range(n):
    color = str(i+1)
    remain = a[i]
    while remain > 0:
        y = cur[0]
        x = cur[1]
        grid[y][x] = color
        remain -= 1
        if y % 2 == 0:
            if x == w-1:
                cur[0] += 1
            else:
                cur[1] += 1
        else:
            if x == 0:
                cur[0] += 1
            else:
                cur[1] -= 1
for i in range(h):
    print(" ".join(grid[i]))

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

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

ans = []

for i in range(len(a)):
    ans = ans + [i+1]*a[i]
for i in range(h):
    if i%2 == 0:
        c = ans[w*i:w*(i+1)]
        print(" ".join(map(str, c)))
    else:
        c = ans[w*i:w*(i+1)]
        c1 = c[::-1]
        print(" ".join(map(str, c1)))

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

b = [0 for _ in range(N)]
b[0] = a[0]
for i in range(N-1):
    b[i+1] = b[i] + a[i]

C = [[None for _ in range(W)] for _ in range(H)]

pt = 0
for i in range(N):
    for j in range(a[i]):
        s,t = pt // W,pt % W
        C[s][t] = i+1
        pt += 1

for i in range(H):
    if i % 2 == 1:
        for j in range(W//2):
            C[i][j],C[i][W-j-1] = C[i][W-j-1],C[i][j]

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()))
G=[[0]*W for i in range(H)]
tate,yoko,now=0,0,0
rev=False
cnt=0
while(cnt<H*W):
    G[tate][yoko]=now+1
    a[now]-=1
    if(a[now]==0):
        now+=1
    if(not rev):
        if(tate<H-1):
            tate+=1
        else:
            yoko+=1
            rev=not rev
    else:
        if(tate>0):
            tate-=1
        else:
            yoko+=1
            rev=not rev
    cnt+=1
    
    
for i in range(H):
    print(*G[i])

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

lst = [[0]*W for i in range(H)]
cnt = 0
color = 1
i = 0

while True:
    for j in range(W):
        lst[i][j] = color
        cnt += 1
        if cnt == a[color-1]:
            color += 1
            cnt = 0
    if color == N+1:
        break
    i += 1
    for j in range(W-1,-1,-1):
        lst[i][j] = color
        cnt += 1
        if cnt == a[color-1]:
            color += 1
            cnt = 0
    if color == N+1:
        break
    i += 1
for row in lst:
    print(*row)

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

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

direction = 0
posx = 0
posy = 0
for i in range(n):
    for j in range(a_l[i]):
        ans[posy][posx] = i+1
        if w == 1:
            direction = 1
        if direction == 0:
            posx += 1
            if posx == w-1:
                direction = 1
        elif direction == 1:
            posy += 1
            direction = 2
        elif direction == 2:
            posx -= 1
            if posx == 0:
                direction = 3
        elif direction == 3:
            posy += 1
            direction = 0

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

Solution (Python):
from collections import deque
h , w = map(int,input().split())
n = int(input())
a = list(map(int,input().split()))
p = 1
d = deque()
for i in range(n):
    for j in range(a[i]):
        if len(d) == w:
            print(*d)
            d = deque()
            p *= -1
        if p == 1:
            d.append(i+1)
        elif p == -1:
            d.appendleft(i+1)
if len(d) != 0:
    print(*d)

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

Solution (Python):
def main():
    h, w = map(int, input().split())
    n = int(input())
    a = [int(x) for x in input().split()]
    answer = [[0 for _ in range(w)] for _ in range(h)]
    dir = [[0, w, 1], [-1, -w - 1, -1]]
    count = 0
    color = 1
    for i in range(h):
        for j in range(dir[i % 2][0], dir[i % 2][1], dir[i % 2][2]):
            answer[i][j] = color
            count += 1
            if count == a[color - 1]:
                count = 0
                color += 1
    for ans in answer:
        print(" ".join(map(str, ans)))


if __name__ == '__main__':
    main()

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

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

num = a.pop(0)
color = 1

for i in range(h):
    for j in range(w):
        if num == 0:
            num = a.pop(0)
            color += 1

        c[i][j] = color
        num -= 1
for i in range(h):
    temp = c[i]
    if i % 2 == 1:
        temp = temp[::-1]
    print(*temp)

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 

def ii(): return int(sys.stdin.readline().rstrip()) 
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) 
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]

def ss(): return sys.stdin.readline().rstrip() 
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) 
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]





h,w=mii()
n=ii()
arr=limii()

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

    if i%2==0:
        for j in range(w):
            mat[i][j]=temp+1
            arr[temp]-=1
            if arr[temp]==0:
                temp+=1
    if i%2==1:    
        for j in reversed(list(range(w))):
            mat[i][j]=temp+1
            arr[temp]-=1
            if arr[temp]==0:
                temp+=1

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

Solution (Python):

H, W = map(int, input().split())
N = int(input())
a = input().split()
a_i = [int(s) for s in a]
result = [[0] * W for i in range(H)]
count = 0

for i in range(H):
    if(i % 2 == 0):
        
        for j in range(W):
            if(a_i[count] > 1):
                result[i][j] = count + 1
                a_i[count] -= 1
            elif(a_i[count] == 1):
                result[i][j] = count + 1
                count += 1
                
    if(i % 2 != 0):
        
        for j in range(W-1, -1, -1):
            if(a_i[count] > 1):
                result[i][j] = count + 1
                a_i[count] -= 1
            elif(a_i[count] == 1):
                result[i][j] = count + 1
                count += 1

                
print("\n".join([" ".join(map(str, l)) for l in result]))

Solution (Python):

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

ans=np.array([[-1]*(w+2)] + [[-1]+[0]*w+[-1] for i in range(h)] + [[-1]*(w+2)])
opt = [[0,1],[1,0],[0,-1],[-1,0]]
temp=[1,1]

for i in range(n):
    num=a[i]
    while num:
        ans[temp[0]][temp[1]] = i+1
        for j in opt:
            if ans[temp[0]+j[0]][temp[1]+j[1]] == 0:
                temp[0]+=j[0]
                temp[1]+=j[1]
                break
        num-=1

for i in range(1,h+1):
    print(*ans[i][1:-1])

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

for i, a in enumerate(A, 1):
  q += [str(i)] * a
  
for h in range(H):
  row = []
  for _ in range(W):
    row.append(q.pop(0))
  if h % 2 != 0:
    row.reverse()
  print(' '.join(x for x in row))

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

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

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

back = 1
c = 1
count = 0
for i in range(H):
  for j in range(W)[::back]:
    C[i][j] = c
    count+=1
    if count == A[c-1]:
      c += 1
      count = 0

  back = -back

for c in C:
  print(" ".join(map(str, c)))

Solution (Python):
import sys

h, w = map(int, sys.stdin.readline().rstrip().split())
n = int(sys.stdin.readline().rstrip())
a = list(map(int, sys.stdin.readline().rstrip().split()))
ans = []
i = 1
for ai in a:
    for j in range(ai):
        ans.append(i)
    i += 1

for i in range(h):
    if i % 2:
        print(' '.join(map(str, reversed(ans[w * i: w * (i + 1)]))))
    else:
        print(' '.join(map(str, ans[w * i: w * (i + 1)])))

Solution (Python):
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def MI(): return map(int, input().split())


def S(): return input()


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


def print_matrix(mat):
    for i in range(len(mat)):
        print(*mat[i])


YNL = {False: 'No', True: 'Yes'}
YNU = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()

nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']



show_flg = False


def main():
    H, W = MI()
    N = I()
    a = LI()
    v = []

    for i in range(len(a)):
        for j in range(a[i]):
            v.append(i+1)

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

    for i in range(len(v)):
        x = i % W
        y = i // W
        if y % 2 == 0:
            graph[y][x] = v[i]
        else:
            graph[y][W-x-1] = v[i]

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


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque
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)
b=deque(b)
ans=[]
for hi in range(h):
  c=[0]*w
  for wi in range(w):
    if hi%2==0:
      j=wi
    else:
      j=w-wi-1
    c[j]=b.popleft()
  ans.append(c)
for ci in ans:
  print(' '.join(map(str,ci)))

Solution (Python):


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

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

i = j = 0
t = 0
flag = True 
while i < h:
    num = nums[t]
    while num:
        matrix[i][j] = t + 1
        num -= 1
        if flag:
            if j + 1 == w:
                i += 1
                flag = False
            else:
                j += 1
        else:
            if j == 0:
                i += 1
                flag = True
            else:
                j -= 1
    t += 1

for r in matrix:
    print(*r)

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+=([str(i+1)]*a)

import numpy as np
ans=[[0]*W for _ in range(H)]
for h in range(H):
    for w in range(W):
        if h%2==0:
            ans[h][w]=L.pop(0)
        else:
            ans[h][-(w+1)]=L.pop(0)

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

Solution (Python):
import sys, re, os
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 S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

H, W = MAP()
N = INT()
A = LIST()
i = 0
C = [[0] * W for i in range(H)]
k = 0
cnt = 0
for h in range(H):
    if h % 2 == 0:
        for w in range(W):
            C[h][w] = k + 1
            A[k] -= 1
            if A[k] == 0:
                k += 1
    else:
        for w in range(W-1, -1, -1):
            C[h][w] = k + 1
            A[k] -= 1
            if A[k] == 0:
                k += 1

for c in C:
    print(*c, end="\n")

Solution (Python):
H,W=map(int,input().split())
N=int(input())
L=list(map(int,input().split()))
ans=[]
for i in range(N):
	for j in range(L[i]):
		ans.append((i+1))
for i in range(H):
	if i%2==0:
		print(*ans[i*W:(i+1)*W])
	elif i%2==1:
		tmp=ans[i*W:(i+1)*W]
		print(*tmp[::-1])

Solution (Python):
import sys,os
from collections import defaultdict, deque
from math import ceil, floor
if sys.version_info[1] >= 5:
    from math import gcd
else:
    from fractions import gcd
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    MOD = 10**9+7

    H,W = LMIIS()
    N = II()
    A = LMIIS()
    ai = 0
    for i in range(H):
        
        line = deque()

        
        if i % 2 == 0:
            j = 0
            while j < W:
                while j < W and A[ai] > 0:
                    
                    line.append(ai+1)
                    A[ai] -= 1
                    j += 1
                if A[ai] == 0 and ai < N-1:
                    ai += 1

            print(*line)
        
        else:

            j = W-1
            while 0 <= j:
                while 0 <= j and A[ai] > 0:
                    
                    line.appendleft(ai+1)
                    A[ai] -= 1
                    j -= 1
                if A[ai] == 0 and ai < N-1:
                    ai += 1
            
            print(*line)




    

if __name__ == '__main__':
    main()

Solution (Python):
import sys,os
from collections import defaultdict, deque
from math import ceil, floor
if sys.version_info[1] >= 5:
    from math import gcd
else:
    from fractions import gcd
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    MOD = 10**9+7

    H,W = LMIIS()
    N = II()
    A = LMIIS()
    ai = 0
    for i in range(H):
        
        line = []

        
        if i % 2 == 0:
            j = 0
            while j < W:
                while j < W and A[ai] > 0:
                    
                    line.append(ai+1)
                    A[ai] -= 1
                    j += 1
                if A[ai] == 0 and ai < N-1:
                    ai += 1

            print(*line)
        
        else:
            j = W-1
            while 0 <= j:
                while 0 <= j and A[ai] > 0:
                    
                    line.append(ai+1)
                    A[ai] -= 1
                    j -= 1
                if A[ai] == 0 and ai < N-1:
                    ai += 1
            
            print(*line[::-1])




    

if __name__ == '__main__':
    main()

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

for k in range(N):
    c += [k+1]*a[k]
now = 0

for k in range(H):
    if k%2 == 0:
        for l in range(W):
            ans[k][l] = c[now]
            now += 1
    else:
        for l in range(W-1,-1,-1):
            ans[k][l] = c[now]
            now += 1
for e in ans:
    print(*e)

Solution (Python):
H,W = map(int,input().split())
N = int(input())
A = list(map(int,input().split()))
A.insert(0,0)
C = [[0 for _ in range(W)] for _ in range(H)]
cur = [0,0,"R"]
for k in range(1,N+1):

    a = A[k]
    cnt = 0
    y,x,d = cur
    for i in range(y,H):
        y = i
        if d=="R":
            for j in range(x,W):
                C[i][j]=str(k)
                cnt += 1
                if j+1<W:
                    x = j+1
                else:
                    x = j
                if cnt==a:break
            if cnt==a:break
            d="L"
        else:
            for j in range(x,-1,-1):
                C[i][j]=str(k)
                cnt += 1
                if j>=1:
                    x = j-1
                else:
                    x = 0
                if cnt==a:break
            if cnt==a:break
            d="R"
    if x==W-1:
        if C[y][x]!=0:
            y += 1
            d="L"
    elif x==0:
        if C[y][x]!=0:
            y += 1
            d="R"
    cur = [y,x,d]                
for i in range(H):
    print(*C[i])

Solution (Python):
import sys
input= lambda: sys.stdin.readline().rstrip()
def pin(type=int):
    return map(type,input().split())


def resolve():
    H,W=pin()
    N,=pin()
    A=list(pin())
    aindex=0
    atemp=0
    for h in range(H):
        temp=[0]*W
        wren=range(W)
        if h%2!=0:wren=reversed(range(W))
        for w in wren:
            temp[w]=aindex+1
            atemp+=1
            if atemp==A[aindex]:
                aindex+=1;atemp=0
        print(*temp)
        

resolve()

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

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

h = 0
w = 0
w_flag = True 

for index, a in enumerate(A):
    count = 0
    while count < a:
        count += 1
        G[h][w] = index + 1
        if w_flag:
            if w == W - 1:
                h += 1
                w_flag = False
            else:
                w += 1
        else:
            if w == 0:
                h += 1
                w_flag = True
            else:
                w -= 1



for tmp in G:
    print (*tmp, sep = ' ')

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

def draw_color():
    global ans, ptr
    if A[ptr] > 0: 
        A[ptr] -= 1
        return ptr + 1
    ptr += 1
    return draw_color()



for h in range(H):
    if h % 2 == 0:
        for w in range(W):
            ans[h][w] = draw_color()
    else:
        for w in reversed(range(W)):
            ans[h][w] = draw_color()

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

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

X = []
for i in range(n):
    X += [i+1]*A[i]

for i in range(h):
    x = X[w*i:w*(i+1)]
    if i%2 == 0:
        print(*x)
    else:
        x.reverse()
        print(*x)

Solution (Python):
def main():
    H, W = (int(i) for i in input().split())
    _ = int(input())
    A = [int(i) for i in input().split()]
    c = [[0]*W for _ in range(H)]
    i = 1
    cnt = 0
    for h in range(H):
        rangeW = range(W) if h % 2 == 0 else range(W)[::-1]
        for w in rangeW:
            c[h][w] = i
            cnt += 1
            if cnt == A[i-1]:
                i += 1
                cnt = 0
    for row in c:
        print(*row)


if __name__ == '__main__':
    main()

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

for i, w in enumerate(range(0, H * W, W)):
    print(*ans[w: w + W][::(-1)**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)]

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

    else:
        for j in range(w-1,-1,-1):
            if a[k]==0:
                k+=1
            a[k]-=1
            ans[i][j]=k+1

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

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

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

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

    else:
        for j in range(w-1,-1,-1):
            if a[k]==0:
                k+=1
            a[k]-=1
            ans[i][j]=k+1

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

Solution (Python):
import sys
def main():
    input = sys.stdin.readline
    H,W=map(int, input().split())
    N=int(input())
    *A,=map(int, input().split())
    c=[[0] * W for _ in range(H)]
    ai=0
    for i in range(H):
        for j in range(W):
            k = j if i&1==0 else W-j-1
            c[i][k]=ai+1
            A[ai]-=1
            if A[ai]<=0: ai+=1
    for i in range(H):
        print(*c[i])

if __name__ == '__main__':
    main()

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

tab_sum = [0]
sum_tmp = 0
for ai in a:
    sum_tmp += ai
    tab_sum.append(sum_tmp)

ans = [[None] * W for _ in range(H)]
    
tab_idx = 1
for idx in range(H * W):
    y = idx // W
    if y % 2 == 0:
        x = idx % W
    else:
        x = (W - 1 - idx) % W
    
    if idx >= tab_sum[tab_idx]: tab_idx += 1
    ans[y][x] = tab_idx

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

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

color = []

for i, a in enumerate(A, 1):
    for _ in range(a):
        color.append(i)

flag = 1
for i in range(H):
    left = i * W
    right = left + W
    print(*color[left:right][::flag])
    flag *= -1

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    H,W=MI()
    N=I()
    a=LI()
    
    ans=[[0]*W for _ in range(H)]
    now=1
    for i in range(H):
        for jj in range(W):
            if i%2==1:
                j=W-1-jj
            else:
                j=jj
                

            if a[now-1]<=0:
                now+=1
            ans[i][j]=now
            a[now-1]-=1
            
    for i in range(H):
        print(' '.join(map(str, ans[i])))

            
            

main()

Solution (Python):
def main():
    h, w = map(int, input().split())
    n = int(input())
    a = [-1] + [int(x) for x in input().split()]
    for i in range(1, n + 1):
        a[i] += a[i - 1]
    answer = ["-1" for _ in range(w * h)]
    index = 0
    for i in range(h * w):
        if not (a[index] < i <= a[index + 1]):
            index += 1
        answer[i] = str(index + 1)
    for i in range(h):
        if i % 2 == 0:
            print(" ".join(answer[i * w: (i + 1) * w]))
        else:
            print(" ".join(reversed(answer[i * w: (i + 1) * w])))


if __name__ == '__main__':
    main()

Solution (Python):
def solve():
    h,w = map(int,input().split())
    ans = [[0 for _ in range(w)] for _ in range(h)]
    n = int(input())
    a = list(map(int,input().split()))
    now = 0
    for i in range(h):
        if i%2==0:
            for j in range(w):
                if a[now]>0:
                    ans[i][j] = now+1
                    a[now]-=1
                else:
                    now+=1
                    ans[i][j] = now+1
                    a[now]-=1
        else:
            for j in range(w)[::-1]:
                if a[now]>0:
                    ans[i][j] = now+1
                    a[now]-=1
                else:
                    now+=1
                    ans[i][j] = now+1
                    a[now]-=1
    for i in range(h):
        for j in range(w):
            print(ans[i][j],end=' ')
        print()
if __name__=='__main__':
    solve()

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

grid = []
for h in range(H):
    x = col[h * W: (h + 1) * W]
    if h % 2 == 0:
        x.reverse()
    grid.append(x)

for g in grid:
    print(*g)

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

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

H, W = map(int, readline().split())
N = int(readline())
Arr = []
A = list(map(int, readline().split()))
for i in range(N):
    Arr.extend([i+1]*A[i])

G = [Arr[W*i:W*(i+1)] for i in range(H)]
for i in range(0, H, 2):
    G[i].reverse()
for g in G:
    print(' '.join(map(str, g)))

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

ans = [[None]*W for _ in range(H)]
y = x = 0
for i,a in enumerate(A):
    for j in range(a):
        ans[y][x] = str(i+1)
        if y%2:
            if x==0:
                y += 1
            else:
                x -= 1
        else:
            if x==W-1:
                y += 1
            else:
                x += 1
for row in ans:
    print(*row)

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

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    H, W = map(int, input().split())
    n = int(input())
    A = list(map(int, input().split()))
    B = []
    for i in range(n):
        for _ in range(A[i]):
            B.append(i + 1)

    res = [[0] * W for _ in range(H)]
    for b in B:
        for h in range(H):
            for w in range(W):
                if res[h][w] == 0:
                    res[h][w] = b
                    break
            else:
                continue
            break

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


if __name__ == '__main__':
    resolve()

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

x, y = 0, 0
sign = 1
for idx, a in enumerate(A):
  idx += 1
  for _ in range(a):
    if x < 0 or x >= W:
      y += 1
      sign = sign * -1
      x += 1 * sign
    ans[y][x] = idx
    x += 1 * sign

for a in ans:
  print(*a)

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)]
i = 0
j = 0
flag = 0
cnt = 0
for p in range(n):
    color = p+1
    numberofcolor = a[p]
    while numberofcolor >0:

        if j > w-1:
            flag = 1- flag
            j = w-1
            i +=1
            continue
        if j <0:
            flag = 1-flag
            j = 0
            i+=1
            continue
        if i > h-1:break
        numberofcolor -= 1
        ans[i][j] = color
        if flag == 0:j +=1
        else:j-=1
            
for i in range(h):
    print(*ans[i])

Solution (Python):
import math  
import bisect  
import queue  


if __name__ == '__main__':
    H, W = map(int, input().split())
    N = int(input())
    A = list(map(int, input().split()))

    que = queue.Queue()
    for i, a in enumerate(A):
        que.put((a, i + 1))

    field = [[0 for _ in range(W)] for _ in range(H)]
    num, color = que.get()
    for i in range(0, H):
        if i % 2 == 0:
            j_range = range(0, W)
        else:
            j_range = range(W - 1, -1, -1)
        for j in j_range:
            if num == 0:
                num, color = que.get()
            field[i][j] = color
            num -= 1

    for row in field:
        print(*row)

Solution (Python):
h,w = map(int,input().split())
n = int(input())
a = list(map(int,input().split()))
c = [0] * w * h
an = [[] for i in range(h)]

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

Solution (Python):
import sys
import numpy as np


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(10 ** 9)


def main():
    H, W = map(int, input().split())
    N = int(input())
    A = list(map(int, input().split()))
    C = np.zeros(H * W, dtype=np.int32)
    first = 0
    for i, a in enumerate(A, 1):
        C[first : first + a] = i
        first += a

    ans = C.reshape((H, W))
    ans[1::2, :] = ans[1::2, ::-1]

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


if __name__ == "__main__":
    main()