Problem:
 Score : 400 points    Problem Statement We have a grid with H rows and W columns of squares.
Snuke is painting these squares in colors 1 , 2 , ... , N .
Here, the following conditions should be satisfied:   For each i ( 1 ≤ i ≤ N ), there are exactly a_i squares painted in Color i . Here, a_1 + a_2 + ... + a_N = H W .  For each i ( 1 ≤ i ≤ N ), the squares painted in Color i are 4-connected . That is, every square painted in Color i can be reached from every square painted in Color i by repeatedly traveling to a horizontally or vertically adjacent square painted in Color i .   Find a way to paint the squares so that the conditions are satisfied.
It can be shown that a solution always exists.      Constraints  1 ≤ H, W ≤ 100  1 ≤ N ≤ H W  a_i ≥ 1  a_1 + a_2 + ... + a_N = H W         Input Input is given from Standard Input in the following format:  H  W  N  a_1  a_2  ...  a_N       Output Print one way to paint the squares that satisfies the conditions.
Output in the following format:  c_{1 1}  ...  c_{1 W}  :  c_{H 1}  ...  c_{H W}   Here, c_{i j} is the color of the square at the i -th row from the top and j -th column from the left.        Sample Input 1 2 2
3
2 1 1      Sample Output 1 1 1
2 3  Below is an example of an invalid solution:  1 2
3 1  This is because the squares painted in Color 1 are not 4-connected.       Sample Input 2 3 5
5
1 2 3 4 5      Sample Output 2 1 4 4 4 3
2 5 4 5 3
2 5 5 5 3       Sample Input 3 1 1
1
1      Sample Output 3 1

Solution (Python):

def draw(i):
    global p,q,hd,wd
    B[p][q] = i
    if (q < W-1 and hd != -1):
        hd = 1
        wd = 0
    elif (hd == 1 and q == W-1) or (hd == -1 and q == 0):
        hd = 0
        wd = 1
    elif (q > 0 and hd != 1):
        hd = -1
        wd = 0
    p += wd
    q += hd

if __name__ == '__main__':
    p = 0
    q = 0
    hd = 1
    wd = 0
    H, W = list(map(int, input().split()))
    N = int(input())
    As = list(map(int, input().split()))
    B = [[0 for i in range(W)] for j in range(H)]

    for i in range(N):
        for j in range(As[i]):
            draw(i+1)

    for i in range(H):
        s = list(map(str, B[i]))
        print(' '.join(s))

Solution (Python):

def draw(i):
    global p,q,hd,wd
    B[p][q] = i
    if (q < W-1 and hd != -1):
        hd = 1
        wd = 0
    elif (hd == 1 and q == W-1) or (hd == -1 and q == 0):
        hd = 0
        wd = 1
    elif (q > 0 and hd != 1):
        hd = -1
        wd = 0
    p += wd
    q += hd

if __name__ == '__main__':
    p = 0
    q = 0
    hd = 1
    wd = 0
    H, W = list(map(int, input().split()))
    N = int(input())
    As = list(map(int, input().split()))
    B = [[0 for i in range(W)] for j in range(H)]

    for i in range(N):
        for j in range(As[i]):
            draw(i+1)

    for i in range(H):
        s = list(map(str, B[i]))
        print(' '.join(s))

Solution (Python):

import random
h, w = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))
i = 0
for y in range(h):
    row = []
    for _ in range(w):
        row += [ i + 1 ]
        a[i] -= 1
        if a[i] == 0:
            i += 1
    if y % 2 == 1:
        row = reversed(row)
    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):
    while a[i] > 0:
        a[i] -= 1
        c.append(str(i + 1))

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

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

HW = [[0 for w in range(W)]for h in range(H)]
C = []

count = 1
for a in A:
    for i in range(a):
        C.append(count)
    count += 1

count = 0
for h in range(H):
    if h % 2 == 0:
        for w in range(W):
            HW[h][w] = C[count]
            count += 1
    else:
        for w in range(W):
            HW[h][W-w-1] = C[count]
            count += 1

for hw in HW:
    for s in hw:
        print(s, end=" ")
    print()

Solution (Python):
height, width = [int(s) for s in raw_input().split(" ")]
n = int(raw_input())
colorcounts = [int(s) for s in raw_input().split(" ")]
table = [[-1 for c in range(width)] for r in range(height)]


def direction(row):
    return 1 if even(i)else -1


def new_current_column(next_row, width):
    return 0 if even(next_row)else width - 1


def even(i):
    return i % 2 == 0


def nopainted_count(row, current_column, width):
    return width - current_column if even(row)else current_column + 1


def paint(table, row, cc, count, color):
    if even(row):
        table[row][cc:cc + count] = [color + 1] * count
    else:
        table[row][cc - count + 1:cc + 1] = [color + 1] * count


cur_color = 0
for i in xrange(0, height):
    column = new_current_column(i, width)
    while nopainted_count(i, column, width) > colorcounts[cur_color]:
        paint(table, i, column, colorcounts[cur_color], cur_color)
        column += direction(i) * colorcounts[cur_color]
        cur_color += 1
    paint(table, i, column, nopainted_count(i, column, width), cur_color)
    colorcounts[cur_color] -= nopainted_count(i, column, width)
for i in table:
    print " ".join([str(s) for s in i])

Solution (Python):
x = list(map(int, input().split()))
y = list(map(int, input().split()))
z = list(map(int, input().split()))
res=[]


for i in range(1,y[0]+1):
    for j in range(z[i-1]):
        res=res+[i]

sw=0
for j in range(0,x[0]):
    if sw==0:  
        for i in range(j*x[1],(j+1)*x[1]):
            print(res[i],end=' ')
        sw=1
        print('')
        continue
    if sw==1:
        for i in range((j+1)*x[1]-1,j*x[1]-1,-1):
            print(res[i],end=' ')
        sw=0
        print('')
        continue
    

quit()

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

l = []

for i,x in enumerate(a):
	for j in range(x):
		l.append(i+1)

for i in range(h):
	t = l[i*w:(i+1)*w]
	if i % 2 == 1:
		t.reverse()
	print(' '.join(map(str, t)))

Solution (Python):
import sys
import numpy as np

h,w = [int(x) for x in sys.stdin.readline().split()]
n = int(input())
a = [int(x) for x in sys.stdin.readline().split()]
s = h*w

l = []

for i,x in enumerate(a):
	for j in range(x):
		l.append(i+1)

l = np.array(l).reshape((h,w))

for i,x in enumerate(l):
	if i % 2 == 0:
		print(' '.join(map(str, x)))
	else:
		print(' '.join(map(str, reversed(x))))

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
gosa = 1.0 / 10**9
mod = 10**9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()


def main():
    h,w = LI()
    n = I()
    a = LI()

    r = [0] * (h*w)
    t = 0
    for i in range(1,n+1):
        c = a[i-1]
        for j in range(c):
            r[t+j] = i
        t += c
    rr = []
    for i in range(h):
        t = r[i*w:i*w+w]
        if i % 2 == 1:
            t.reverse()
        rr.append(t)

    return '\n'.join([' '.join(map(str, r)) for r in rr])



print(main())

Solution (Python):
import numpy as np

def grid_coloring():
	h,w = map(int, raw_input().split(" "))
	n = int(raw_input())
	a = map(int, raw_input().split(" "))
	_current_color = 1
	ans = np.zeros((h,w), dtype=int)
	for i in range(h):
		if i%2==0:
			for j in reversed(range(w)):
				if a[_current_color-1]!=0:
					ans[i][j]=_current_color
					a[_current_color - 1]-=1
				else:
					_current_color +=1
					ans[i][j] = _current_color
					a[_current_color - 1] -= 1
		else:
			for j in range(w):
				if a[_current_color-1]!=0:
					ans[i][j]=_current_color
					a[_current_color - 1]-=1
				else:
					_current_color +=1
					ans[i][j] = _current_color
					a[_current_color - 1] -= 1
	for i in range(h):
		print " ".join(map(str, ans[i]))


if __name__ == "__main__":
	grid_coloring()

Solution (Python):

import numpy as np
def GridColoring(H, W, N, a):
    a = np.array(map(int, a))
    
    c = np.zeros((H,W))
    direct = True
    x = 0
    y = 0
    for color in np.argsort(a)[::-1]:
        for i in range(a[color]):
            c[y,x] = color+1
            if ((x == 0) and not(direct)) or ((x == (W-1)) and direct):
                y += 1
                direct = not(direct)
            else:
                if direct:
                    x += 1
                else:
                    x -= 1
    return c

def output(X):
    import sys
    for i in range(X.shape[0]):
        for j in range(X.shape[1]):
            sys.stdout.write("%d " %X[i,j])
        sys.stdout.write("\n")



if __name__ == "__main__":
    l = raw_input().split()
    H = int(l[0])
    W = int(l[1])
    N = int(raw_input())
    a = raw_input().split()
    c = GridColoring(H,W,N,a)
    output(c)

Solution (Python):
import sys
from collections import deque
import copy
import numpy as np

from math import *
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [H, W] = [int(input_raw[0]), int(input_raw[1])]
    input_raw = read_func().strip().split()
    [N] = [int(input_raw[0])]
    input_raw = list(read_func().strip().split())
    
    
    color_num_list = np.zeros((N))
    for i in range(N):
        color_num_list[i] = (int(input_raw[i]))

    index_list = np.argsort(color_num_list)[::-1]
    sorted_color_num = np.sort(color_num_list)[::-1]

    mat = np.zeros((H, W),'int')
    now_i = 0
    now_j = 0
    rev_flag = False
    
    
        
        
        
        
    for i in range(N):
        color_num = sorted_color_num[i]
        color = index_list[i] + 1
        while color_num > 0:
            mat[now_i][now_j] = color
            if (now_j < W - 1 and rev_flag == False)  or  (now_j >= 1 and rev_flag == True):
                s = -1 if rev_flag == True else 1
                now_j += s
            else:
                now_i += 1
                rev_flag = not(rev_flag)
            color_num -= 1

    for i in range(H):
        for j in range(W):
            print mat[i][j],
        print


if __name__ == '__main__':
    main()

Solution (Python):

import numpy as np

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

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

ans_flat = np.array(ans_flat)
ans_sq = ans_flat.reshape(H,W)

for i in range(H):
    if i % 2 == 1:
        ans_sq[i] = ans_sq[i][::-1]
    print(' '.join(ans_sq[i].astype(str)))

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

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

index = [0, 0]
sign = 1
for color in range(N):
    for number in range(a[color]):
        out[index[0]][index[1]] = color + 1
        if index[0] == H - 1 and sign == 1:
            index[1] += 1
            sign = -1
        elif index[0] == 0 and sign == -1:
            sign = 1
            index[1] += 1
        else:
            index[0] += sign

for i in range(H):
    [print(x, end=' ') for x in out[i]]
    print()

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

    flatten = []
    for i in range(0, N):
        flatten.append([(i + 1) for j in range(a[i])])
    flatten_2 = []
    for tmp in flatten:
        flatten_2.extend(tmp)
    
    ans = [[] for i in range(H)]

    for i in range(0, H):
        if i % 2 == 0:
            ans[i] = list(map(str, flatten_2[i * W:(i + 1) * W]))

        else:
            ans[i] = flatten_2[i * W:(i + 1) * W]
            ans[i].reverse()
            ans[i] = list(map(str, ans[i]))

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

Solution (Python):


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

import numpy as np

H,W=spaceinput()
N=int(input())
a=spaceinput()

field=np.zeros([H,W])
k=0
for i in range(H):
    if i%2==0:
        for j in range(W):
            field[i][j]=k+1
            a[k]-=1
            if a[k]==0:
                k+=1

    else:
        for j in reversed(range(W)):
            field[i][j]=k+1
            a[k]-=1
            if a[k]==0:
                k+=1


for i in range(H):
    a=field[i]
    b=list(map(str,map(int,a.tolist())))
    
    print(" ".join(b))

Solution (Python):
h,w=map(int,raw_input().split())
n=int(raw_input())
a=map(int,raw_input().split())
b=[[0]*2 for i in range(n+1)]
for i in range(1,n+1):
	b[i][0]=a[i-1]
	b[i][1]=i
b=sorted(b, key=lambda x: -x[0])
c=[[0 for i in range(w)] for i in range(h)]
x=y=0
for i in range(n):
	for j in range(b[i][0]):
		c[x][y]=b[i][1]
		if x%2==0:
			y+=1
		else:
			y-=1
		if y>=w:
			x+=1
			y-=1
		if y<0:
			x+=1
			y+=1
for i in range(h):
	for j in range(w):
		print c[i][j],
	print

Solution (Python):
from heapq import heappush, heappop

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

a_list = []
for i in range(N):
    heappush(a_list, (a[i], i + 1))

c = [[0] * W for _ in range(H)]
remain = 0
for y in range(H):
    if y % 2 == 0:
        for x in range(W):
            if remain == 0:
                remain, num = heappop(a_list)
            c[y][x] = num
            remain -= 1
    else:
        for x in reversed(range(W)):
            if remain == 0:
                remain, num = heappop(a_list)
            c[y][x] = num
            remain -= 1

for row in c:
    s = ' '.join([str(i) for i in row])
    print(s)

Solution (Python):
def fill_left(k, y, x, a, ans):
    global cnt
    for j in xrange(x, w):
        ans[y][j] = k + 1
        cnt += 1
        if cnt == a[k]:
            return j
    return -1

def fill_right(k, y, x, a, ans):
    global cnt
    for j in xrange(x, -1, -1):
        ans[y][j] = k + 1
        cnt += 1
        if cnt == a[k]:
            return j
    return -1

h, w = map(int, raw_input().split())
n = int(raw_input())
a = map(int, raw_input().split())
ans = [[0] * w for _ in xrange(h)]
nowy = 0
nowx = 0
left = True
cnt = 0
k = 0
while nowy < h:
    if left:
        tmp = fill_left(k, nowy, nowx, a, ans)
        if tmp == -1:
            left = False
            nowx = w - 1
            nowy += 1
        else:
            if tmp == w - 1:
                left = False
                nowx = w - 1
                nowy += 1
                k += 1
                cnt = 0
            else:
                k += 1
                nowx = tmp + 1
                cnt = 0
    else:
        tmp = fill_right(k, nowy, nowx, a, ans)
        if tmp == -1:
            left = True
            nowx = 0
            nowy += 1
        else:
            if tmp == 0:
                left = True
                nowx = 0
                nowy += 1
                k += 1
                cnt = 0
            else:
                k += 1
                nowx = tmp - 1
                cnt = 0
for i in xrange(h):
    for j in xrange(w):
        print ans[i][j],
    print

Solution (Python):
[H,W]=[int(x) for x in raw_input().split()]
N=int(raw_input())
A=[int(x) for x in raw_input().split()]
B=[[0 for x in range(W)] for y in range(H)]
p=0
q=0
d=1
k=0
D=6
for i in range(1,N+1):
    for j in  range(A[i-1]):
        B[p][(q+k)*d]=i
        q+=1
        if q==W:
            p+=1
            q=0
            d*=-1
            k= not k
for i in range(H):
    print "".join([str(x).ljust(D) for x in B[i]])

Solution (Python):

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

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

result = ""
for h in range(H):
    tmp = [str(n) for n in list_[h*W:(h+1)*W]]
    if h%2==0:
        result += " ".join(tmp) + "\n"
    else:
        result += " ".join(tmp[::-1]) + "\n"
print(result[:-1])

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

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

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

Solution (Python):
H,W=map(int,raw_input().split())
N=int,raw_input()
a=map(int,raw_input().split())
c=[[0 for i in range(W)] for i in range(H)]
num=0
count=0
d=1
x=0
y=0
s=0
while x<W:
    count+=1
    if a[num]+s<count:
        s+=a[num]
        num+=1
    c[y][x]=num+1
    y+=d
    if d==1 and y==H:
        x+=1
        d=-1
        y-=1
    if d==-1 and y==-1:
        x+=1
        d=1
        y+=1
for i in c:
    for j in i:
        print j,
    print

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

c = []
for i in range(len(d)):
	for _ in range(d[i]):
		c.append(i+1)

for i in range(h):
	res = ""
	for j in range(w):
		if i % 2 == 0:
			res = res + str(c.pop()) + ' '
		else:
			res = str(c.pop()) + ' ' + res

	print(res)

Solution (Python):

INF = 10 ** 20
MOD = 10 ** 9 + 7


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


def read():
    H, W = ILI()
    N = II()
    a = ILI()
    return H, W, N, a


def solve(H, W, N, a):
    ans = [[None] * W for __ in range(H)] 
    ind_a = 0
    a_count = 0
    color_a = list(range(1, N + 1))
    for h in range(H):
        if h % 2 == 0:
            for w in range(W):
                ans[h][w] = color_a[ind_a]
                a_count += 1
                if a_count == a[ind_a]:
                    ind_a += 1
                    a_count = 0
        else:
            for w in reversed(range(W)):
                ans[h][w] = color_a[ind_a]
                a_count += 1
                if a_count == a[ind_a]:
                    ind_a += 1
                    a_count = 0

    ans = [" ".join(map(str, ele)) for ele in ans]
    ans = "\n".join(ans)
    return ans


def main():
    params = read()
    print(solve(*params))


if __name__ == "__main__":
    main()

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

b=[]
for i in range(N):
  b+=[i+1]*a[i]
ans=[0]*H
for i in range(H):
  if i%2==0:
    print(" ".join(map(str,b[i*W:(i+1)*W])))
  else:
    print(" ".join(map(str,b[(i+1)*W-1:(i)*W-1:-1])))

Solution (Python):
import sys




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


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


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


def read_str():
    return input()


def solve():
    h, w = read_int_list()
    n = read_int()
    a = read_int_list()
    c = [[0] * w for i in range(h)]
    color = [0] * (w * h)
    t = 0
    for k in range(n):
        for l in range(a[k]):
            color[t] = k + 1
            t += 1
    t = 0
    for i in range(h):
        if i % 2 == 1:
            for j in range(w):
                c[i][j] = color[t]
                t += 1
        else:
            for j in range(w - 1, -1, -1):
                c[i][j] = color[t]
                t += 1
    for i in range(h):
        for j in range(w):
            print(c[i][j], end='')
            end = ' '
            if j == w - 1:
                end = '\n'
            print(end=end)
    return 0


def main():
    res = solve()


if __name__ == '__main__':
    main()

Solution (Python):
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)]

cnt = 0
for i in range(n):
    for j in range(a[i]):
        r = cnt // w
        c = (r % 2) * (w - 1) + (-1)**(r % 2) * (cnt % w)
        ans[r][c] = i + 1
        cnt += 1

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

Solution (Python):
ri = lambda: map(int, raw_input().split())
def pi(a): print (' '.join(map(str, a)))

h, w = ri()
n = input()
a = ri()

g = [[i + 1] * x for i, x in enumerate(a)]
g = [x for l in g for x in l]

for i in xrange(h):
    l = g[i*w:i*w+w]
    if i & 1:
        l = l[::-1]
    pi(l)

Solution (Python):

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

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

i = 0
j = 0
ind = 1

for k in range(H*W):
    if i % 2 == 0:
        a_list[ind-1] -= 1
        res[i][j] = str(ind)
        j += 1
        if a_list[ind-1] == 0:
            ind += 1
        if j == W:
            j = W - 1
            i += 1
    else:
        a_list[ind-1] -= 1
        res[i][j] = str(ind)
        j -= 1
        if a_list[ind-1] == 0:
            ind += 1
        if j == -1:
            j = 0
            i += 1
        
for i in range(H):
    print(" ".join(res[i]))

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

ci = 0
for i in range(h):
    for j in range(w):
        if not a[ci]: ci+= 1
        if i%2==0: grid[i][j] = ci+1
        else: grid[i][~j] = ci+1
        a[ci]-= 1
for row in grid: print(*row)

Solution (Python):
def makelist(n, m):
	return [[0 for i in range(m)] for j in range(n)]

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

c = makelist(H, W)

y = 0
x = 0
vol = 1
for i in range(N):
	now = i + 1
	cnt = a[i]
	for k in range(cnt):
		c[y][x] = now
		if vol == 1:
			if x == W-1:
				y += 1
				vol = -vol
			else:
				x += vol
		elif vol == -1:
			if x == 0:
				y += 1
				vol = -vol
			else:
				x += vol
for y in range(H):
	for x in range(W):
		if x == 0:
			print(c[y][x], end="")
		else:
			print(" " + str(c[y][x]), end="")
	print()

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

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

x,y = 0,0
d = 1
for c,a in enumerate(A):
  c += 1

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

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

Solution (Python):
from itertools import chain

h, w = map(int, input().split())
n = int(input())
field = chain.from_iterable([i + 1] * a for i, a in enumerate(map(int, input().split())))
for r, row in enumerate(zip(*[field] * w)):
    if r & 1:
        print(*reversed(row))
    else:
        print(*row)

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

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

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

x = 0
y = 0
i = 0

d = 1
while i<N:
    grid[y][x] = i+1
    a[i]-=1
    if a[i]==0:
        i+=1

    x+=d
    
    if x==W:
        x-=1
        y+=1
        d= -1
        
    if x==-1:
        x+=1
        y+=1
        d = 1

for r in grid:
    print(" ".join([str(i) for i in r]))

Solution (Python):



import array
from bisect import *
from collections import *
import fractions
import heapq 
from itertools import *
import math
import re
import string

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

ret = []
r = []
for i, a in enumerate(As):
    while a > 0:
        if len(r) == W:
            ret.append(r)
            r = []
        r.append(i + 1)
        a -= 1
ret.append(r)

for i, r in enumerate(ret):
    if i % 2:
        print(*r[::-1])
    else:
        print(*r)

Solution (Python):
H, W = map(int, input().split())
N = int(input())
A = [int(x) for x in input().split()]
board = [[0] * W for _ in range(H)]
colors = [i + 1 for i in range(N) for _ in range(A[i])]

for i in range(H):
    if i % 2 == 0:
        for j in range(W):
            board[i][j] = colors[i * W + j]
    else:
        for j in range(W):
            board[i][W-j-1] = colors[i * W + j]
for row in board:
    print(*row)

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