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):
import queue


def m():
    h, w = map(int, input().split())
    n = int(input())
    a = list(map(int, input().split()))
    q = queue.Queue()
    for i in range(1, n+1):
        for _ in range(a[i-1]):
            q.put(i)
    
    ans = [[0 for _ in range(w+1)] for _ in range(h+1)]

    for i in range(1, h+1):
        if i % 2 == 1:
            for k in range(1, w+1):
                ans[i][k] = q.get()
        else:
            for k in range(w, 0, -1):
                ans[i][k] = q.get()

    for i in range(1, h+1):
        for k in range(1, w+1):
            print(ans[i][k], end=' ')
        print('')

m()

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

M = [[0 for _ in range(W)] for _ in range(H)]
C = []
for i in range(len(A)):
    C += [str(i+1)]*A[i]

k = 0
for i in range(H):
    for j in range(W):
        if i%2==0:
            M[i][j] = C[k]
        else:
            M[i][W-1-j] = C[k]
        k += 1

for i in range(H):
    print(" ".join(M[i]))

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 += ' '.join(s)
        ans += '\n'
    ans = ans[:-1]
    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()))
ans = []
for i in range(H):
    ans.append([0]*W)
m = 0
j = 0
k = 0
for i in range(len(a)):
    m = a[i]
    while m > 0:
        ans[j][k] = str(i+1)
        m -= 1
        if (j%2 == 0 and k == W-1) or (j%2 == 1 and k == 0):
            j += 1
        elif j%2 == 0:
            k += 1
        else:
            k -= 1
for i in range(H):
    print(" ".join(ans[i]))

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

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

ans = []
for color, num in enumerate(A, 1):
    ans += [str(color)] * num

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

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

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

cnt=0
for i in range(H):
  if i%2==0:
    for j in range(W):
      if A[cnt]!=0:
        ans[i][j]=(cnt+1)
        A[cnt]-=1
      if A[cnt]==0:
        cnt+=1
  else:
    for k in range(W-1,-1,-1):
      if A[cnt]!=0:
        ans[i][k]=(cnt+1)
        A[cnt]-=1
      if A[cnt]==0:
        cnt+=1
for i in range(H):
  for j in range(W):
    if j==(W-1):
      print(ans[i][j])
    else:
      print(ans[i][j],"",end="")

Solution (Python):
H, W = map(int, input().split())
N=int(input())
a=[int(i) for i in input().split()]
c = [[0 for i in range(W)]for j in range(H)]
n=0
k=0
for i in range(H):
    for j in range(W):
        if i%2==0:
            c[i][j]=n+1
        else:
            c[i][W-1-j]=n+1
        k+=1
        if k==a[n]:
            k=0
            n+=1
for i in range(H):
    st=""
    for j in range(W):
        st+=str(c[i][j])+" "
    print(st)

Solution (Python):
from collections import deque
h,w = [int(i) for i in input().split()]
n = int(input())
a0= [int(i) for i in input().split()]
a = []
for i,j in enumerate(a0):
  a.extend([str(i+1) for _ in range(j)])
a.sort()

f=True
for i in range(h):
  n=[]
  for j in range(w):
    n.append(a.pop())
  n.sort(reverse=f)
  print(*n)
  f=not f

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

s=[]
for ind, i in enumerate(a):
	for j in range(i):
		s.append(str(ind+1))

for i in range(H):
	tmp_s=s[i*W:i*W+W]
	if i%2==1:
		print ' '.join(tmp_s[::-1])
	else:
		print ' '.join(tmp_s)

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

M = []
for i in range(n):
    M += [i+1]*A[i]
for i in range(h):
    print(*M[w*i:w*i+w][::2*(i%2)-1])

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


def foo(xs):
    for i, x in enumerate(xs):
        for j in range(x):
            yield str(i+1)

left_to_right = True
gen = foo(xs)
for i in range(h):
    ys = []
    for j in range(w):
        ys.append(next(gen))
    if left_to_right:
        print(" ".join(ys))
    else:
        print(" ".join(reversed(ys)))
    left_to_right = not left_to_right

Solution (Python):


import sys

H, W = [int(w) for w in sys.stdin.readline().split()]
N = int(sys.stdin.readline())
a = [int(w) for w in sys.stdin.readline().split()]

color = 0
for y in range(H):
  row = []
  for x in range(W):
    while a[color] <= 0: color += 1
    row.append(color + 1)
    a[color] -= 1
  if y % 2 != 0: row = reversed(row)
  s = ' '.join([str(x) for x in row])
  print(s)

Solution (Python):


import sys

H, W = [int(w) for w in sys.stdin.readline().split()]
N = int(sys.stdin.readline())
a = [int(w) for w in sys.stdin.readline().split()]

m = sorted([(i,a[i]) for i in range(len(a))], key=lambda p: p[1], reverse=True)

color = 0
for y in range(H):
  row = []
  for x in range(W):
    while m[color][1] <= 0: color += 1
    row.append(m[color][0] + 1)
    m[color] = (m[color][0], m[color][1] - 1)
  if y % 2 != 0: row = reversed(row)
  s = ' '.join([str(x) for x in row])
  print(s)

Solution (Python):



import itertools

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

m = sorted([(i,a[i]) for i in range(len(a))], key=lambda p: p[1], reverse=True)

color = 0
for y in range(H):
  s = ''
  for x in range(W):
    while m[color][1] <= 0: color += 1
    if s != '': s += ' '
    s += str(m[color][0] + 1)
    m[color] = (m[color][0], m[color][1] - 1)
  if y % 2 != 0:
      s = s.split()
      s.reverse()
      s = ' '.join(s)
  print(s)

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

grid = [[0]*W for _ in range(H)]
grid_iter = [0,0]
for i in range(N):
	for j in range(a[i]):
		grid[grid_iter[1]][grid_iter[0]] = i+1

		if grid_iter[0] == 0 and grid_iter[1] % 2 == 1:
			grid_iter = [0,grid_iter[1]+1]

		elif grid_iter[0] == W-1 and grid_iter[1] % 2 == 0:
			grid_iter = [W-1,grid_iter[1]+1]

		else:
			if grid_iter[1] % 2 == 1:
				grid_iter[0] -= 1
			else:
				grid_iter[0] += 1

for i in range(H):
	for j in range(W):
		print(str(grid[i][j]) + " ",end = "")
	print()

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

dy = 1
x = 0
y = 0
n = 1
for i in range(H*W):
    ans[y][x] = str(n)
    a[0] -= 1
    if a[0] == 0:
        a = a[1:]
        n += 1
    if y == H-1 and dy == 1:
        x += 1
        dy = -1
    elif y == 0 and dy == -1:
        x += 1
        dy = 1
    else:
        y += dy

for i in range(H):
    print(' '.join(ans[i]))

Solution (Python):
h, w    = list(map(lambda v:int(v), input().split()))
input()
colors  = list(map(lambda v:int(v), input().split()))

color_idx   = 0
color_output_count  = 0
res     = []

for y in range(h):
    row = []
    for x in range(w):
        row.append(color_idx + 1)
        color_output_count += 1
        if (colors[color_idx] == color_output_count):
            color_idx   += 1
            color_output_count  = 0

    if (y % 2) == 1:
        row.reverse()

    res.append(row)


for y in range(h):
    for x in range(w):
        print(str(res[y][x]) + " ", end="")
    print("")

Solution (Python):
H,W = map(int, raw_input().split(" "))
N = int(raw_input())
a = map(int, raw_input().split(" "))

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

x = 0
y = 0
for i in xrange(len(a)):
    for j in xrange(a[i]):
        ans[y][x] = i + 1
        if x < W - 1:
            x += 1
        else:
            x = 0
            y += 1

for i,l in enumerate(ans):
    for j in l if(i%2==0)else l[::-1]:
        print j,
    print ""

Solution (Python):

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

index = np.argsort(a)[::-1]+1
a_sorted = np.sort(a)[::-1]

ans = np.zeros((h,w)).astype(dtype='int')

row,col,color = 0,0,index[0]
a_index = 0
col_direction = 1
cnt = 0
for i in range(h*w):
    ans[row][col] = color
    col += col_direction
    if(col == w):
        col_direction = -1
        col -= 1
        row += 1
    elif(col == -1):
        col_direction = 1
        row += 1
        col += 1
    cnt += 1
    if(cnt == a_sorted[a_index]):
        
        cnt = 0
        a_index += 1
        if(a_index == len(a)):
            a_index = len(a)-1
        color = index[a_index]
        

anslist = ans.tolist()
for i in range(h):
    for j in range(w):
        print(ans[i][j],end='')
        if(j != w-1):
            print(' ',end='')
    print('')

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

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

Solution (Python):

"""
http://abc069.contest.atcoder.jp/tasks/arc080_b

"""
from itertools import repeat

def walk_grid(H, W):
    
    
    
    x, y = 0, 0
    step = 1
    while True:
        yield (x, y)
        y += step
        if y >= H or y < 0:
            step *= -1
            y += step
            x += 1


def solve(repeats, H, W, N):
    grid = [['.']*W for _ in range(H)]
    pos = walk_grid(H, W)
    for c, r in enumerate(repeats, start=1):
        for _ in range(r):
            x, y = pos.__next__()
            grid[y][x] = c
    return grid


def main():
    H, W = map(int, input().split())
    N = int(input())
    repeats = [int(x) for x in input().split()]
    result = solve(repeats, H, W, N)
    for r in result:
        print(*r)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import defaultdict
from itertools import product, groupby
from math import pi
from collections import deque
from bisect import bisect, bisect_left, bisect_right
INF = 10 ** 10


def main():
    H, W = map(int, input().split())
    N = int(input())
    a_list = list(map(int, input().split()))
    c_list = []
    for i, a in enumerate(a_list, start=1):
        c_list += [i] * a

    ans = [[0] * W for _ in range(H)]
    for i, c in enumerate(c_list):
        h = i // W
        if h % 2 == 0:
            ans[h][i % W] = c
        else:
            ans[h][-(i % W + 1)] = c

    for line in ans:
        print(*line)


if __name__ == '__main__':
    main()

Solution (Python):
def disp(c):
    for cc in c:
        print(cc, end=' ')
    print()

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

i = 0
for x in range(H):
    c = []
    for y in range(W):
        c.append(i + 1)
        a[i] -= 1
        if a[i] == 0:
            i += 1

    if x % 2 == 0:
        disp(c)
    else:
        disp(c[::-1])

Solution (Python):
H, W = map(int, raw_input().split())
N = input()
a = map(int, raw_input().split())
x = 0
B = [[-1 for x in range(W)] for y in range(H)]
z = 0
for i in range(N):
    for j in range(a[i]):
        x, y = z % W, z / W
        if y % 2 == 1:
            x = W - x - 1
        B[y][x] = i+1
        z += 1

for y in range(H):
    for x in range(W):
        print B[y][x],
    print

Solution (Python):
H,W = (int(i) for i in input().split())
N = int(input())
aini = [int(j) for j in input().split()]
al = []
for n in range(len(aini)):
	al.extend([n+1 for o in range(aini[n]) ])
ans = [al[l:l+W] for l in range(0,len(al),W)]

for m in range(H):
	if (m+2)%2!=0:
		ans[m] = ans[m][::-1]		
	print(' '.join(map(str,ans[m])))

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

    c = [0] * (h*w)
    
    focus = 0
    for i in range(n):
        for j in range(a[i]):
            c[focus] = i+1
            focus += 1

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

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

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

for index,i in enumerate(a):
    for j in range(i):
        aa=count//W
        bb=count%W
        if aa%2==0:
            l[aa][bb]=index+1
        else:
            l[aa][W-bb-1]=index+1
        count+=1
        
for i in l:
    print(" ".join(map(str,i)))

Solution (Python):
H,W = map(int, input().split())
N = int(input())
a = list(map(int, input().split()))
i = 0
s = ["" for j in range(H)]
for y in range(H):
    for x in range(W):
        if x != 0: s[y] = s[y] + " "
        s[y] = s[y] + str(i+1)
        a[i] = a[i] - 1
        if a[i] == 0:
            i = i+1
    if y%2 == 0 : print(s[y])
    else:
        print(" ".join(s[y].split()[::-1]))

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


space = []
mass = [[0 for i in range(M)] for j in range(H)]
count = 0


for i in range(N):
    for j in range(a[i]):
        space.append(i+1)  



for k in range(H):
    for n in range(M):
        mass[k][n] = str(space[count])
        count += 1

for l in range(H):
  if l%2 == 1:
      mass[l].reverse()


for x in range(H):
  print(" ".join(mass[x]))

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


c = 1

for height in range(1,H+1):
    colors=[]
    for width in range(1, W+1):
        if a[c-1]>0:
            a[c-1] = a[c-1]-1
        else:
            c = c+1
            a[c-1] = a[c-1]-1
        if height%2 == 0:
            colors = [c] + colors
        else:
            colors = colors + [c]
    else:
        for width in range(1, W+1):
            print(colors[width-1], end=" ")
        else:
            print("")

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

ar = []
i = 0
for a in A:
    i += 1
    for _ in range(a):
        ar.append(i)


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

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

for h in range(H):
    for w in range(W):
         print('{} '.format(ls[h][w]),end="")
    print()

Solution (Python):


mozir=input()
hyo=mozir.split(' ')
Height=int(hyo[0])
Width=int(hyo[1])

N=int(input())

mozir=input()
hyo=mozir.split(' ')
kosuu=[int(mono) for mono in hyo]

color_ptr=0
cnt=0

muki='R'
for tate in range(0,Height,1):
    demoji=''
    for yoko in range(0,Width,1):
        if muki=='R':
            demoji=demoji+str(color_ptr+1)+' '
        else:
            demoji=str(color_ptr+1)+' '+demoji
        
        cnt+=1
        if cnt == kosuu[color_ptr]:
            color_ptr+=1
            cnt=0

    print(demoji[0:len(demoji)-1])
    muki='R' if muki=='L' else 'L'

Solution (Python):
h,w = map(int, raw_input().split())
n = input()
co = map(int, raw_input().split())
list = []
for x in range(1,n+1):
    for y in range(0,co[x-1]):
        list.append(str(x))
for x in range(0,h):
    if x % 2 == 0:
        print(" ".join(list[w*x:w*(x+1)]))
    else:
        rlist = list[w*x:w*(x+1)]
        rlist.reverse()
        print(" ".join(rlist))

Solution (Python):
h,w=map(int,input().split())
n=int(input())
arr=list(map(int,input().split()))
def nextPoint(i,j):
    if i%2==0 and j<w-1:
        return (i,j+1)
    elif i%2==1 and j>0:
        return (i,j-1)
    elif j==0 or j==w-1:
        return(i+1,j)

ans = [[0 for col in range(w)] for row in range(h)]
cur=(0,0)
for i in range(n):
    for _ in range(arr[i]):
        ans[cur[0]][cur[1]]=i+1
        cur=nextPoint(cur[0],cur[1])
for row in ans:
    print(' '.join(list(map(str,row))))

Solution (Python):
h,w=map(int,raw_input().split())
n=input()
g=map(int,raw_input().split())
l=[]
for i in range(n):
    l=l+[i+1]*g[i]
for i in range(h):
    l1=l[i*w:(i+1)*w]
    if i%2==1:
        l1.reverse()
    for j in l1:
        print j,
    print

Solution (Python):
def read(): return list(map(int, input().split()))


h, w = read()
n = int(input())
A = read()

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

for i in range(h):
    out = s[i * w: i * w + w]
    if i & 1:
        out = list(reversed(out))

    print(" ".join(out))

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

ci = 0
for i in range(H):
    arr = []
    while len(arr) < W:
        l = min(W - len(arr), src[ci])
        arr += [ci+1] * l
        src[ci] -= l
        if src[ci] == 0: ci += 1
    if i%2 == 1:
        arr.reverse()
    print(' '.join(map(str,arr)))

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

data = list(map(int, input().split()))









col = 1
count = 1
grid = [["" for w in range(W)] for h in range(H)]
for h in range(H):
    for w in range(W):
        grid[h][w] = str(col)
        if count == data[col - 1]:
            count = 1
            col += 1
        else:
            count += 1

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

Solution (Python):
h,w = map(int, raw_input().split())
n = int(raw_input())
l = map(int, raw_input().split())
for i in range(1,n):
    l[i] += l[i-1]
idx = 0
mp = [[0 for _ in range(w)] for _ in range(h)]
for y in range(h):
    for x in range(w):
        p = y * w + x + 1
        if p > l[idx]:
            idx += 1
        if y % 2 == 1:
            x = w - 1 - x
        mp[y][x] = idx + 1
for a in mp:
    print " ".join(map(str,a))

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

c=1
kc=0
ansc=0
ans=[]
for i in range(len(a)):
    for j in range(a[i]):
        ans.append(c)
    c=c+1
for i in range(h):
    for j in range(w):
        if kc%2!=0:
            for k in range(ansc+w-1,ansc-1,-1):
                print(ans[k],end=" ")
            ansc=ansc+w
            break
        else:
            print(ans[ansc],end=" ")
            ansc=ansc+1
    kc=kc+1
    print()

Solution (Python):
def Q4(H, W, N, lst):
    l = []
    for (x, i) in zip(lst, range(1, len(lst) + 1)):
        l += [i] * x
    for i in range(H):
        if i % 2 == 0:
            m = []
            for j in range(W):
                m += [l.pop(0)]
            for j in range(W):
                print('{}'.format(m[j]), end="")
                if j!=W-1:
                    print(' ',end="")
            print('\n')
        else:
            m = []
            for j in range(W):
                m += [l.pop(0)]
            m = m[::-1]
            for j in range(W):
                print('{}'.format(m[j]), end="")
                if j!=W-1:
                    print(' ',end="")
            print('\n')

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

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

mat = [] * h

line = []
for i in range(n):
    for j in range(a[i]):
        line.append(i+1)
        if len(line) == w:
            if len(mat) % 2 == 0:
                mat.append(line)
            else:
                line.reverse()
                mat.append(line)
            line = []
                
for i in mat:
    print(' '.join(map(str, i)))

Solution (Python):
H,W=[int(x) for x in input().split()]
N=int(input())
a=[int(x) for x in input().split()]
s=[]
for i in range(N):
    s= s+([str(i+1)]*a[i])
for l in range(H):
    if l%2==0:
        print(" ".join(s[W*l:W*(l+1)]))
    else:
        print(" ".join(s[W*l:W*(l+1)][::-1]))

Solution (Python):
import sys


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

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

    switch = True
    num_of_cells = 0
    color_num = 1
    index = 0
    for row in range(H):
        for col in range(W):
            if switch:
                color_matrix[row][col] = color_num
            else:
                color_matrix[row][(W - 1) - col] = color_num
            num_of_cells += 1
            if num_of_cells == A[index]:
                num_of_cells = 0
                color_num += 1
                index += 1

        switch = not switch

    
    for array in color_matrix:
        print(' '.join(map(str, array)))

main()

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

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

C = []
for i in range(H):
    C.append(c[i*W:(i+1)*W])

for i, row in enumerate(C):
    if i % 2 == 1:
        row.reverse()
    print(" ".join(map(str,row)))

Solution (Python):
import numpy as np

def main():
    H, W = [int(i) for i in input().split()]
    N = int(input())
    a = [int(i) for i in input().split()]
    
    color_ar = np.zeros((H, W), dtype=np.int32)
    
    
    y = 0
    x = 0
    
    for i in range(N):
        t = a[i]
        while t > 0:
            if(np.count_nonzero(color_ar) == 0):
                color_ar[0,0] = i + 1
                if((y + 1) % 2 == 0):
                    x -= 1
                else:
                    x += 1
                
                if((x < 0) or (x >= W)):
                    y += 1
                    if(x < 0):
                        x = 0
                    else:
                        x = W - 1
                    
            else:
                color_ar[y,x] = i + 1
                if((y + 1) % 2 == 0):
                    x -= 1
                else:
                    x += 1
                
                if((x < 0) or (x >= W)):
                    y += 1
                    if(x < 0):
                        x = 0
                    else:
                        x = W - 1
            t -= 1
            
    
    for y in range(H):
        for x in range(W):
            print(color_ar[y,x], end="")
            if(x < W - 1):
                print(" ",end="")
        if(y < H - 1 ):
            print("")
            
if __name__=="__main__":
    main()

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

grid = [[0 for x in range(W)] for y in range(H)]
x,y = 0,0
for i in range(N):
    while colors[i] > 0:
        grid[y][x] = i + 1
        if x % 2 == 0:
            if y == H - 1:
                x += 1
            else:
                y += 1
        else:
            if y == 0:
                x += 1
            else:
                y -= 1
                
        colors[i] -= 1
        
for i in range(H):
    for j in range(W):
        print(grid[i][j],end=" ")
    print()

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

T = [[None for i in range(W)] for i in range(H)]

h , w = 0, 0
down = True
first = True

ha = []
reverse = False
for i in range(W):
    if reverse:
        ha += zip(range(H)[::-1],[i] * H)
    else:
        ha += zip(range(H), [i] * H)
    reverse = not reverse

wa = []
b = 0
for c, k in enumerate(an):
    c += 1
    for i in range(k):
        h,w = ha[b]
        T[h][w] = c
        b += 1

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

Solution (Python):
import sys




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


def read_str_list():
    return input().split()


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


def read_str():
    return input()


def main():
    h, w = read_int_list()
    n = read_int()
    a = read_int_list()
    color = [0] * (h * w)
    t = 0
    for i in range(n):
        for k in range(a[i]):
            color[t] = i + 1
            t += 1

    c = [[0] * w for i in range(h)]
    t = 0
    for i in range(h):
        if i % 2 == 0:
            start = 0
            stop = w
            step = 1
        else:
            start = w - 1
            stop = -1
            step = -1

        for j in range(start, stop, step):
            c[i][j] = color[t]
            t += 1

    for row in c:
        print(*row)


main()

Solution (Python):
[h,w]=[int(i) for i in input().split()]
n=int(input())
a=[[int(i),x+1] for (x,i) in enumerate(input().split())]
a=sorted(a,key=lambda x:x[0])
a=a[::-1]
cc=[]
for i in a:
 for j in range(i[0]):
   cc.append(str(i[1]))
c=[[0 for i in range(w)]for j in range(h)]
def dfs(u,v):
    S=[]
    c[u][v]=cc.pop()
    S.append([u,v])
    while len(S):
        v=S.pop()
        for i in [[0,1],[1,0],[-1,0],[0,-1]]:
            if 0<=v[0]+i[0]<h and 0<=v[1]+i[1]<w and c[v[0]+i[0]][v[1]+i[1]]==0:
                c[v[0]+i[0]][v[1]+i[1]]=cc.pop()
                S.append([v[0]+i[0],v[1]+i[1]])
                break
dfs(0,0)
for i in range(h):
 print(" ".join(c[i][:]))

Solution (Python):
nums = [int(x) for x in input().split()]
H,W = nums[0],nums[1]
N = int(input())
A = [int(x) for x in input().split()]
co_index = [0 for i in range (N)]; co_index[0] = A[0]
for i in range(1,N):
    co_index[i] = co_index[i-1]+A[i]
field = [[0 for i in range(W)] for j in range(H)]

c = 1
check,index = 0,0
for i in range(H):
    for j in range(W):
        field[i][j] = c
        check += 1
        if check >= co_index[index]:
            c += 1
            index += 1

for i in range(H):
    if i % 2 == 1: field[i].reverse()
for line in field:
    line = [str(elem) for elem in line]
    print(" ".join(line))

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

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

table = [["0" for i in range(W)] for i in range(H)]
x,y,c = 0,0,1

for i in range(1,N+1):
    for j in range(a[i-1]):
        table[y][x] = str(i)
        if 0 <= x+c <= W-1:
            x += c
        elif (x+c == W)or(x+c == -1):
            c *= -1
            y += 1

for i in range(H):
    tmp = ' '.join(table[i])
    print(tmp)

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

    m = [[0 for i in range(W)] for j in range(H)]
    a = []
    j = 1
    for i in A:
        for _ in range(i):
            a.append(j)
        j += 1

    i = 0
    for h in range(H):
        for w in range(W):
            m[h][w] = a[i]
            i += 1

    flag = 1
    for c in m:
        if flag == -1:
            print(*c[::-1])
        else:
            print(*c)
        flag *= -1

main()

Solution (Python):
from operator import itemgetter

h,w = [int(i) for i in input().split()]
n = int(input())
a_list = [(j,int(i)) for j,i in enumerate(input().split(),1)]

a_list = sorted(a_list,key = itemgetter(1), reverse=True)
b_list = [i for i,j in a_list for _ in range(j)]

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

Solution (Python):
l = input().split()
H = int(l[0])
W = int(l[1])
N = int(input())


c= [[0 for i in range(W)] for j in range(H)]
color_num = [int(i) for i in input().split()]
row = 0
column = 0

for i in range(N):
    index = color_num[i]
    for j in range(0, index):
        c[row][column] = i+1

        if W!=1:
            if column % (W-1) == 0 and column != 0:
                row += 1
                column = 0
            else:
                column += 1
        else:
            row += 1

for i in range(H):
    if i%2 == 1:
        c[i].reverse()


for i in range(0, H):
    for j in range(0, W):
        print(c[i][j], end=' ')
    print('')

Solution (Python):
H,W = map(int,input().split())
N = int(input())
M = []
for _ in range(H):
  M.append([0]*W)
cr = 0
cc = 0
d = 1
for i,n in enumerate(map(int,input().split())):
  color = i+1
  for _ in range(n):
    M[cr][cc] = color
    if d == 1:
      if cc == W-1:
        cr += 1
        d = -d
      else:
        cc += 1
    else:
      if cc == 0:
        cr += 1
        d = -d
      else:
        cc -= 1
for i in range(H):
  print(' '.join(map(str,M[i])))

Solution (Python):
if __name__ == "__main__":
    h, w = map(int, input().split())
    n = int(input())
    an = list(map(int, input().split()))

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

    rows = [seq[s:e] for s, e in zip(range(0, h*w, w), range(w, h*w+w, w))]
    rows = [row if i%2 == 0 else row[::-1] for i, row in enumerate(rows)]

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

Solution (Python):

import numpy as np

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

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

grid = np.array(grid).reshape((H, W))

for i in range(H):
    if i % 2 ==0:
        print(' '.join(map(str, list(grid[i,:]))) )
    else:
        print(' '.join(map(str, list(grid[i,::-1]))) )