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 copy
import random
import bisect 
import fractions 
import math


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

color_count = {}
for i in range(N):
    color_count[i+1] = a[i]

color_count = dict(sorted(color_count.items(),key=lambda x:-x[1]))


mas = [[0 for i in range(W)] for j in range(H)]
colors = list(color_count.keys())

color = colors[0]
color_i = 0
color_cnt = color_count[color]
cnt = 0

for h_i in range(H):
    if h_i%2==0:
        for w_i in range(W):
            mas[h_i][w_i] = color
            cnt += 1
            if h_i == H - 1 and w_i == W-1:
                break
            if cnt == color_cnt:
                color_i += 1
                color = colors[color_i]
                cnt = 0
                color_cnt = color_count[color]
    else:
        for w_i in reversed(range(W)):
            mas[h_i][w_i] = color
            cnt += 1
            if h_i == H - 1 and w_i == 0:
                break
            if cnt == color_cnt:
                color_i += 1
                color = colors[color_i]
                cnt = 0
                color_cnt = color_count[color]


for h_i in range(H):
    for w_i in range(W):
        print(mas[h_i][w_i],end=" ")
    print()

Solution (Python):
from functools import reduce
import math

def main():
    
    
    
    

    
    
    
    
    
    

    
    

    
    
    
    
    
    
    
    
    
    INF = float("inf")

    
    MOD = 10**9+7
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    
    
    
    
    H, W = (int(_) for _ in input().split())
    N = int(input().rstrip())
    a = list(int(_) for _ in input().split())
    a_dict = dict()
    for i in range(N):
        a_dict[i+1] = a[i]

    a_dict_sorted = dict()
    for k, v in sorted(a_dict.items(), key=lambda x: -x[1]):
        a_dict_sorted[k] = v

    Grid = []
    for i in range(H):
        Grid.append(list ( -1 for j in range(W)))
        
    a_index = 0
    a_cnt = a[a_index]
    for h in range(H):
        for w in range(W):
            Grid[h][w] = a_index+1
            if a_cnt - 1 <=0:
                a_index +=1
                if(a_index == N):
                    break
                a_cnt = a[a_index]
            else:
                a_cnt -=1
                
    for h in range(H):
        if h % 2 ==1:
            Grid[h] = Grid[h][::-1]
    for h in range(H):
        print(' '.join(str(_) for _ in Grid[h]))
    
if __name__ == '__main__':
    main()

Solution (Python):
h,w = map(int,input().split())
n = int(input())
a = list(map(int,input().split()))
color = 1
c = [[''for _ in range(w)]for _ in range(h)]
for i in range(h):
  for j in range(w):
    if a[color-1] == 0:
      color += 1
    c[i][j] = str(color)
    a[color-1] -= 1
for i in range(0,h,2):
  c[i] = c[i][::-1]
for i in range(h):
  print(' '.join(c[i]))

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

def print_ans(ans):
  for i in ans:
    for j in i:
      print(str(j) + ' ',end='')
    print()
  
ans = [[0 for i in range(W)] for j in range(H)]
ans[0][0] = 1
a[0] -= 1
color = 1
if a[0] == 0:
  color += 1
r,c = 0,0
direction = 0
for i in range(H*W-1):
  if direction ==0:
    if r >= H-1 or ans[r+1][c] != 0:
      direction = 1
      c += 1
    else:
      r+= 1
  elif direction == 1:
    if c >= W-1 or ans[r][c+1] != 0:
      direction = 2
      r -= 1
    else:
      c += 1
  elif direction == 2:
    if r == 0 or ans[r-1][c] != 0:
      direction = 3
      c -= 1
    else:
      r -= 1
  elif direction == 3:
    if c == 0 or ans[r][c-1] != 0:
      direction = 0
      r += 1
    else:
      c -= 1
  ans[r][c] = color
  a[color-1] -= 1
  if a[color-1] == 0:
    color += 1
print_ans(ans)

Solution (Python):

import sys
from collections import deque

H,W=map(int, sys.stdin.readline().split())
N=input()
A=map(int, sys.stdin.readline().split())


L=[]

cnt=0
flag="r"
q=deque([]) 
for i,a in enumerate(A):
	for u in range(a):
		cnt+=1
		
		if flag=="r":
			q.append(i+1)
		elif flag=="l":
			q.appendleft(i+1)

		if cnt==W:
			cnt=0
			L.append(q)
			q=deque([]) 
			if flag=="r": flag="l"
			elif flag=="l": flag="r"

for u in L:
	for x in u:
		print x,
	print

Solution (Python):

import sys
from collections import deque

H,W=map(int, sys.stdin.readline().split())
N=input()
A=map(int, sys.stdin.readline().split())


L=[]

cnt=0
flag="r"
q=deque([]) 
for i,a in enumerate(A):
	for u in range(a):
		cnt+=1
		
		if flag=="r":
			q.append(i+1)
		elif flag=="l":
			q.appendleft(i+1)

		if cnt==W:
			cnt=0
			L.append(q)
			q=deque([]) 
			if flag=="r": flag="l"
			elif flag=="l": flag="r"

for u in L:
	for x in u:
		print x,
	print

Solution (Python):
import numpy as np
h,w = map(int,input().split())
n = int(input())
a = list(map(int,input().split()))
mat = np.zeros((h,w),dtype=int)
st = 0
for i in range(len(a)):
    for idx in range(st,st+a[i]):
        mat[idx//w][idx%w]=i+1
    st+=a[i]
for i in range(1,h,2):
    mat[i]=mat[i][::-1]
for i in range(h):
    for j in range(w):
        print(mat[i][j],end=" ")
    print("")

Solution (Python):
H,W = map(int,input().split())
N = int(input())
a = list(map(int,input().split()))
l = []
for color,m in enumerate(a,1):
    l += [color]*m
for i in range(H):
    k = l[:W]
    l = l[W:]
    if i%2 != 0:
        k.reverse()
    print(*k)

Solution (Python):
H, W = map(int, input().split())
N = int(input())
a = list(map(int, input().split()))
ans = []
c = 1
for i in a:
    for j in range(i):
        ans.append(c)
    c += 1
for i in range(H):
    for j in range(W):
        if i % 2 == 0:
            print(ans[i * W + j], end = "")
        else:
            print(ans[i * W + W - 1 - j], end = "")
        if j < W - 1:
            print(" ", end = "")
        else:
            print()

Solution (Python):
H, W = map(int, input().split())
N = int(input())
a = list(map(int, input().split()))
ans = []
c = 1
for i in a:
    for j in range(i):
        ans.append(c)
    c += 1
for i in range(H):
    for j in range(W):
        if i % 2 == 0:
            print(ans[i * W + j], end = "")
        else:
            print(ans[i * W + W - 1 - j], end = "")
        if j < W - 1:
            print(" ", end = "")
        else:
            print()

Solution (Python):
height, wide = map(int, input().split())
n = int(input())
anslist = []

a = list(map(int, input().split()))
for i, j in enumerate(a):
    for l in range(j):
        anslist.append(i+1)
for k in range(height):
    t = anslist[k*wide:(k+1)*wide]
    if k % 2 == 1:
        t.reverse()
    print(' '.join(map(str, t)))

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

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

Solution (Python):
h,w = map(int,input().split())
lis = []
ans = []
nu = 0
nn = int(input())
li = list(map(int,input().split()))
for i in range(nn):
    for j in range(li[i]):
        if nu % 2 == 0:
            lis.append(str(i+1))
        else:
            lis.insert(0,str(i+1))
        if len(lis) == w:
            ans.append(str(" ".join(lis)))
            lis = []
            nu += 1
for i in range(h):print(ans[i])

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

L = [[0 for i in range(W)] for j in range(H)]
for i in range(H * W):
    if int((i // W)) % 2 == 0:
        L[int((i // W))][i % W] = A[i]
    else:
        L[int((i // W))][-1 - i % W] = A[i]

for i in range(H):
    L[i] = list(map(str, L[i]))


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

Solution (Python):
h, w = map(int, input().split())
n = int(input())
alist = list(map(int, input().split()))
cmap = [[-1 for _ in range(w)] for _ in range(h)]
i = 0
for ai, a in enumerate(alist):
  ai += 1
  for _a in range(a):
    if ((i + _a) // w) % 2 == 0:
      cmap[(i + _a) // w][(i + _a) % w] = ai
    else:
      cmap[(i + _a) // w][-((i + _a) % w + 1)] = ai
  i += a

for cm in cmap:
  print(*cm, sep=' ')

Solution (Python):

def start_process(high, width, number, color_data):
    ans_data = []
    for i in range(number):
        ans_data += [str(i + 1) for j in range(color_data[i])]
    for i in range(high):
        ans_kari = ans_data[i * width : (i + 1) * width]
        if i % 2 == 1:
            ans_kari = ans_kari[::-1]
        print(' '.join(ans_kari))

def main():
    high, width = map(int, input().split())
    number = int(input())
    color_data = list(map(int, input().split()))
    start_process(high, width, number, color_data)

if __name__ == '__main__':
    main()

Solution (Python):
from statistics import mean, median,variance,stdev
import numpy as np
import sys
import math
import fractions
import itertools
import copy
from operator import itemgetter

def j(q):
    if q==1: print("YES")
    else:print("NO")
    exit(0)


def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")

def ip():
    return int(input())

h,w= (int(i) for i in input().split())       









n = ip()
a = [int(i) for i in input().split()]
horz = 1
vert = 1
x = 0
y = 0
ans  = [[0 for i in range(w)] for i in range(h)]
pos = 0
for i in range(h*w):
    ans[y][x] = pos+1
    a[pos]-=1
    if a[pos] ==0:
        pos+=1
    if y == h-1 and vert == 1:
        x+=1
        vert = -1
    elif y == 0 and vert == -1:
        x+=1
        vert = 1
    else: y+=vert
for i in range(h):
    for k in range(w):
        if k+1 != w: print(ans[i][k],"",end="")
        else : print(ans[i][k],end="")
    print()

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

l=[]
for i in range(n):
    for j in range(a[i]):
      l.append(i+1)
ans=[]
for i in range(h):
    if i%2==1:
        x=l[i*w:(i+1)*w]
        ans.append(x[::-1])
    else:
        ans.append(l[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()))
res = [[-1 for _ in range(W)] for _ in range(H)]
tmp = []
for i in range(N):
    for j in range(A[i]):
        tmp.append(i + 1)
        
for i in range(H):
    result = ''
    for j in range(W):
        if i%2 == 0:
            result = result + str(tmp[W * i + j])
            if j != W - 1:
                result = result + ' '
        else:
            result =  str(tmp[W * i + j]) + result
            if j != W - 1:
                result = ' ' + result 
            
    print(result)

Solution (Python):
H,W=map(int,input().split())
N=int(input())
A=list(map(int,input().split()))
ANS = [ [0]*W for _ in range(H)]
nc=1
nct=A[0]
ans=[]
for y in range(H):
  if y%2==0:
    for x in range(W):
      nct-=1
      ANS[y][x] = str(nc)
      if nct==0 and nc<N:
        nct=A[nc]
        nc+=1
  else:
    for x in range(W-1,-1,-1):
      nct-=1
      ANS[y][x] = str(nc) 
      if nct==0 and nc<N:
        nct=A[nc]
        nc+=1
        
      
for y in range(H):
    print(" ".join(ANS[y]) )

Solution (Python):
H,W = map(int,input().split())
N = int(input())
L = list(map(int,input().split()))
M = [[] for i in range(H)]
c = 0
x = 0
y = 0
for i in range(N):
  while c < L[i]:
    if x != W:
      M[y].append(i+1)
      c += 1
      x += 1
    else:
      y += 1
      x = 1
      M[y].append(i+1)
      c += 1
  c = 0
for i in range(H):
  if i%2 != 0:
    M[i].reverse()
for j in range(H):
  print(' '.join(map(str,M[j])))

Solution (Python):
H, W = (int(i) for i in input().split())
N = int(input())
A = [int(i) for i in input().split()]
C = []
for i in range(N):
  for j in range(A[i]):
    C.append(i+1)
ans = [[0]*W for i in range(H)]
for i in range(H*W):
  y = i // W
  if y%2==0:
    x = i%W
  else:
    x = W-1-i%W
  ans[y][x] = C[i]
for i in range(H):
  ans[i] = [str(a) for a in ans[i]]
  print(" ".join(ans[i]))

Solution (Python):

import sys
input = sys.stdin.readline

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

l = [0]*(H*W)
prev = 0
for i, a in enumerate(A):
    color = i+1
    cnt = 0
    while cnt < a:
        l[cnt+prev] = color
        cnt += 1
    prev = prev+a

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

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


q = 1
h = 0
w = 0
cnt = 0
g = 0
count = 0

def judge(tmph,tmpw):
    ok = False
    if tmph >= 0 and tmph < H and tmpw >= 0 and tmpw < W:
        if c[tmph][tmpw] < 0:
            ok = True
    return ok
for _ in range(H*W):
    c[h][w] = q
    cnt += 1
    if cnt == arr[q]:
        q += 1
        cnt = 0
    tmph = h + goto[g][0]
    tmpw = w + goto[g][1]
    for i in range(4):
        if judge(tmph,tmpw):
            break
        g += 1
        g %= 4
        tmph = h + goto[g][0]
        tmpw = w + goto[g][1]
    h = tmph
    w = tmpw

for i in c:
    print(*i)

Solution (Python):
H, W = [int(_) for _ in input().split()]
N = int(input())
A = [int(_) for _ in input().split()]
B = []
for i in range(N):
    B += [i + 1] * A[i]
for j in range(H):
    print(' '.join(map(str, B[j * W:(j + 1) * W][::(-1)**(1&j)])))

Solution (Python):
a,b= map(int,input().split(" "))
c = int(input())
ar = list(map(int,input().split(" ")))
br = [[0 for i in range(b)] for j in range(a)]
go = "M"
ap = 1
for i in range(a):
    if go == "M":
        for j in range(b):
            if ar[0] == 0:
                del ar[0]
                ap += 1
            br[i][j] = ap
            ar[0] -= 1
        go = "U"
    else:
        for j in range(b-1,-1,-1):
            if ar[0] == 0:
                del ar[0]
                ap += 1
            br[i][j] = ap
            ar[0] -= 1
        go = "M"
for r in br:
    for i in range(b-1):
        print(r[i],end=" ")
    print(r[b-1])

Solution (Python):

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

ans = []

current_a=a[0]
current_id = 1
for i in range(H):
    w_ans = []
    for j in range(W):
        if current_a == 0:
            current_a = a[current_id]
            current_id += 1
        
        current_a -= 1
        w_ans.append(str(current_id))
    if i%2==1:
        w_ans = w_ans[::-1]
    ans.append(w_ans)

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

Solution (Python):
h,w=map(int,input().split())
n=int(input())
a=list(map(int,input().split()))
ret=[["0" for i in range(w)]for j in range(h)]
x=0
y=0
for i in range(n):
    for j in range(a[i]):
        ret[y][x]=str(i+1)
        if y&1:
            if x==0:
                y+=1
            else:
                x-=1
        else:
            if x==w-1:
                y+=1
            else:
                x+=1
for i in range(h):
    print(" ".join(ret[i]))

Solution (Python):
import numpy as np
h,w=map(int, input().split())
n=int(input())
a=list(map(int, input().split()))
m=np.arange(w*h)
c=0
for i,j in enumerate(a):
    m[c:c+j]=i+1
    c+=j
m=m.reshape(h,w)
m[::2,:]=m[::2,::-1]
for i in m:print(*i)

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

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

Solution (Python):

def inpl(): return [int(i) for i in input().split()]
def nextin(nwin,prin):
    if prin[1] != nwin[1]+1 and nwin[1] != W-1:
        return [nwin[0],nwin[1]+1]
    if prin[1] != nwin[1]-1 and nwin[1] != 0:
        return[nwin[0],nwin[1]-1]
    else:
        return[nwin[0]+1,nwin[1]]

H, W = inpl()
HW = [[0 for _ in range(W)] for _ in range(H)]
N = int(input())
a = inpl()
previn = [0,-1]
nowin = [0,0]
for i in range(1,N+1):
    for _ in range(a[i-1]):
        nh,nw = nowin
        HW[nh][nw] = i
        nowin = nextin(nowin,previn)
        previn = [nh,nw]
for i in HW:
    print(*i)

Solution (Python):
import itertools
h, w=map(int, raw_input().split())
n=input()
a=list(itertools.chain(*[[c+1]*a for c, a in enumerate(map(int, raw_input().split()))]))
for i in xrange(h):
	for j in xrange(w):
		print a[i*w+j if i%2==0 else (i+1)*w-j-1],
	print

Solution (Python):
import itertools
h, w=map(int, raw_input().split())
n=input()
a=list(itertools.chain(*[[c+1]*a for c, a in enumerate(map(int, raw_input().split()))]))
for i in xrange(h):
	for j in xrange(w):
		print a[i*w+j if i%2==0 else (i+1)*w-j-1],
	print

Solution (Python):

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

l = []
a = [int(x) for x in input().split()]
for i in range(n):
    l.extend([i+1 for _ in range(a[i])])

for hi in range(h):
    line = []
    for wi in range(w):
        line.append(l[w*hi+wi])
    if hi%2==1:
        line = line[::-1]

    print(*line)

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

Solution (Python):
def getN():
    return int(input())


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


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

w,h = getNM()
n = getN()
nums = getList()

seq = []
for i in range(n):
    seq = seq + [(i+1) for kazu in range(nums[i])]

for dan in range(w):
    subset = seq[h*dan:h*(dan+1)]
    if dan % 2 == 0:
        print(" ".join(map(str,subset)))
    else:
        print(" ".join(map(str, subset[::-1])))

Solution (Python):
h,w = map(int, input().split())
n = int(input())
list_a = list(map(int, input().split()))
list_a_post = []
for i in range(n):
    for j in range(list_a[i]):
        list_a_post.append(i+1)
list_ans = []
p = 0
for i in range(h):
    memo_list = []
    for j in range(w):
        memo_list.append(list_a_post[p])
        p += 1
    if i % 2 == 1:
        memo_list.reverse()
    list_ans.append(memo_list)
for i in range(h):
    for j in range(w):
        list_ans[i][j] = str(list_ans[i][j])
for row in list_ans:
    print(" ".join(row))

Solution (Python):


from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(1000000)

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

pos = 0
ans = [[0] * W for i in range(H)]
for i in range(H):
    R = range(W) if i % 2 else range(W - 1, -1, -1)
    ls = []
    for j in R:
        if a[pos] == 0:
            pos += 1
        a[pos] -= 1
        ans[i][j] = pos + 1

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

Solution (Python):
h,w = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))
l = []
for i,m in enumerate(a,1):
    l += [i]*m
for i in range(h):
    k = l[:w]
    l = l[w:]
    if i&1:
        k.reverse()
    print(*k)

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

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

row_is_even = True

def next_grid(h, w):
	if h % 2 == 0:
		if w == W - 1:
			return h + 1, w
		else:
			return h, w + 1
	else:
		if w == 0:
			return h + 1, w
		else:
			return h, w - 1

def color(h, w, num, cnt):
	for _ in range(cnt):
		ans[h][w] = num
		h, w = next_grid(h, w)
	return h, w

h, w = color(0, 0, 1, a[0])
for i in range(1, n):
	h, w = color(h, w, i + 1, a[i])

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

Solution (Python):
from sys import exit, setrecursionlimit, stderr
from functools import reduce
from itertools import *
from collections import *
from bisect import *

def read():
  return int(input())
 
def reads():
  return [int(x) for x in input().split()]

H, W = reads()
N = read()
A = reads()

field = [[0] * W for _ in range(H)]
ls = sorted(product(range(H), range(W)), key=lambda p: (p[0], (-1)**p[0] * p[1]))
for i, j in ls:
  field[i][j] = len(A)
  A[-1] -= 1
  if A[-1] == 0:
    A.pop()

for i in range(H):
  print(" ".join(str(x) for x in field[i]))

Solution (Python):
s=input().split()
h,w=int(s[0]),int(s[1])
n=int(input())
s=input().split()
l=[int(s[i]) for i in range(n)]
res=[]
for i in range(1,n+1):
    res+=[i]*l[i-1]
for i in range(h):
    ll=res[i*w:i*w+w]
    if i%2==1:
        ll=ll[-1::-1]
    for j in ll[:-1]:
        print(j,end=" ")
    print(ll[-1])

Solution (Python):

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

for i in range(n):
    for j in range(a[i]):
        ass.append(i+1)



k = 1
l = 1
ans = ''

for i in range(h):
    ans = ''
    for j in range(w):
        if l == 1:
            if i == h-1 and j == w-1:
                ans += str(ass[i*w+j])
            else:
                ans += str(ass[i*w+j]) + ' '
        else:
            if i == h-1 and j == w-1:
                ans += str(ass[(i+1)*w-j-1])
            else:
                
                ans += str(ass[(i+1)*w-j-1]) + ' '
    l = l * (-1)
    print(ans)

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

    grid = [[0 for j in range(w)] for i in range(h)]
    i, j, idx = 0, 0, 0
    while idx < n:
        while 0 < a[idx]:
            grid[i][j] = idx + 1
            a[idx] -= 1

            if i % 2 == 0:
                j += 1
                if j == w:
                    j = w - 1
                    i += 1
            else:
                j -= 1
                if j == -1:
                    j = 0
                    i += 1
        idx += 1

    for i in range(h):
        print(" ".join([str(j) for j in grid[i]]))


if __name__ == '__main__':
    main()

Solution (Python):
h,w = map(int,raw_input().split())

n = int(input())

arr = map(int,raw_input().split())

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


x = 0
y = 0
d = 1
p = 0

for i in range(h*w):

	if arr[p] == 0:
		p += 1

	field[y][x] = str(p + 1)
	arr[p] -= 1

	x = x + d
	if x == -1 or x == w:
		x -= d
		y += 1
		d *= -1

for i in range(h):
	print ' '.join(field[i])

Solution (Python):
'''input
2 2
3
2 1 1
'''
h,w = map(int,raw_input().split())
n  =input()
arr = map(int,raw_input().split())
ans = [[-1]*w for i in range(h)]
ind = 0
for i in xrange(h):
	if i&1:
		for j in xrange(w-1,-1,-1):
			if arr[ind] == 0:
				ind += 1
				ans[i][j] = ind+1
				arr[ind] -= 1
			else:
				ans[i][j] = ind+1
				arr[ind] -= 1
	else:
		for j in xrange(w):
			if arr[ind] == 0:
				ind += 1
				ans[i][j] = ind+1
				arr[ind] -= 1
			else:
				ans[i][j] = ind+1
				arr[ind] -= 1
for i in xrange(h):
	for j in xrange(w):
		print ans[i][j],
	print

Solution (Python):
H,W=map(int,input().split())
N=int(input())
A=[int(i) for i in input().split()]
table=[[-1]*W for i in range(H) ]
u=0
v=0
t=0
for i in range(N):
    s=A[i]
    while s>0:
        if t==0 and (u<W-1):
            table[v][u]=i+1
            u+=1
            s-=1
        elif t==0 and u==W-1:
            table[v][u]=i+1
            
            v+=1
            t=(t+1)%2
            s-=1
        elif t==1 and u>0:
            table[v][u]=i+1
            u-=1
            s-=1
        elif t==1 and u==0:
            table[v][u]=i+1
            
            v+=1
            t=(t+1)%2
            s-=1 
for i in range(H):
    print(' '.join(map(str,table[i])))

Solution (Python):
def read():
    return int(input())


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


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


H, W = readmap()
N = read()
A = readlist()

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

i = 0
j = 0
for n in range(N):
    a = A[n]
    while a:
        C[i][j] = n + 1
        if (j == 0 and i % 2 == 1) or (j == W - 1 and i % 2 == 0):
            i += 1
        elif i % 2 == 0:
            j += 1
        else:
            j -= 1
        a -= 1

for i in range(H):
    print(" ".join(list(map(str, C[i]))))

Solution (Python):
h,w=map(int,input().split())
n=int(input())
a=list(map(int,input().split()))
ret=[["0" for i in range(w)]for j in range(h)]
x=0
y=0
for i in range(n):
    for j in range(a[i]):
        ret[y][x]=str(i+1)
        if y&1:
            if x==0:
                y+=1
            else:
                x-=1
        else:
            if x==w-1:
                y+=1
            else:
                x+=1
for i in range(h):
    print(" ".join(ret[i]))

Solution (Python):
grid_condition = input().split()
height = int(grid_condition[0])
width = int(grid_condition[1])
color_variety = int(input())
input_condition = input().split()

list_in_list = list()

for i in range(height):
    list_in_list.append(list())

pointer = 0
number = 1

for i in range(1, color_variety + 1):
    times = int(input_condition[i-1])
    for j in range(times):
        list_in_list[pointer].append(i)
        if len(list_in_list[pointer]) == width:
            pointer += 1


for i in range(len(list_in_list)):
    t = ''
    if i%2 == 1:
        list_in_list[i].sort(reverse=True)
    for j in list_in_list[i]:
        t = t + str(j) + " "
    print(t)

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

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 j in range(h)]
fromL = True
x = 0
y = 0

for i in range(1, n+1) :
    for j in range(a[i-1]) :
        c[y][x] = i
        if w == 1 :
            y += 1
        elif fromL :
            if x == w - 1 :
                y += 1
                fromL = False
            else :
                x += 1
        else :
            if x == 0 :
                y += 1
                fromL = True
            else :
                x -= 1

for ci in c :
    print(*ci)

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

cells = [0] * (h * w)
p = 0
for i in range(n):
    for j in range(p, p + a[i]):
        cells[j] = i + 1
    p += a[i]

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

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 h in range(H)]
flg = True
h = 0
w = 0
for i, a in enumerate(A):
    for _ in range(a):
        ans[h][w] = i + 1
        if flg:
            if w < W - 1:
                w += 1
            else:
                h += 1
                flg = False
        else:
            if w > 0:
                w -= 1
            else:
                h += 1
                flg = True
for l in ans:
    print(" ".join(map(str, l)))

Solution (Python):


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

status = 0 
x = 0
y = 0
switch = 0

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

for i in range(N):
    tmp = As[i]
    while tmp > 0:
        if status == 0:
            ans[x][y] = i+1
            if y == H-1-switch:
                x += 1
                status = 1
            else:
                y += 1
        elif status == 1:
            
            ans[x][y] = i+1
            if x == W-1-switch:
                y -= 1
                status = 2
            else:
                x += 1
        elif status == 2:
            ans[x][y] = i+1
            if y == switch:
                x -= 1
                status = 3
                switch += 1
            else:
                y -= 1
        else:
            ans[x][y] = i+1
            if x == switch:
                y += 1
                status = 0
            else:
                x -= 1
        tmp -= 1

for h in range(H):
    for w in range(W):
        print(ans[w][h],end=' ')
        
        
    print()

Solution (Python):
H,W=[int(i) for i in input().split()]
N=int(input())
a=[int(i)for i in input().split()]
tile=[[0 for i in range(W)]for j in range(H)]

x=0
y=0
for idA in range(1,len(a)+1):
        num=a[idA-1]
        while num>0:
                tile[y][x]=idA
                if y%2==0:
                        x+=1
                else:
                        x-=1

                if x==-1:
                        y+=1
                        x=0
                elif x==W:
                        y+=1
                        x=W-1
                num-=1
for y in range(H):
        for x in range(W):
                if x!=0:
                        print(" ",end="")
                print(tile[y][x],end="")
        print()

Solution (Python):

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

from itertools import chain
b = list(chain.from_iterable( [[i+1]*a[i] for i in range(N)] ))

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

for i in range(H):
    print(' '.join(map(str, c[i])))