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

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

h = -1
ans = []
for i, b in enumerate(B):
    if i % W == 0:
        h += 1
        C = []
    C.append(b)
    if i % W == W-1:
        if h%2 == 0:
            ans.append(C)
        else:
            ans.append(C[::-1])

for C in ans:
    print(' '.join([str(c) for c in C]))

Solution (Python):
h, w = list(map(int, input().split()))
n = int(input())
a = list(map(int, input().split()))
b = [[0 for i in range(w)] for j in range(h)]
top = 0
bot = h-1
left = 0
right = w-1
x = 0
y = 0
flag = 0

for i in range(n):
  cnt = a[i]
  while cnt>0:
    b[y][x] = i+1
    cnt -= 1
    if flag==0 and x==left and y==bot:
      x += 1
      left += 1
      flag = (flag+1)%4
    elif flag==1 and x==right and y==bot:
      y -= 1
      bot -= 1
      flag = (flag+1)%4
    elif flag==2 and x==right and y==top:
      x -= 1
      right -= 1
      flag = (flag+1)%4
    elif flag==3 and x==left and y==top:
      y += 1
      top += 1
      flag = (flag+1)%4
    elif flag == 0:
      y += 1
    elif flag == 1:
      x += 1
    elif flag == 2:
      y -= 1
    elif flag == 3:
      x -= 1
for row in b:
  print(*row)

Solution (Python):
H, W = [int(i) for i in input().split()]
N = int(input())
a = [int(i) for i in input().split()]
c = [[0 for j in range(W)] for i in range(H)]
w = 0
h = 0
for i in range(N):
    for j in range(a[i]):
        c[h][w] = i+1
        if h%2 == 0:
            if w < W - 1:
                w += 1
            else:
                h += 1
        else:
            if w > 0:
                w -= 1
            else:
                h += 1
for i in range(H):
    print(*c[i])

Solution (Python):
H, W = map(int, input().split())
N = int(input())
*A, = map(int, input().split())
grid = [[-1]*W for _ in range(H)]
order = []
for i in range(H):
    if i % 2 == 0:
        for j in range(W): order.append((i, j))
    else:
        for j in reversed(range(W)): order.append((i, j))
idx = 0
for i, a in enumerate(A):
    color = i + 1
    for x, y in order[idx: idx+a]: grid[x][y] = color
    idx += a
for g in grid:
    print(*g)

Solution (Python):
import numpy as np

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

ans = np.zeros((H*W), dtype=np.int)

idx = 0
for i in range(1, N+1):
    ans[idx:idx+1+a[i-1]] = i
    idx += a[i-1]

ans = np.reshape(ans, (H, W))

x = [1, -1]

for i in range(H):
    print(' '.join(map(str, list(ans[i])[::x[i%2]])))

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

for i in range(N):
    b=[i+1]*a[i]
    a_all.extend(b)

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

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

Solution (Python):





from sys import stdin,setrecursionlimit


setrecursionlimit(10**6)
input=stdin.readline

h,w=map(int,input().split())
n=int(input().rstrip())
a=[int(i) for i in input().split()]
nh,nw=0,0
ans=[[0]*w for i in range(h)]
now=True
for i,j in enumerate(a,1):
    for _ in range(j):
        if nw<0:
            nh+=1
            nw+=1
            now=not now
        elif nw>=w:
            nh+=1
            nw-=1
            now=not now
        ans[nh][nw]=i
        if now:
            nw+=1
        else:
            nw-=1
for i in ans:
    print(*i)

Solution (Python):
H,W,N,*A=map(int,open(0).read().split())
for i,r in enumerate(zip(*[iter(sum([[i]*a for i,a in enumerate(A,1)],[]))]*W)):print(*r[::1-2*(i%2)])

Solution (Python):
H,W,N,*A=map(int,open(0).read().split())
C=[]
for i,a in enumerate(A,1):C+=[i]*a
for i,r in enumerate(zip(*[iter(C)]*W)):print(*r[::1-2*(i%2)])

Solution (Python):
H,W,N,*A=map(int,open(0).read().split())
C=[]
for i,a in enumerate(A,1):C+=[i]*a
for i,r in enumerate(zip(*[iter(C)]*W)):print(*r[::-1]if i%2 else r)

Solution (Python):
H,W,N,*A=map(int,open(0).read().split())
C=[]
for i,a in enumerate(A,1):
	C+=[i]*a
for i,r in enumerate(zip(*[iter(C)]*W)):
	print(*r[::-1]if i%2 else r)

Solution (Python):
H,W,N,*A=map(int, open(0).read().split())
C = []
for i,a in enumerate(A,1):
	C+=[i]*a
for i,r in enumerate(zip(*[iter(C)]*W)):
	print(*r[::-1]if i%2 else r)

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

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

for i, r in enumerate(zip(*[iter(C)] * W)):
    if i % 2 == 0:
        print(*r)
    else:
        print(*reversed(r))

Solution (Python):

import sys
input = sys.stdin.readline

def main():
    H, W = tuple(map(int, input().split()))
    N = int(input())
    A = tuple(map(int, input().split()))
    F = []  
    for color, cnt in enumerate(A, 1):
        F += [color] * cnt
    for p, i in enumerate(range(1, H + 1), 1):
        if p & 1:  
            print(*F[(i - 1) * W : i * W])
        else:
            print(*F[(i - 1) * W : i * W][::-1])


if __name__ == "__main__":
    main()

Solution (Python):

def main():
    H, W = tuple(map(int, input().split()))
    N = int(input())
    A = tuple(map(int, input().split()))
    F = []  
    for color, cnt in enumerate(A, 1):
        F += [color] * cnt
    par = 1  
    for i in range(1, H + 1):
        if par:
            print(*F[(i - 1) * W : i * W])
        else:
            print(*F[(i - 1) * W : i * W][::-1])
        par ^= 1


if __name__ == "__main__":
    main()

Solution (Python):

def main():
    H, W = tuple(map(int, input().split()))
    N = int(input())
    A = tuple(map(int, input().split()))
    G = []  
    for color, cnt in enumerate(A, 1):
        G += [color] * cnt
    par = 0  
    for i in range(1, H + 1):
        if not par:
            print(*G[(i - 1) * W : i * W])
        else:
            print(*G[(i - 1) * W : i * W][::-1])
        par ^= 1


if __name__ == "__main__":
    main()

Solution (Python):
import copy
h,w = map(int,input().split())
n = int(input())
a = list(map(int,input().split()))
l = []
l1 = []
key = 0
p = 0
q = 0
for i in range(n):
    count = a[i]
    for j in range(a[i]):
        l1.append(i+1)
        if q == w-1:
            if key % 2 == 0:
                l.append(l1)
            else:
                l1.reverse()
                l.append(l1)
            l1 = []
            q = 0
            key = key + 1
        else:
            q = q + 1
for i in l:
    print(*i)

Solution (Python):
def main():
    h,w = map(int,input().split())
    n = int(input())
    ls = [int(x) for x in input().split()]
    ans = [[0 for _ in range(w)] for _ in range(h)]
    i,j = 0,0
    now = 1
    while len(ls)!=0:
        cnt = ls.pop(0)
        while cnt!=0:
            if j==w:
                i+=1
                j= 0
            ans[i][j] = now
            cnt-=1
            j +=1
        now +=1
    
    for i in range(h):
        if i%2==0:
            for j in range(w):
                if j == w-1:
                    print(ans[i][j])
                else:
                    print(ans[i][j],end=" ")
        else:
            for j in reversed(range(w)):
                if j == 0:
                    print(ans[i][j])
                else:
                    print(ans[i][j],end=" ")

if __name__ == '__main__':
    main()

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007


def A():
    n,m = LI()
    a = LIR(n)
    ans = 0
    for i in range(2):
        for j in range(2):
            for k in range(2):
                a.sort(key = lambda x:(-1)**i*x[0]+(-1)**j*x[1]+(-1)**k*x[2])
                p = [0,0,0]
                for l in range(m):
                    p[0] += a[l][0]
                    p[1] += a[l][1]
                    p[2] += a[l][2]
                p = abs(p[0])+abs(p[1])+abs(p[2])
                if p > ans:
                    ans = p
    print(ans)
    return


def B():
    def root(x):
        if x == par[x]:
            return x
        par[x] = root(par[x])
        return par[x]

    def unite(x,y,su):
        x = root(x)
        y = root(y)
        su += comb_2(s[x])+comb_2(s[y])
        if rank[x] < rank[y]:
            s[y] += s[x]
            su -= comb_2(s[y])
            par[x] = y
        else:
            s[x] += s[y]
            su -= comb_2(s[x])
            par[y] = x
            if rank[x] == rank[y]:
                rank[x] += 1
        return su

    def comb_2(a):
        return a*(a-1)//2

    n,m = LI()
    v = LIR(m)
    par = [i for i in range(n)]
    rank = [0]*n
    s = [1]*n
    su = comb_2(n)
    ans = [su]
    for a,b in v[:0:-1]:
        a -= 1
        b -= 1
        if root(a) != root(b):
            su = unite(a,b,su)
        ans.append(su)
    for i in ans[::-1]:
        print(i)
    return


def C():
    def root(x):
        if par[x] == x:
            return par[x]
        r = root(par[x])
        d[x] += d[par[x]]
        par[x] = r
        return par[x]

    def weight(x):
        root(x)
        return d[x]

    def unite(x,y,w):
        w += weight(x)
        w -= weight(y)
        x = root(x)
        y = root(y)
        if rank[x] < rank[y]:
            par[x] = y
            d[x] = -w
        else:
            par[y] = x
            d[y] = w
            if rank[x] == rank[y]:
                rank[x] += 1
    n,m = LI()
    par = [i for i in range(n)]
    rank = [0]*n
    d = [0]*n
    for i in range(m):
        l,r,w = LI()
        l -= 1
        r -= 1
        if root(l) != root(r):
            unite(l,r,w)
        else:
            if weight(r)-weight(l) != w:
                print("No")
                return
    print("Yes")
    return


def D():
    h,w = LI()
    n = I()
    a = LI()
    ans = [0]*h*w
    p = 0
    for i in range(n):
        for j in range(a[i]):
            ans[p+j] = i+1
        p += a[i]
    for i in range(h):
        s = ans[i*w:(i+1)*w]
        if i%2:
            print(*s[::-1])
        else:
            print(*s)
    return


def E():

    return


def F():

    return


def G():

    return


def H():

    return


def I_():

    return


if __name__ == "__main__":
    D()

Solution (Python):
def split_list(l, n):

    for dx in range(0, len(l), n):
        yield l[dx:dx + n]

H, W = map(int, input().split())
N = int(input())
a = list(map(int, input().split()))
list1 = [0] * H * W

count = 0
for i in range(N):
    for j in range(a[i]):
        list1[count + j] = i + 1
    count += a[i]

ans_list = list(split_list(list1, W))

for i in range(H):
    list2 = list(ans_list[i])
    if i % 2 == 0:
        print(" ".join(map(str, list2)))
    else:
        list2.reverse()
        print(" ".join(map(str, list2)))

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

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

import numpy as np
res = np.zeros(H*W,dtype=np.int64)

pos = 0
for i in range(N):
    color = i+1
    for j in range(A[i]):
        res[pos+j] = color
    pos += A[i]
res = np.reshape(res,(H,W))
for i in range(H):
    if i%2 == 0:
        pass
    else:
        res[i] = res[i][::-1]


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())
l=list(map(int,input().split()))
l2=[]
lt=[]
a=1
for x in l:
  for i in range(x):
    if len(lt)>=W:
      if len(l2)%2==1:
        lt.reverse()
        l2.append(lt)
      else:
        l2.append(lt)
      lt=[][:]
      lt.append(a)
    else:
      lt.append(a)
  a+=1
if len(lt)>0:
  if len(l2)%2==1:
    lt.reverse()
    l2.append(lt)
  else:
    l2.append(lt)
for x in l2:
  print(' '.join([str(a) for a in x]))

Solution (Python):

import sys


def solve(H: int, W: int, N: int, a: "List[int]"):
    S = []
    for i, n in enumerate(a):
        j = i + 1
        S.extend([j] *  n)
    
    for h in range(H):
        row = S[h*W:h*W+W]
        if h % 2 == 0:
            print(' '.join(map(str, row)))
        else:
            print(' '.join(map(str, row[::-1])))

    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  
    W = int(next(tokens))  
    N = int(next(tokens))  
    a = [ int(next(tokens)) for _ in range(N) ]  
    solve(H, W, N, a)

if __name__ == '__main__':
    main()

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

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

Solution (Python):
import numpy as np

h, w = map(int, input().split())
N = int(input())
a = np.array(list(map(int, input().split())))

x = np.argsort(a)
x = x[::-1]
a = a[x]

tmp = np.zeros(h*w)

k = 0
for i in range(N):
    for j in range(a[i]):
        tmp[k] = x[i]+1
        k += 1

tmp = tmp.reshape((h,w))

for y in range(h):
    if y&1 == 0:
        for x in range(w):
            print(int(tmp[y][x]), end=" ")
        print()
    else:
        for x in range(w-1, -1, -1):
            print(int(tmp[y][x]), end=" ")
        print()

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

Solution (Python):
H,W=map(int,input().split())
N=int(input())
A=list(map(int,input().split()))
a=[[0]*W for i in range(H)]
r,c=0,0
x=1
for i in range(H*W):
    a[r][c]=x
    A[x-1]-=1
    if A[x-1]==0:
        x+=1
    if (r%2==0 and c==W-1 or r%2==1 and c==0) and r<H-1:
        r+=1
    elif r%2==0:
        c+=1
    else:
        c-=1
for l in a:
    print(' '.join(map(str,l)))

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

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

ans = [0] * (H * W)

index = 0

for color , a in enumerate(A) :
    for _ in range(a) :
        ans[index] = color + 1
        index += 1

for i in range(H) :
    sub = ans[i * W : (i + 1) * W]
    if i % 2 == 0 :
        for j in range(W) :
            print('{} '.format(sub[j]), end='')
    else :
        sub.reverse()
        for j in range(W) :
            print('{} '.format(sub[j]), end='')
    print('')

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

c = []
idx = 0
jdx = 0

for i, cnt in enumerate(a):
    i += 1
    for _ in range(cnt):
        
        if jdx%2 == 0:
            if idx < W:
                c += [i]
                idx += 1
            else:
                print(*c)
                c = [i]
                idx -= 1
                jdx += 1
        else:
            if idx > 0:
                c = [i] + c
                idx -=  1
            else:
                print(*c)
                c = [i]
                idx += 1
                jdx += 1

print(*c)

Solution (Python):
from functools import *

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

ans_list = ([[i+1] * a[i] for i in range(N)])
ans_list = reduce(lambda a, b: a + b, ans_list)

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

Solution (Python):
import math

H,W = [int(x) for x in input().split()]
N = int(input())
a = [int(x) for x in input().split()]
li = []
for ind,i in enumerate(a):
    for j in range(i):
        li.append(ind+1)
prt = ''
for ind,i in enumerate(li):
    if(math.floor(ind/W)%2 == 0):
        prt+=str(i)+' '
        if(ind%W==W-1):
            prt=prt[:-1]
            print(prt)
            prt=''
    else:
        prt=' '+str(i)+prt
        if(ind%W==W-1):
            prt=prt[1:]
            print(prt)
            prt=''

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

stage = [[0 for _ in range(W)] for _ in range(H)]
ds = ((0, 1), (1, 0), (0, -1), (-1, 0))

def set_val(p, v):
	global stage
	stage[p[0]][p[1]] = v

def get_val(p):
	return stage[p[0]][p[1]]

current_p = [0, 0]
set_val(current_p, 1)
A[0] -= 1

for i, a in enumerate(A):
	n = a
	while 0 < n:
		for d in ds:
			next_p = (current_p[0] + d[0], current_p[1] + d[1])
			if 0 <= next_p[0] < H and 0 <= next_p[1] < W:
				v = get_val(next_p)
				if v == 0:
					set_val(next_p, i + 1)
					current_p = next_p
					break
		n -= 1

for line in stage:
	print(*line)

Solution (Python):
h,w=map(int,input().split())
n=int(input())
arr=list(map(int,input().split()))
ans=[[0]*w for _ in range(h)]
hpos=0
wpos=0
apos=0
cnt=0
while 1:
  while cnt<arr[apos]:
    ans[hpos][wpos]=apos+1
    if hpos%2==0:
      wpos+=1
      if wpos==w:
        hpos+=1
        wpos=w-1
    else:
      wpos-=1
      if wpos==-1:
        hpos+=1
        wpos=0
    cnt+=1
  cnt=0
  apos+=1
  if hpos==h:
    break
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()))
m=[[0 for _ in range(w)] for _ in range(h)]
c=0
for y in range(h):
    for x in range(w):
        if y%2==0:
            m[y][x]=c+1
            a[c]-=1
        else:
            m[y][w-1-x]=c+1
            a[c]-=1
        if a[c]==0:
            c+=1
for v in range(h):
    s=str(m[v][0])
    for u in range(1,w):
        s+=" "+str(m[v][u])
    print(s)

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:
        print(*(list(reversed(ans[i * W:(i + 1) * W]))))
    else:
        print(*ans[i * W:(i + 1) * W])

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


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

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

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

h,w = li()
n = ni()
a = list(li())

grid = [[0]*w for _ in range(h)]
cnt = 0
for i, ai in enumerate(a):
    for _ in range(ai):
        grid[cnt//w][cnt%w] = i+1
        cnt += 1

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

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 _ in range(h)]

co=0
for i in range(n):
    for j in range(a[i]):
        if (co//w)%2==1:
            ans[co//w][w-1-co%w]=i+1
        else:
            ans[co//w][co%w]=i+1
        co+=1
for i in range(h):
    for j in range(w-1):     
        print(ans[i][j],end=" ")
    print(ans[i][w-1])

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

i = 0
buf = [0] * W
k = 1
for x, ai in zip(range(1, N+1), map(int, input().split())):
  for _ in range(ai):
    buf[i] = x
    i += 1
    if i >= W:
      print(" ".join(map(str, buf[::k])))
      i = 0
      k = -k

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

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

cnt = 1
cnt2 = 0
for h in range(H):
    if h % 2 == 0:
        for w in range(W):
            L[h][w] = cnt
            cnt2 += 1
            if cnt2 == A[cnt-1]:
                cnt += 1
                cnt2 = 0
            
    else:
        for w in range(W)[::-1]:
            L[h][w] = cnt
            cnt2 += 1
            if cnt2 == A[cnt-1]:
                cnt += 1
                cnt2 = 0
for h in range(H):
    print(*L[h])

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

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

for i in range(H):
    row = ""
    for j in range(W):
        r = W*i
        if i % 2 == 0:
            c = j
        else:
            c = W - 1 - j
        row += str(res[r+c])
        row += " "
    print(row)

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

aidx = 0
didx = 0

while didx < H*W:
    idx = 0
    while idx < a[aidx]:
        d1 = math.ceil((didx+1)/W)-1
        if d1 % 2 == 1:
            d2 = W - 1 - didx % W
        else:
            d2 = didx % W
        d[d1][d2] = aidx+1
        idx += 1
        didx += 1
    aidx += 1
    
for i in range(H):
    print(' '.join(map(str,d[i])))

Solution (Python):
from itertools import cycle, chain, repeat


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




c = {
    (i, j): k
    for k, i, j in zip(
        chain(
            *(
                repeat(k, m)
                for k, m in enumerate(a, 1)
            )
        ),
        chain(
            *(
                repeat(r, W)
                for r in range(H)
            )
        ),
        cycle(
            chain(
                range(W),
                reversed(range(W)),
            )
        )
    )
}
ans = '\n'.join(
    ' '.join(
        str(c[(i, j)])
        for j in range(W)
    )
    for i in range(H)
)


print(ans)

Solution (Python):
from itertools import accumulate, cycle, chain, repeat
from bisect import bisect


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




A = list(accumulate(a))
c = {
    (i, j): bisect(A, k) + 1
    for k, i, j in zip(
        range(H * W),
        chain(
            *(
                repeat(r, W)
                for r in range(H)
            )
        ),
        cycle(
            chain(
                range(W),
                reversed(range(W)),
            )
        )
    )
}
ans = '\n'.join(
    ' '.join(
        str(c[(i, j)])
        for j in range(W)
    )
    for i in range(H)
)


print(ans)

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

Solution (Python):
h,w=map(int,input().split())
n=int(input())
A=[int(i) for i in input().split()]
AA=[]
for i in range(n):
  ai=A[i]
  for j in range(ai):
    AA.append(i+1)
for i in range(h):
  AAA=AA[i*w:(i+1)*w]
  if i%2!=0:
    AAA=AAA[::-1]
  print(*AAA)

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

c = [[0]*W for i in range(H)]
x = 0
y = 0
for i in range(N):
    for j in range(A[i]):
        c[y][x] = i+1
        if y % 2:
            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(map(str,c[i])))

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

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

def gen():
  for h in range(H):
    for w in range(W):
      if h%2==0:
        yield (h,w)
      else:
        yield (h, W-1-w)

snake_gen = gen()
for i,a in enumerate(A):
  for _ in range(a):
    h, w = snake_gen.__next__()
    ans[h][w] = str(i+1)
    
for r in ans:
  print(' '.join(r))

Solution (Python):
import itertools
H,W=map(int,input().split())
N=int(input())
a=itertools.chain(*(itertools.repeat(i+1, cnt) for i,cnt in enumerate(map(int,input().split()))))

for h in range(H):
	it = map(str, itertools.islice(a, W))
	if h % 2 == 1:
		it = reversed(list(it))
	print(" ".join(it))

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

p = [0,0]
for i in range(n):
    for j in range(a[i]):
        c[p[0]][p[1]]=str(i+1)
        if p[0]%2==0:
            if p[1]==w-1:
                p[0]+=1
            else:
                p[1]+=1
        else:
            if p[1]==0:
                p[0]+=1
            else:
                p[1]-=1

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

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

Solution (Python):
from collections import deque
import sys
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def MI(): return map(int, sys.stdin.readline().split())
def II(): return int(sys.stdin.readline())


H, W = MI()
N = II()
a = LI()
color = deque()
ans = [[0] * W for _ in range(H)]




for i, num in enumerate(a):
    for j in range(num):
        color.append(i + 1)






"""
while len(coordinate_q) != 0:
    y_tmp, x_tmp = coordinate_q.popleft()
    for i in range(4):
        y, x = y_tmp + y_diff[i], x_tmp + x_diff[i]
        if 0 <= y <= H-1 and 0 <= x <= W-1 and visited[y][x] == 0:
            ans[y][x] = color.popleft()
            visited[y][x] = 1
            coordinate_q.append([y, x])
"""

for i in range(H):
    for j in range(W):
        if i % 2 == 0:
            ans[i][j] = color.popleft()
        else:
            ans[i][-(j+1)] = color.popleft()

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

Solution (Python):



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

import numpy as np
c = np.zeros(H*W,dtype=np.int32)

first = 0
for i,a in enumerate(A,1):
  c[first:first+a] = i
  first += a
  
c2d = c.reshape((H,W))

c2d[1::2,:] = c2d[1::2,::-1]

for i in range(H):
  print(' '.join(c2d[i].astype(str)))

Solution (Python):
import sys, os

f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
    sys.stdin = open('./input.txt', 'r')

def solve():
    h, w = f()
    n = f()[0]
    a = f()

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

    cur = 0
    for i in range(h):
        if i%2==0:
            for j in range(w):
                if a[cur]==0:
                    cur+=1
                g[i][j] = cur+1
                a[cur] -= 1
        else:
            for j in reversed(range(w)):
                if a[cur]==0:
                    cur+=1
                g[i][j] = cur+1
                a[cur] -= 1

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

            if (j==w-1):
                print()
            else:
                print(' ', end='')


solve()

Solution (Python):


import itertools
from collections import Counter
from collections import defaultdict
import bisect

def main():
    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 in range(N):
        rest = A[i]
        while rest > 0:
            ans[h][w] = i + 1
            rest -= 1
            if h % 2 == 0:
                if 0 <= w < W - 1:
                        w += 1
                else:
                    h += 1
            else:
                if 0 < w <= W - 1:
                        w -= 1
                else:
                    h += 1

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


if __name__ == '__main__':
    main()

Solution (Python):
H,W = map(int,input().split())
N = int(input())
a = list(map(int,input().split()))
import copy
h,w = 0,0
flg = True
ansl = []
l = []
for i in range(N):
    for j in range(a[i]):
        if W > w:
            l.append(i+1)
            w += 1
        else:
            if flg == True:
                flg = False
                ansl.append(copy.deepcopy(l))
            else:
                flg =True
                ansl.append(copy.deepcopy(l[::-1]))
            w = 1
            l = []
            l.append(i+1)

if flg == True:
    ansl.append(copy.deepcopy(l))
else:
    ansl.append(copy.deepcopy(l[::-1]))

for l in ansl:
    print(' '.join(map(str,l)))