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

Solution (Python):
def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

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

H,W = two_int()
N=one_int()
A=many_int()

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

output=[]
for i in range(1,H+1):
    output.append(lists[W*(i-1):W*i])

for i,o in enumerate(output):
    temp = list(map(str,o))
    if i%2==0:
        print(" ".join(temp))
    else:
        print(" ".join(temp[::-1]))

Solution (Python):
h,w = map(int,input().split())
n = int(input())
a = list(map(int,input().split()))
ww = [0 for i in range(w)]
hw = [list(ww) for i in range(h)]

count = 1
tmp_w = 0
tmp_h = 0
for i in a:
    for j in range(i):
        
        hw[tmp_h][tmp_w] = str(count)
        if tmp_w != w - 1:
            tmp_w += 1
        else:
            tmp_h += 1
            tmp_w = 0
    count += 1
y = 1
for i in hw:
    if y % 2 == 0:
        i.reverse()
    y += 1
    print(' '.join(i))

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

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

Solution (Python):
import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)
INF = 10**30

def main():
    h, w = list(map(int, input().strip().split()))
    n = int(input().strip())
    a = list(map(int, input().strip().split()))
    c = [[0] * w for _ in range(h)]
    k = 1
    l = 0
    for i in range(h):
        for j in range(w):
            if i % 2 == 0:
                c[i][j] = k
            else:
                c[i][w-1-j] = k
            a[l] -= 1
            if a[l] == 0:
                k += 1
                l += 1
    for i in range(h):
        print(' '.join(map(str, c[i])))

if __name__ == '__main__':
    main()

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

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

for i in range(n):
  for j in range(aa[i]):
    nums.append(i+1)
  
for i in range(len(nums)):  
  ans[i//w][i%w] = nums[i]  
  
for hi in range(h):
  if hi % 2 == 1:    
    print(' '.join(map(str,list(reversed(ans[hi])))))
  else:
    print(' '.join(map(str,ans[hi])))

Solution (Python):
H,W=map(int,input().split())
N=int(input())
A=[int(i) for i in input().split()]
M=[[-1 for _ in range(W)] for _ in range(H)]
i,j=0,0
direc=0
circ=0

class solve:
    def __init__(self, L):
        self.L=L
        self.i=0
        self.len=len(L)
    def get(self):
        if self.L[self.i] != 0 :
            self.L[self.i] -= 1
            return self.i+1
        else:
            self.i += 1
            self.L[self.i] -= 1
            return self.i+1
ans=solve(A)    

for _ in range(H*W):
    M[i][j]=ans.get()
    if direc == 0:
        if i == H-1-circ:
            direc = 1
            j += 1
        else:
            i += 1
    elif direc == 1:
        if j == W-1-circ:
            direc = 2
            i -= 1
        else:
            j += 1
    elif direc == 2:
        if i == circ:
            direc = 3
            j -= 1
        else:
            i -= 1
    elif direc == 3:
        if j == 1+circ:
            direc = 0
            circ += 1
            i += 1
        else:
            j -= 1
for m in M:
    print(*m)

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

for i, a in enumerate(A):
    for j in range(a):
        B[k].append(i + 1)
        if len(B[k]) == W:
            k += 1

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

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

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

h = 0
w = 0
for idx, a in enumerate(A):
    num = idx + 1
    for _ in range(a):
        H_W[h][w] = num
        if w % 2 == 0:
            if 0 <= h + 1 < H:
                h += 1
            else:
                w += 1
        else:
            if 0 <= h - 1 < H:
                h -= 1
            else:
                w += 1

for a in H_W:
    print(" ".join(map(str, a)))

Solution (Python):
H,W,N,*A = map(int,open(0).read().split())
h,w = 0,0
D = [[0]*W for i in range(H)]
for i in range(N):
    c=0
    while c<A[i]:
        D[h][w] = i+1
        if h%2==0:w+=1
        else:w-=1
        if w<0 or w>=W:
            h+=1
            w = 0 if h%2==0 else W-1
        c+=1
for d in D:print(*d)

Solution (Python):
H, W, N, *a = map(int, open(0).read().split())
ans = []
for i in range(N):
  ans.extend([i+1 for _ in range(a[i])])
ans = [ans[i:i+W] for i in range(0, H*W, W)]
for i in range(1, H, 2):
  ans[i] = reversed(ans[i])
for r in ans:
  print(' '.join(map(str,r)))

Solution (Python):
h,w=map(int,input().split())
n=int(input())
l=list(map(int,input().split()))
count=0
ans=[["0" for i in range(w)] for j in range(h)]
now=0
index=0
locate=[0,0]
while count<h*w:
  count+=1
  if now==0:
    now+=l[index]
    index+=1
  x=locate[0]
  y=locate[1]
  ans[x][y]=str(index)
  if x%2==0:
    if y<w-1:
      y+=1
    else:
      x+=1
  else:
    if y!=0:
      y-=1
    else:
      x+=1
  now-=1
  locate=[x,y]
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()))  


ans = [[-1]*W for _ in range(H)]
colors = []
for i in range(len(A)):
    colors.extend([i+1]*A[i])
pointa = 0
for i in range(H):
    
    if i % 2 == 0:
        for j in range(W):
            ans[i][j] = colors[pointa]
            pointa += 1
    else:
        for j in reversed(range(W)):
            ans[i][j] = colors[pointa]
            pointa += 1
for a in ans:
    print(*a)

Solution (Python):
H,W=map(int,input().split())
N=int(input())
L=list(map(int,input().split()))
col=0
M=[[0]*W for _ in range(H)]



for i in range(H):
    if i%2==0:
        for j in range(W):
        
        
            if L[col]>0:
                M[i][j]=col+1
                L[col]-=1
            if L[col]==0:
                col+=1
    else:
        for j in range(W-1,-1,-1):
        
        
            if L[col]>0:
                M[i][j]=col+1
                L[col]-=1
            if L[col]==0:
                col+=1

for q in range(H):
    for w in range(W):
        print(M[q][w],end=" ")
    print()

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

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

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

for line in B:
    print(*line)

Solution (Python):
H,W=map(int,input().split())
N=int(input())
A=list(map(int,input().split()))
C=[[0 for w in range(W)] for h in range(H)]

i=1
for h in range(H):
    for w in (range(W) if h%2==0 else range(W-1,-1,-1)):
        
        C[h][w]=i
        A[i-1]-=1
        if not A[i-1]:
            i+=1
for c in C:
    c=list(map(str,c))
    print(" ".join(c))

Solution (Python):

import sys














def main():
    H, W, n, *a = [int(x) for x in sys.stdin.read().split()]
    color_count = [None] + a 
    
    canvas = [[None for _ in range(W)] for _ in range(H)]
    
    h, w = 0, 0
    for i in range(1, n+1):
        while color_count[i] > 0:
            canvas[h][w] = i
            color_count[i] -= 1

            if h % 2 == 0:
                w += 1
                if w == W:
                    h += 1
                    w = W - 1
            else:
                w -= 1
                if w == -1:
                    h += 1
                    w = 0

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

if __name__ == "__main__":
    main()

Solution (Python):
h, w = [int(i) for i in input().split()]
n = int(input())
a = [int(i) for i in input().split()]

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

i, j = 0, 0
for k, l in enumerate(a):
    for _ in range(l):
        c[i][j] = str(k + 1)
        j += 1
        if j == w:
            j = 0
            i += 1

for i in range(h):
    if i % 2 == 1:
        c[i] = list(reversed(c[i]))

for i in c:
    print(" ".join(i))

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


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

Solution (Python):

import sys, math
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
sys.setrecursionlimit(10**8)
inf = float('inf')
count=0

h,w=map(int,input().split())
ans=[[0]*w for i in range(h)]
n=int(input())
A=list(map(int,input().split()))
now=0
for i in range(h):
    for j in range(w):
        a=j
        if i%2==1:
            j=w-j-1
        if not A[now]:
            now+=1
        A[now]-=1
        ans[i][j]=now+1
        j=a
        
for a in ans:
    print(*a)

Solution (Python):
h,w=map(int,input().split())
n=int(input())
A=list(map(int,input().split()))
MAP=[[0 for i in range(w)] for j in range(h)]
s,num=0,1
for i in range(h):
    if i%2==0:add=[k for k in range(w)]
    else:add=[k for k in reversed(range(w))]
    for j in add:
        MAP[i][j]=num
        A[s] -=1
        if A[s]==0:
            s +=1
            num +=1

for i in range(h):print(*MAP[i])

Solution (Python):


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

l=[]
for i in range(N):
    l+=(([i+1]*A[i]))



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

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

cur_W = 0 
cur_H = 0 

answer = [[0] * W for _ in range(H)]
sgn = 1 
for i in range(1, len(A)+1):
    for j in range(A[i-1]):
        answer[cur_H][cur_W] = i
        cur_W += sgn
        if not 0 <= cur_W <= W-1:
            cur_W -= sgn
            cur_H += 1
            sgn *= -1

for x in answer:
    print(*x)

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):
h,w=map(int,input().split())
n=int(input())
a=list(map(int,input().split()))
s=[]
for i in range(1,n+1):
  for j in range(a[i-1]):
    s.append(str(i))
b=[]
for i in range(h):
  if i%2==0:
    b.append(s[:w])
  else:
    t=s[:w]
    b.append(t[::-1])
  s=s[w:]
for i in range(h):
  print(*b[i])

Solution (Python):
import sys


		
def main():
	h, w = map(int, input().split())
	n = int(input())
	anl=list(map(int, input().split()))
	ans=[]
	cm=0
	for color, ni in enumerate(anl, start=1):
		v=ni
		while v > 0:
			cl = w - len(ans)
			if cl > v:
				ans = ans + [color] * v
				v=0
			else:
				ans = ans + [color] * cl
				v-=cl
				ans = ans
				if cm % 2 == 1:
					ans=ans[::-1]
				pp=' '.join([str(v) for v in ans])
				print(pp)
				cm+=1
				ans=[]
	
	

isTest=False

def pa(v):
	if isTest:
		print(v)

if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
		isTest=True
	else:
		pass
		
			
	ret = main()
	if ret is not None:
		print(ret)

Solution (Python):
h, w = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))
c = 0
l = [ [0]*w for _ in range(h) ]
for i in range(n):
  for j in range(a[i]):
    l[c//w][c%w] = i+1
    c += 1

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

Solution (Python):
def main():
    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)]
    x=y=key=0
    for i in range(n):
        for _ in range(a[i]):
            ans[x][y]=str(i+1)
            if key==0:
                y+=1
            else:
                y-=1
            if key==0 and y==w:
                x+=1
                y-=1
                key=1
            elif key==1 and y==-1:
                x+=1
                y+=1
                key=0
    for i in range(h):
        print(" ".join(ans[i]))
if __name__ == "__main__":
    main()

Solution (Python):
h,w = map(int,input().split())
n = int(input())
l = []
a = list(map(int,input().split()))
for i in range(n):
  for j in range(a[i]):
    l.append(i+1)
for i in range(h):
  if i % 2 == 0:
    print(' '.join(map(str,l[i*w:(i+1)*w])))
  else:
    b = l[i*w:(i+1)*w]
    b.reverse()
    print(' '.join(map(str,b)))

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

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

Solution (Python):
from collections import deque

def main():
    H, W = map(int, input().split())
    N = int(input())
    A = list(map(int, input().split()))
    d = deque()
    for i in range(N):
        for a in range(A[i]):
            d.append(i+1)
    C = [[0] * W for _ in range(H)]

    for h in range(H):
        if h % 2 == 0:
            for w in range(W):
                C[h][w] = d.popleft()
        else:
            for w in range(W-1,-1,-1):
                C[h][w] = d.popleft()
    
    for h in range(H):
        print(*C[h])


if __name__ == "__main__":
    main()

Solution (Python):
h, w = map(int, input().split())
n = int(input())
a_array = [int(x) for x in input().split()]

ans = [[0 for x in range(w)] for y in range(h)]
index = 0
now = a_array[index]
reverse = True

for i in range(h):
    if reverse:
        w_range = [x for x in range(w)]
        reverse = False
    else:
        w_range = [x for x in reversed(range(w))]
        reverse = True
    for j in w_range:
        if now == 0:
            index += 1
            now = a_array[index]
        ans[i][j] = index+1
        now -= 1

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 = list(map(int, input().split()))

breakA = []
for i, v in enumerate(A):
  for _ in range(v):
    breakA.append(i + 1)



import itertools
def splitparN(iterable, N):
    for i, item in itertools.groupby(enumerate(iterable), lambda x: x[0] // N):
        yield (x[1] for x in item)

for i, x in enumerate(splitparN(breakA, W)):
  if i % 2 == 0:
    print(*list(x))
  else:
    print(*list(x)[::-1])

Solution (Python):

def main():
    """
    move left to right in odd rows and vice versa in even rows
    1 s--->
    2 <----
    3 --->t
    """
    H, W, N, *A = map(int, open(0).read().split())
    grid = sum([[color] * cnt for color, cnt in enumerate(A, 1)], [])
    for i, g in enumerate(zip(*[iter(grid)] * W), 1):
        if i & 1:
            print(*g)
        else:
            print(*g[::-1])


if __name__ == "__main__":
    main()

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)]
i = 0
j = 0
dj = 1
k = 0
l = 0
while(l<H*W):
  if A[k] == 0:
    k += 1
  M[i][j] = k+1
  A[k] -= 1
  j += dj
  if j > W-1:
    j = W-1
    dj = -1
    i += 1
  elif j < 0:
    j = 0
    dj = 1
    i += 1
  l += 1
for m in M:
  print(*m)

Solution (Python):
import sys
from itertools import accumulate
h, w = [int(i) for i in sys.stdin.readline().split()]
n = int(input())
a_ls = [int(i) for i in sys.stdin.readline().split()]
cumsum = [j for j in accumulate(a_ls)]
start = [0,0]
flg = True
_map = [[0 for i in range(w)] for j in range(h)]
color = 1
cnt = 0
while True:
    if cnt in cumsum:
        color += 1
    y, x = start
    _map[y][x] = color
    if flg:
        if x + 1 >= w:
            if y + 1 >= h:
                break
            flg = False
            y += 1
        else:
            x += 1
    else:
        if x - 1 < 0:
            if y + 1 >= h:
                break
            y += 1
            flg = True
        else:
            x -= 1
    _next = [y, x]
    start = _next
    cnt += 1

for y in range(h):
    print(" ".join([str(j) for j in _map[y]]))

Solution (Python):


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

c_list = []

for i, a in enumerate(a_list):
    c_list += [i + 1] * a

for i in range(H):
    w_list = c_list[W * i : W * (i + 1)]
    if i % 2 == 1:
        w_list = reversed(w_list)
    print(" ".join([str(w) for w in w_list]))

Solution (Python):
from sys import stdin
H, W = [int(_) for _ in stdin.readline().rstrip().split()]
N = int(stdin.readline().rstrip())
A = [int(a) for a in stdin.readline().rstrip().split()]

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

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

index = 0
for h in range(H):
  for w in range(W):
    ans[h][w] = flattened[index]
    index += 1

for h in range(H):
  if h % 2 == 0:
    print(*ans[h], sep=" ")
  else:
    print(*ans[h][::-1], sep=" ")

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**6)
    h, w = map(int, input().split())
    n = int(input())
    a = list(map(int, input().split()))
    dx = [1, -1]
    mode = 0
    ans = [[0]*w for _ in range(h)]
    x = 0
    y = 0
    i = 0
    while(0 <= y < h):
        while(0 <= x < w):
            if(a[i] > 0):
                ans[y][x] = i+1
                x += dx[mode]
                a[i] -= 1
            else:
                i += 1
        y += 1
        mode = 1-mode
        x += dx[mode]
    for i in ans:
        print(*i)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter,defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

h,w = inpl()
n = inp()
a = inpl()
q = deque(a)
tmp = q.popleft()
cnt = 1
res = [[0 for i in range(w)] for j in range(h)]
for y in range(h):
    for x in range(w):
        if y%2:
            x = w - x - 1
        if tmp == 0:
            tmp = q.popleft()
            cnt += 1
        res[y][x] = cnt
        tmp -= 1
for y in range(h):
    for x in range(w):
        print(res[y][x])

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

ind = 0
counter = 0
ans = [[0 for _ in range(w)] for _ in range(h)]
for i in range(h):
    if i%2 == 0:
        for j in range(w):
            ans[i][j] = ind+1
            counter += 1
            if counter == a[ind]:
                ind += 1
                counter = 0
    else:
        for j in range(w):
            ans[i][w-j-1] = ind+1
            counter += 1
            if counter == a[ind]:
                ind += 1
                counter = 0
for i in range(h):
    for j in range(w):
        print(ans[i][j], end=' ')
    print()

Solution (Python):
H,W= list(map(int, input().split()))
N= int(input())
a= list(map(int, input().split()))
li=[[0]*W for i in range(H)]
color=0
x=0;y=0
r=0;vec=0
for i in a:
    color+=1
    for _ in range(i):
        li[x][y]=color
        if vec:
            if r==1:
                y+=1
            else:
                y-=1
        else:
            if r==0:
                x+=1
            else:
                x-=1
        try :
            if li[x][y]!=0:
                if vec:
                    if r==1:
                        y-=1
                    else:
                        y+=1
                else:
                    if r==0:
                        x-=1
                    else:
                        x+=1
                r+=1
                r%=4
                vec^=1
                if vec:
                    if r==1:
                        y+=1
                    else:
                        y-=1
                else:
                    if r==0:
                        x+=1
                    else:
                        x-=1
        except:
            if vec:
                if r==1:
                    y-=1
                else:
                    y+=1
            else:
                if r==0:
                    x-=1
                else:
                    x+=1
            r+=1
            r%=4
            vec^=1
            if vec:
                if r==1:
                    y+=1
                else:
                    y-=1
            else:
                if r==0:
                    x+=1
                else:
                    x-=1
for i in li:
    for j in i:
        print(j,end=" ")
    print("")

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

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

n = int(input())

arr = list(map(int,input().split()))
ans = [[0 for i in range(w)] for j in range(h)]

right = True
temp = 0
ind = 1
for i in range(h):
    for j in range(w):
        if right:
            ans[i][j] = ind
            temp += 1
        else:
            ans[i][-j-1] = ind
            temp += 1
        if temp>=arr[ind-1]:
            ind += 1
            temp = 0
    right = not right

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

Solution (Python):
h,w  = map(int,input().split())
n = int(input())
a = list(map(int,input().split()))
res = []
for _ in range(h):
    res.append([])
line = 0

def insert(res,i,l):
    if l %2 == 0:
        res[l].append(str(i+1))
    else:
        res[l].insert(0,str(i+1))

for i in range(0,n):
    ammount=a[i]
    while len(res[line]) < w and ammount > 0:
        insert(res,i,line)
        ammount -= 1
    if len(res[line]) == w:
        line += 1
    while ammount >= w:
        res[line] = [str(i+1) for _ in range(w)]
        ammount -= w
        line += 1
    if ammount > 0:
        res[line] = [str(i+1) for _ in range(ammount)]

for i in range(0,h):
    print(' '.join(res[i]))

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    H,W = map(int,input().split())
    N = int(input())
    A = list(map(int,input().split()))
    ans = [[0]*W for i in range(H)]
    x = 0
    y = 0
    flag = 0
    for i in range(N):
        color = i+1
        num = A[i]
        for j in range(num):
            ans[y][x] = color
            if flag==0:
                x+=1
                if x==W:
                    x = W-1
                    flag = 1
                    y += 1
            else:
                x-=1
                if x==-1:
                    x = 0
                    flag=0
                    y += 1
    for i in range(H):
        print(*ans[i])
    
main()

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

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

a=0
ii=0
for i in range(H):
  for j in range(W):
    if a==0:
      a=A[ii]
      ii+=1
    G[i][j]=str(ii)
    a-=1

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

Solution (Python):
import sys
import heapq
import bisect

mod = 10**9+7
dd = ((-1,0),(1,0),(0,-1),(0,1))

def I(): return(int(sys.stdin.readline()))
def LI(): return([int(x) for x in sys.stdin.readline().split()])
def S(): return(sys.stdin.readline()[:-1])
def IR(n): return([I() for _ in range(n)])

def GCD(a,b):
    while b!=0:
        a,b = b,a%b
    return a

def LCM(a,b):
    return a * b // GCD(a,b)

def main():
    H,W = LI()
    N = I()
    A = LI()
    HW = [[0] * W for _ in range(H)]
    
    a_flag = [1,A[0]]
    for h in range(H):
        if h%2 == 0:
            range_W = range(W)
        else:
            range_W = range(W-1,-1,-1)
        for w in range_W:
            HW[h][w] = a_flag[0]
            if a_flag[1] == 1 and a_flag[0] < len(A):
                a_flag = [a_flag[0]+1,A[a_flag[0]]]
            else:
                a_flag[1] -= 1

    for x in HW:
        print(" ".join(map(str,x)))



            

if __name__ == "__main__":
    main()

Solution (Python):
from collections import Counter
import sys
sys.setrecursionlimit(1000000)

def solve():
  h,w = (int(i) for i in input().split())
  n = int(input())
  a = list(int(i) for i in input().split())
  query = []
  flag = True
  for i in range(n):
    times = a[i]
    number = i+1
    for j in range(times):
      query.append(number)
  ans = []
  retu = []
  while query:
    nextnumber = query.pop(0)
    if len(retu) < w:
      retu.append(nextnumber)
    else:
      ans.append(retu)
      retu = [nextnumber]
  ans.append(retu)
  for i in range(h):
    if i%2 == 1:
      ans[i] = ans[i][::-1]
  for j in range(h):
    print(*ans[j])
solve()

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

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

for i in range(h):
    f, e, s = 0, w, 1
    if i % 2: f, e, s = w-1, -1, -1 
    for j in range(f, e, s):
        ai += a[ai] <= 0
        a[ai] -= 1
        c[i][j] = ai+1

for l in c: print(*l)

Solution (Python):
H, W = map(int, input().split())
N = int(input())
a = list(map(int, input().split()))
grid = [[0] * W for i in range(H)]
count = 0
reverse = False
for i in range(H):
    if reverse:
        for j in reversed(range(W)):
            if a[count] == 0:
                count += 1
            grid[i][j] = count+1
            a[count] -= 1
        reverse = False
    else:
        for j in range(W):
            if a[count] == 0:
                count += 1
            grid[i][j] = count+1
            a[count] -= 1

        reverse = True


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

Solution (Python):
import sys
from collections import defaultdict
from heapq import *
sys.setrecursionlimit(200000)
input = sys.stdin.readline

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



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

def bfs(i, j, remain, now):
    if now > N:
        return

    if remain == 0:
        bfs(i, j, L[now], now + 1)
        return

    if m[i][j] == 0:
        m[i][j] = now
    else:
        return

    if i + 1 < H and m[i + 1][j] == 0:
        bfs(i + 1, j, remain - 1, now)

    if i - 1 >= 0 and m[i - 1][j] == 0:
        bfs(i - 1, j, remain - 1, now)

    if j + 1 < W and m[i][j + 1] == 0:
        bfs(i, j + 1, remain - 1, now)

    if j - 1 >= 0 and m[i][j - 1] == 0:
        bfs(i, j - 1, remain - 1, now)        

bfs(0, 0, L[0], 1)

for v in m:
    print(*v)

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):
h,w=map(int,input().split())
n=int(input())
a=list(map(int,input().split()))
ans=[]
i=0
rest=a[i]
for j in range(h):
    ans_j=[]
    for k in range(w):
        if(rest==0):
            i+=1
            rest=a[i]

        if(j%2==0):
            ans_j.append(i+1)
            rest-=1
        else:
            ans_j.insert(0, i+1)
            rest-=1
    ans.append(ans_j)

for i in range(len(ans)):
    for j in range(len(ans[i])):
        print(ans[i][j],end=" ")
    print()

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

def main():
    H,W = map(int,input().split())
    N = int(input())
    a = list(map(int,input().split()))
    cl = []
    for i in range(N):
        cl += [(i+1) for _ in range(a[i])]

    ans = [[] for _ in range(H)]
    for i in range(H):
        ans[i] = cl[(W*i):(W*(i+1))]

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

if __name__ == "__main__":
    main()

Solution (Python):


from collections import deque

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

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

res=deque(res)

for i in range(h):
    tmp=[]
    for j in range(w):
        tmp.append(res.popleft())
    if i%2:print(*tmp[::-1])
    else:print(*tmp)