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()))
ans = [[0]*W for _ in range(H)]
h = 0
w = 0
for i, a in enumerate(A):
    for _ in range(a):
        if w == W:
            h += 1
            w = 0
        
        if h % 2 != 0:
            ind = W-w-1
        else:
            ind = w
        ans[h][ind] = i+1
        w += 1
for _ in range(H):
    print(*ans[_])

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)
s = 0
for i in range(H):
    if i % 2== 0:
        print(*b[s:s+W])
    else:
        print(*b[s+W-1:s-1:-1])
    s += W

Solution (Python):
import numpy as np
H,W = map(int,input().split())
N = int(input())
lsa = list(map(int,input().split()))
ls2 = []
for i in range(N):
    ls2 += [i+1]*lsa[i]
arr = np.array(ls2)
arr = arr.reshape(H,W).tolist()
arr2 = []
for i in range(H):
    if i % 2 == 0:
        arr2.append(arr[i])
    else:
        arr[i].reverse()
        arr2.append(arr[i])
for i in range(H):
    arr2[i] = [str(j) for j in arr2[i]]
    print(' '.join(arr2[i]))

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

k = 0
for i in range(N):
    for j in range(a[i]):
        data[k].append(i + 1)
        if len(data[k]) == W:
            k += 1

for i in range(H):
    if i % 2 == 0:
        for j in range(W):
            print(data[i][j],'', end = '')
    else:
        for j in range(W):
            print(data[i][W - j - 1],'', end = '')
    print()

Solution (Python):
def main():
	H, W = [int(x) for x in input().split(" ")]
	N = int(input())
	A = [int(a) for a in input().split(" ")]
	c = [[0] * W for i in range(H)]
	k = 0
	for i in range(len(c)):
		for j in range(len(c[i])):
			if i % 2 == 0:
				c[i][j] = str(k + 1)
				A[k] -= 1
			elif i % 2 == 1:
				c[i][-j-1] = str(k + 1)
				A[k] -= 1
			if A[k] == 0:
				k += 1
	print("\n".join([" ".join(ci) for ci in c]))

main()

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

from collections import deque
q=deque([])
for i in range(n):
  for j in range(a[i]):
    q.append(i+1)

cnt=0
for i in range(h):
  s=[]
  for i in range(w):
    p=q.popleft()
    s.append(p)
  cnt+=1
  if cnt%2==0:
    s.reverse()
    for num in s:
      print(num,end=" ")
    print()
  else:
    for num in s:
      print(num,end=" ")
    print()

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

i=-1
j=0
ans=[]
for x in range(h):
  l=[]
  for y in range(w):
    i+=1
    if i==a[j]:
      i=0
      j+=1
    l.append(j+1)
  ans.append(l)
for i in range(0,len(ans),2):
  ans[i].reverse()
print("\n".join(map(lambda x:" ".join(map(str,x)),ans)))

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

A_ = []
for a_idx,a in enumerate(A):
    A_.extend([a_idx+1]*a)

i=0
for w in range(0,len(A_),W):
    i+=1
    ans=A_[w:w+W]
    if i%2 == 0:
        ans.reverse()
    print(' '.join(map(str,ans)))

Solution (Python):
import sys


stdin = sys.stdin
def ns(): return stdin.readline().rstrip()
def ni(): return int(stdin.readline().rstrip())
def nm(): return map(int, stdin.readline().split())
def nl(): return list(map(int, stdin.readline().split()))


def nextidx(h, x, y, dir):
    if y == h - 1 and dir == -1:
        return x + 1, y, 1
    elif y == 0 and dir == 1:
        return x + 1, y, -1
    else:
        return x, y - dir, dir


def main():
    h, w = nm()
    n = ni()
    A = nl()
    M = [[0] * w for _ in range(h)]
    dir = -1
    x = 0
    y = 0
    for i in range(n):
        for j in range(A[i]):
            M[x][y] = i + 1
            y, x, dir = nextidx(h, y, x, dir)
    for m in M:
        print(*m)


if __name__ == '__main__':
    main()

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

c=[[0 for i in range(W)] for i in range(H)]
k=0
for i in range(H):
    if i%2==0:
        for j in range(W):
            c[i][j]=k+1
            a[k]-=1
            if a[k]==0:
                k+=1
    else:
        for j in range(W-1,-1,-1):
            c[i][j]=k+1
            a[k]-=1
            if a[k]==0:
                k+=1
for i in range(H):
    for j in range(W):
        print(c[i][j],end=' ')
    print()

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

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

stdin = sys.stdin

ni = lambda: int(ns())
nf = lambda: float(ns())
na = lambda: list(map(int, stdin.readline().split()))
nb = lambda: list(map(float, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  

H, W = na()
N = ni()
a = na()
ans = []
for i in range(N):
    for _ in range(a[i]):
        ans.append(i+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):
H, W = map(int, input().split())
N = int(input())
from collections import deque

A = deque(list(map(int, input().split())))
nuriwake = 1
ans = []
for i in range(H):
    t = []
    for j in range(W):
        if A[0] == 0:
            A.popleft()
            nuriwake += 1
        A[0] -= 1
        t.append(nuriwake)
    if i % 2 != 0:
        t = reversed(t)
    ans.append(t)
for a in ans:
    print(' '.join(map(str, 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
k = 0
flg = True
while i < h:
    ans[i][j] = str(k+1)
    a[k] -= 1
    if a[k] == 0:
        k += 1
    if flg:
        j += 1
        if j == w:
            j -= 1
            i += 1
            flg = False
    else:
        j -= 1
        if j == -1:
            j += 1
            i += 1
            flg = True
for i in range(h):
    print(' '.join(ans[i]))

Solution (Python):
with open(0) as f:
    H, W, N, *a = map(int, f.read().split())

line = []
for i in range(N):
    line += [i+1] * a[i]
ans = [line[i*W:(i+1)*W] for i in range(H)]
for i, v in enumerate(ans):
    if i&1:
        v.reverse()
    print(*v)

Solution (Python):
import math
from math import gcd,pi,sqrt
INF = float("inf")

import sys
sys.setrecursionlimit(10**6)
import itertools
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]


def main():
    h,w = i_map()
    n = i_input()

    a = i_list()
    ans = []
    for m,i in enumerate(a):
        ans.extend([str(m+1)]*i)
    ans = list(ans)
    for i in range(1,h+1):
        if i%2 == 0:
            print(" ".join(ans[w*(i-1):w*(i-1)+w]))
        else:
            print(" ".join(ans[w*(i-1):w*(i-1)+w][::-1]))

if __name__=="__main__":
    main()

Solution (Python):
H, W = map(int,input().split())
N = int(input())
a = list(map(int,input().split()))
c = [[None]*W for _ in range(H)]
dirs = [(0,1),(1,0),(0,-1),(-1,0)]
i,j = 0,0
d = 0
color = 0
for _ in range(H*W):
    c[i][j] = color+1
    a[color] -= 1
    if a[color] == 0:
        color += 1
    di,dj = dirs[d]
    ni,nj = i+di,j+dj
    if 0 <= ni < H and 0 <= nj < W and c[ni][nj] == None:
        i,j = ni,nj
        continue
    d = (d+1) % 4
    di,dj = dirs[d]
    i,j = i+di,j+dj
for l in c:
    print(*l)

Solution (Python):
import numpy as np

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

colors = np.array([[0]*W]*H)

i = 0
j = 0
flag = True
count = 0
for k in range(N):
    for m in range(a[k]):
        colors[i, j] = str(k+1)
        count += 1
        if count % W == 0:
            i += 1
            if flag == True:
                flag = False
            else:
                flag = True
            continue
        if flag == True:
            j += 1
        else:
            j -= 1

for i in range(H):
    s = list(colors[i])
    for j in range(W):
        s[j] = str(s[j])
    p = ' '.join(s)
    print(p)

Solution (Python):
H,W=map(int,input().split())
N=int(input())
A=list(map(int,input().split()))
a=[0 for i in range(H*W)]
b=[0 for i in range(N)]
b[0]=A[0]
for i in range(1,N):
  b[i]=b[i-1]+A[i]

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

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

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

l = []
for i in range(n):
    for _ in range(a[i]):
        l.append(i+1)

ans = []
flag = True
for i in range(h):
    if flag:
        ans.append(l[w*i:w*(i+1)])
    else:
        ll = l[w*i:w*(i+1)]
        ll.reverse()
        ans.append(ll)
    flag = not flag

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

Solution (Python):
H,W,N,*A = map(int,open(0).read().split())
B = []

for n in range(N):
  B+=A[n]*[n+1]

for h in range(H):
  print(*B[h*W:h*W+W][::1-h%2*2])

Solution (Python):
from numpy import *
H,W = map(int,input().split())
N = int(input())
A = list(map(int,input().split()))
B = []
ans = zeros((H,W),int64)

for n in range(N):
  B.extend(A[n]*[n+1])

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

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

Solution (Python):
H, W = map(int, input().split())
N = int(input())
c = [[0]*W for _ in range(H)]
a = list(map(int, input().split()))
colors = []
for i in range(N-1, -1, -1):
    colors += [i+1]*a[i]

route = []
for h in range(H):
    tmp = []
    for w in range(W):
        tmp.append((h, w))
    if h % 2 == 0:
        route += tmp
    else:
        route += tmp[::-1]

for i in range(H*W):
    x, y = route[i]
    c[x][y] = colors.pop()

for row in c:
    print(*row)

Solution (Python):

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

bb=[[0]*w for i in range(h)]
idx=0
kk=0
for i in range(h):
    if i%2==0:
        wst=0
        wed=w
        wpt=1
    else:
        wst=w-1
        wed=-1
        wpt=-1
        
    for j in range(wst,wed,wpt):
        if kk>=a[idx]:
            idx+=1
            kk=0
        bb[i][j]=str(idx+1)
        kk+=1
        
for i in range(h):
    print(" ".join(bb[i]))

Solution (Python):
H, W = map(int, input().split())
N = int(input())
a = [int(x) for x in input().split()]
temp = []
for i in range(N):
    for _ in range(a[i]):
        temp.append(i+1)
ans = []
for i in range(H):
    ans = temp[i*W:(i+1)*W]
    if i&1:
        ans.reverse()
    print(*ans)

Solution (Python):
h,w=map(int,input().split());input();a,MAP,w1,h1=list(map(int,input().split())),[[0 for i in range(w)] for j in range(h)],0,0
for i,j in enumerate(a):
  for _ in range(j):
    if w1<w:
      MAP[h1][w1]= i+1
      w1+=1
      if w1==w:
        w1=0
        h1+=1
for i in range(h):
  if i%2!=0: MAP[i]=MAP[i][::-1]
[print(*i,sep=' ') for i in MAP]

Solution (Python):
h,w=map(int,input().split());input();a,MAP,w1,h1=list(map(int,input().split())),[[0 for i in range(w)] for j in range(h)],0,0
for i,j in enumerate(a):
  for _ in range(j):
    if w1<w:
      MAP[h1][w1]= i+1
      w1+=1
      if w1==w:
        w1=0
        h1+=1
for i in range(h):
  if i%2!=0: MAP[i] = MAP[i][::-1]
[print(*i,sep=' ') for i in MAP]

Solution (Python):
h,w=map(int,input().split());input();a,d,MAP,w1,h1=list(map(int,input().split())),[],[[0 for i in range(w)] for j in range(h)],0,0
for i,j in enumerate(a):
  for _ in range(j):
    if w1<w:
      MAP[h1][w1]= i+1
      w1+=1
      if w1==w:
        w1=0
        h1+=1
for i in range(h):
  if i%2!=0: MAP[i] = MAP[i][::-1]
[print(*i,sep=' ') for i in MAP]

Solution (Python):
h,w=map(int,input().split());input();a,d,MAP,w1,h1=list(map(int,input().split())),[],[[0 for i in range(w)] for j in range(h)],0,0
for i,j in enumerate(a):
  for _ in range(j):
    if w1<w:
      MAP[h1][w1]= i+1
      w1+=1
      if w1==w:
        w1=0
        h1+=1
if h>2 and w>2:
  for i in range(h):
    if i%2!=0:
      MAP[i] = MAP[i][::-1]
[print(*i,sep=' ') for i in MAP]

Solution (Python):
H,W = map(int,input().rstrip().split(" "))
N = int(input())
a = list(map(int,input().rstrip().split(" ")))
ans = [[0 for _ in range(W)]for _ in range(H)]
nowx = 0
nowy = 0
iki = True
for i in range(N):
    for j in range(a[i]):
        ans[nowy][nowx] = i+1
        if iki:
            nowx += 1
            if nowx >= W:
                nowx = W - 1
                nowy += 1
                iki = False
        else:
            nowx -= 1
            if nowx < 0:
                nowx = 0
                nowy += 1
                iki = True
for i in range(H):
    print(" ".join(map(str,ans[i])))

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

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

def nex(x,y):
    if y%2 == 0 and x == H-1:
        return x,y+1
    elif y%2 == 1 and x==0:
        return x,y+1
    elif y%2 ==0:
        return x+1,y
    else:
        return x-1,y

nowX,nowY = 0,0
for i in range(N):
    for j in range(a[i]):
        ans[nowX][nowY] = i+1
        nowX,nowY = nex(nowX,nowY)

for i in range(len(ans)):
    print(" ".join(list(map(str,ans[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)]

y, x = 0, 0
direction = 1
for i in range(N):
  for _ in range(a[i]):
    c[y][x] = (i+1)
    if direction == 1:
      if x+1 <= W-1:
        x += 1
      else:
        y += 1
        direction *= -1
    else:
      if x-1 >= 0:
        x -= 1
      else:
        y += 1
        direction *= -1
  
for cc in c:
  print(*cc)

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

ans = []
for i in range(h):ans.append([])

count = 0
for i in range(h):
    for j in range(w):
        ans[i].append(data[count])
        count+=1

count = 0

for i in ans:
  if(count%2==0):print(' '.join(i))
  else:print(' '.join(list(reversed(i))))
  count+=1

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

ans=[]
for i in range(N):
  ans.extend([i+1]*a[i])
  
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 collections
h, w= map(int, raw_input().split())
n,ais = int(raw_input()), map(int, raw_input().split())
mat = [[-1 for _ in range(w)] for ii in range(h)]
c = 0
for i in range(len(mat)):
	for j in (range(len(mat[0])) if i % 2 else range(len(mat[0])-1,-1,-1)) : 
		mat[i][j] = c +1
		ais[c] -= 1
		if ais[c] == 0: c +=1
for l in mat: print ' '.join(map(str,l))

Solution (Python):

import collections
h, w= map(int, raw_input().split())
n,ais = int(raw_input()), map(int, raw_input().split())
mat = [[-1 for _ in range(w)] for ii in range(h)]
c = 0
for i in range(len(mat)):
	for j in range(len(mat[0]))[::(-1 if i % 2 else 1)]:
		mat[i][j] = c +1
		ais[c] -= 1
		if ais[c] == 0: c +=1
for l in mat: print ' '.join(map(str,l))

Solution (Python):

h, w= map(int, raw_input().split())
n = int(raw_input())
ais = map(int, raw_input().split())
mat = [[-1 for _ in range(w)] for ii in range(h)]
import collections
color = 0
vis = set([])
def bfs(start, color, count,mat):
	q = collections.deque([start])
	vis = set([start])
	while(q and count):
		count -=1
		cur = q.popleft()
		mat[cur[0]][cur[1]] = color + 1
		i,j = cur
		for ni,nj in [(i-1,j),(i+1,j),(i,j-1),(i,j+1)]:
			if 0 <= ni < len(mat) and 0<= nj < len(mat[0]):
				if (ni,nj) not in vis:
					vis.add((ni,nj))
					q.append((ni,nj))

for i in range(len(mat)):
	for j in range(len(mat[0]))[::(-1 if i % 2 else 1)]:
		mat[i][j] = color +1
		ais[color] -= 1
		if ais[color] == 0: color +=1
for l in mat:
	print ' '.join(map(str,l))

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

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

x = 0
y = 0
f = True
for i in range(n):
    for j in range(a[i]):
        ans[y][x] = str(i+1)
        if f:
            x += 1
        else:
            x -= 1
        if x == w:
            x = w-1
            y += 1
            f = False
        if x == -1:
            x = 0
            y += 1
            f = True

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

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

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

l = [[] for _ in range(h)]
x = 0
y = 0
for i in range(n):
  for _ in range(a[i]):
    l[y].append(i+1)
    x += 1
    if x == w:
      x = 0
      y += 1
for i in range(h):
  if i%2 == 0: print(*l[i])
  else: print(*reversed(l[i]))

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

ans = []
for i in range(n):
  for _ in range(a[i]):
    ans.append(i+1)
  
for i in range(h):
  if i%2 == 0: print(*ans[i*w:(i+1)*w])
  else: print(*reversed(ans[i*w:(i+1)*w]))

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

b = []

ans = [[0]*w]*h

for i in range(n):
	b += [i+1]*a[i]

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

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()))
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)
    else:
        print(*c[::-1])

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

for i in range(N):
  for _ in range(A[i]):
    ans.append(i+1)
  
for i in range(H):
  if i%2 == 0:
    print(*ans[i*W:(i+1)*W])
  else:
    print(*reversed(ans[i*W:(i+1)*W]))

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

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

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

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

Solution (Python):
h,w=map(int,input().split())
n=int(input())
a=[0]+list(map(int,input().split()))
c=[[0]*w for _ in range(h)]
g=[0,w-1][h%2]
x,y=0,0
i=1
while True:
  c[x][y]=i
  a[i]-=1
  if x==h-1 and y==g:break
  if a[i]==0:i+=1
  if x%2:
    if y:y-=1
    else:x+=1
  else:
    if y==w-1:x+=1
    else:y+=1
for i in c:
  print(*i)

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()

def main():
    H, W = map(int, input().split())
    N = int(input())
    A = list(map(int, input().split()))
    ans = [0] * (H * W)
    idx = 0
    for i in range(N):
        cnt = 0
        while cnt < A[i]:
            ans[idx] = i + 1
            cnt += 1
            idx += 1
    for i in range(H):
        if i % 2 == 0:
            print(" ".join(map(str, ans[W * i: W * (i + 1)])))
        else:
            a = ans[W * i: W * (i + 1)]
            a.reverse()
            print(" ".join(map(str, a)))


if __name__ == "__main__":
    main()

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

flag=0  

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


dammy=0
k=0
l=0
for i in range(N):
    
    for j in range(a[i]):
        out[k][l] = i+1
        if l==W-1:
            l=0
            k+=1
        else:
            l+=1
    



for i in range(H):
    if i%2==1:
        
        out[i].reverse()
        print(*out[i])
    else:
        print(*out[i])

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

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

i = 0
j = 0
num = 1
while True:
    mat[j][i] = num
    a[num-1] -= 1
    if a[num-1] == 0:
        num += 1
    if i <= w-2:
        i += 1
    elif i == w-1:
        if j == h-1:
            break
        i = 0
        j += 1

for i in range(h):
    if i % 2 == 1:
        tmp = mat[i]
        tmp.reverse()
        mat[i] = tmp

for i in range(h):
    for j in range(w-1):
        print(mat[i][j], end = ' ')
    print(mat[i][-1])

Solution (Python):
H,W = list(map(int,input().split()))
N = int(input())
A = list(map(int,input().split()))
cnt = 0
L = []
i = 1
while cnt<H*W:
    if A[i-1]>0:
        A[i-1]+=-1
        L.append(i)
        cnt+=1
    else:
        i+=1
for i in range(H):
    out = [0]*W
    if i%2==0:
        for j in range(W):
            out[j]=L[W*i+j]
    else:
        for j in range(W):
            out[W-1-j]=L[W*i+j]
    print(*out)

Solution (Python):
from collections import deque
h,w=map(int,input().split())
n=int(input())
a=list(map(int,input().split()))
ae=[]
ans=[]
rl=0
for i in range(n):
  ae.extend([i+1]*a[i])
for x in range(h):
  if x%2==0:
    ans.append(ae[w*x:w*(x+1)])
  else:
    ans.append(reversed(ae[w*x:w*(x+1)]))

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

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    H, W, N, *A = map(int, read().split())

    C = [[0] * W for _ in range(H)]
    vec = [i for i, a in enumerate(A, 1) for _ in range(a)]

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

    for row in C:
        print(*row)

    return


if __name__ == '__main__':
    main()

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

ans=[]
for j in range(h):
    if j%2==0:
        ans.append(list(temp[j*w:(j+1)*w]))
    else:
        ans.append(list(temp[(j+1)*(w)-1:j*w-1:-1]))
   
        

for k in ans:
    ans2=[str(v) for v in k]
    print(" ".join(ans2))

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

ans = [[0 for _ in range(W)] for _ in range(H)]
c = 1  
n = 0  
for i in range(H):
    if i % 2 == 0:
        start = 0
        end = W
        by = 1
    else:
        start = W - 1
        end = -1
        by = -1
    for j in range(start, end, by):
        ans[i][j] = c
        n += 1
        if n >= a[c]:
            c += 1
            n = 0

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

color = 0
cnt = 0
res = [[0] * w for _ in range(h)]
for i in range(h):
    if i % 2 == 0:
        for j in range(w):
            res[i][j] = color + 1
            cnt += 1
            if cnt == a[color]:
                color += 1
                cnt = 0
    else:
        for j in range(w-1, -1, -1):
            res[i][j] = color + 1
            cnt += 1
            if cnt == a[color]:
                color += 1
                cnt = 0

for i in range(h):
    for j in range(w):
        print(res[i][j], end=' ')
    print()

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

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

for y in range(h):
  print(" ".join(list(map(str, mat[y]))))

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

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()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

H, W = MAP()
N = INT()
a = LIST()[::-1]

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

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

Solution (Python):
h,w = map(int,input().split())
n = int(input())
A = [int(i) for i in input().split()]
dic = {}
for i in range(n):
    dic[i+1] = A[i]

Map = [0 for i in range(h*w)]
height = 0
width = 0
now = 0
color = list(dic.keys())

color_num = 0
while now < h*w:
    if height%2==0:
        Map[now] = color[color_num]
        dic[color[color_num]] -= 1
        if dic[color[color_num]]==0:
            color_num += 1
        width += 1
        if width==w:
            height += 1
            width = w-1
    else:

        Map[now] = color[color_num]
        dic[color[color_num]] -=1
        if dic[color[color_num]]==0:
            color_num += 1
        width -= 1
        if width==-1:
            height += 1
            width = 0
    now = height*w + width
st = 0

for i in range(h):
    print(*Map[st:st+w])
    st = st+w

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

Solution (Python):
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  

H,W = LI()
N = I()
a = LI()

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

for i in range(H):
    if i % 2 == 0:
        print(*ANS[W*i:W*(i+1)])
    else:
        print(*reversed(ANS[W*i:W*(i+1)]))

Solution (Python):
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  
def LS2(): return list(sys.stdin.readline().rstrip())  

H,W = LI()
N = I()
a = LI()

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

for i in range(H):
    if i % 2 == 0:
        print(*ANS[W*i:W*(i+1)])
    else:
        print(*reversed(ANS[W*i:W*(i+1)]))

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

ans = []
for i, ai in enumerate(a):
  ans.extend([str(i+1)] * ai)
  
for i in range(h):
  l = ans[i * w : (i + 1) * w ]
  if i % 2:
    l.reverse()
  print(' '.join(l))

Solution (Python):
from collections import deque

H, W = 2, 2
N = 3
ARR = [2, 1, 1]

H, W = 3, 5
N = 5
ARR = [1, 2, 3, 4, 5]

H, W = 1, 1
N = 1
ARR = [1]

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

def calculate(h, w, n, arr):
    resources = deque()

    for index, ar in enumerate(arr):
        for m in range(ar):
            resources.append((index + 1))

    nodeStatus = [[False for i in range(w)] for j in range(h)]
    for j in range(h):
        if j % 2 == 0:
            startIndex = 0
            for i in range(startIndex, w, 1):
                nodeStatus[j][i] = str(resources.popleft())
        else:
            startIndex = w - 1
            for i in range(startIndex, -1, -1):
                nodeStatus[j][i] = str(resources.popleft())

    for j in range(h):
        print(" ".join(nodeStatus[j]))


calculate(H, W, N, ARR)

Solution (Python):
def next_pos(x, y, W):
    if x % 2 == 0 and y == W - 1:
        return (x + 1, y)
    elif x % 2 == 0:
        return (x, y + 1)
    elif y == 0:
        return (x + 1, y)
    else:
        return (x, y - 1)


def solve(a, H, W):
    x, y = 0, 0
    c = [[""] * W for _ in range(H)]
    for i, v in enumerate(a):
        for _ in range(v):
            c[x][y] = str(i + 1)
            x, y = next_pos(x, y, W)
    return c


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

for row in solve(a, H, W):
    print(" ".join(row))