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

out=[]
for i,a in enumerate(A):
  for _ in range(a):
    out.append(i+1)
ans,cnt=[],0
for i in range(h):
  if i%2==0:
    ans.append([out[cnt+j] for j in range(w)])
  else:
    ans.append([out[cnt+w-1-j] for j in range(w)])
  cnt+=w
for a in ans:
  print(*a)

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)
cnt = 0
for i in range(0,H*W,W):
  if cnt % 2 != 0:
    print(*ans[i:i+W][::-1],end="\n")
  else:
    print(*ans[i:i+W],end="\n")
  cnt += 1

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




def get_position(H, W):
    for i in range(H):
        if i % 2 == 0:
            for j in range(W):
                yield i, j
        else:
            for j in range(W-1, -1, -1):
                yield i, j


buf = [['' for _ in range(W)] for _ in range(H)]
positions = get_position(H, W)
for c, a in enumerate(A):
    for _ in range(a):
        i, j = positions.__next__()
        
        buf[i][j] = str(c+1)

for b in buf:
    print(' '.join(b))

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 not i%2:
        print(*ans[w*i:w*i+w])
    else:
        print(*ans[w*i+w-1:w*i-1:-1])

Solution (Python):
H,W = map(int,input().split())
N = int(input())
A = []
for i, a in enumerate(map(int,input().split())):
  A.append([a,i+1])
A.sort(reverse= True)
grids = [[-1 for _ in range(W)] for _ in range(H)]

pos = [0,0]
for a,i in A:
  cnt = 0
  while cnt != a:
    grids[pos[1]][pos[0]] = i
    cnt += 1
    if pos[1]%2==0:
      if pos[0] != W-1: pos[0] +=1
      else: pos = [W-1,pos[1]+1]
    else:
      if pos[0] != 0: pos[0] -=1
      else: pos = [0,pos[1]+1]

[print(*row) for row in grids]

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

area = [[0 for i in range(w)] for i in range(h)]
num = 1
count = 0

for i in range(h):
    if i % 2 == 0:
        for j in range(w):
            area[i][j] = num
            count += 1
            if count == a[num-1]:
                num += 1
                count = 0
    else:
        for j in range(w-1, -1, -1):
            area[i][j] = num
            count += 1
            if count == a[num - 1]:
                num += 1
                count = 0

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

Solution (Python):
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')

H, W =LI()
N = I()
a = LI()
c = [[0]*W for i in range(H)]
k = int(0)
cur = int(0)
kin = int(0)
for i,anum in enumerate(a):

    for j in range(anum):

        if cur%2==0 and (k+j)==W:
            cur +=1
            k = j-1
        if cur%2==1 and (W-j+k)==-1:
            cur +=1
            k = -j
        if cur%2==0:
            c[cur][k+j] = i+1
        else:
            c[cur][W-j+k] = i+1

    if cur % 2 == 0:
        k = k+j+1
    else:
        k = -j+k-1

for v in c:
    print(" ".join(map(str,v)))

Solution (Python):
import numpy as np
H,W = [int(x) for x in input().split()]
N = int(input())
a = [int(x) for x in input().split()]
ans = np.array([1]*a[0])
for i in range(1,N):
  
  ans=np.r_[ans,[i+1]*a[i]]
ans = np.reshape(ans,(H,W))
for i in range(0,H,2):
  ans[i,:]=ans[i,::-1]
for i in range(H):
  print(*ans[i,:])

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

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

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

for i in range(H):
    row = b[i * W:(i + 1) * W]
    if i % 2 == 1:
        row.reverse()
    print(' '.join([str(c) for c in row]))

Solution (Python):

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

a_list = [int(e) for e in input().split()]

paint_list = []

for i in range(N):
    paint_list += [i+1 for _ in range(a_list[i])]   


ans = []
for i in range(H+2):
    if i==0 or i==H+1:
        ans.append([-1 for _ in range(W+2)])
    else:
        line = [0 for _ in range(W+2)]
        line[0]=-1
        line[W+1]=-1
        ans.append(line)


x = 1
y = 1
direction = 0

def print_ans():
    for i in range(1,H+1):
        print(' '.join(map(str, ans[i][1:W+1])))

for color in paint_list:
    ans[y][x] = color
    
    
    
    if direction%4 == 0:
        if ans[y+1][x]==0:
            y += 1
        else:
            direction += 1
            x += 1
    elif direction%4 == 1:
        if ans[y][x+1]==0:
            x += 1
        else:
            direction += 1
            y -= 1
    elif direction%4 == 2:
        if ans[y-1][x]==0:
            y -= 1
        else:
            direction += 1
            x -= 1
    elif direction%4 == 3:
        if ans[y][x-1]==0:
            x -= 1
        else:
            direction += 1
            y += 1

print_ans()

Solution (Python):
import sys
input = sys.stdin.readline
H, W = map(int, input().split())
N = int(input())
a = list(map(int, input().split()))
c = [[0] * W for _ in range(H)]
tour = [[0] * W for _ in range(H)]
s = []
s.append([0, 0])
ss = []
ss.append([0, 0])
tour[0][0] = 1
t = 0
counter = 1
while counter < H * W:
  counter += 1
  p = s.pop()
  n = [-1, -1]
  while not (0 <= n[0] < H and 0 <= n[1] < W and tour[n[0]][n[1]] == 0):
    if t == 0:
      n = [p[0] + 1, p[1]]
    elif t == 1:
      n = [p[0], p[1] + 1]
    elif t == 2:
      n = [p[0] - 1, p[1]]
    else:
      n = [p[0], p[1] - 1]
    if 0 <= n[0] < H and 0 <= n[1] < W and tour[n[0]][n[1]] == 0:
      break
    else:
      t = (t + 1) % 4
  s.append(n)
  tour[n[0]][n[1]] = tour[p[0]][p[1]] + 1
  ss.append(n)

for i in range(N):
  for _ in range(a[i]):
    p = ss.pop()
    
    c[p[0]][p[1]] = i + 1
for i in range(H):
  print(*c[i])

Solution (Python):
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
import math
import queue


Mod = 1000000007
 
  
def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))+1):
        if prime[i] == 1:
            for j in range(2*i,n+1):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n+1):
        if prime[i] == 1:
            res.append(i)
    return res

def factorial(i):
    if i == 1:
        return 1
    else:
        return i*factorial(i-1)
 
class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py
 
    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)




def main(): 
    H,W = map(int,input().split())
    N = int(input())
    a = list(map(int,input().split()))
    p = [[]*W for i in range(H)]
    psub = []
    for i in range(N):
        for j in range(a[i]):
            psub.append(i+1)
    for i in range(H):
        for j in range(W):
            p[i].append(psub[i*W+j])
        if i % 2 != 0:
            p[i] = p[i][::-1]
    for i in range(H):
        print(*p[i],sep = " ")


if __name__ == "__main__":
    main()

Solution (Python):
h, w = map(int, input().split())
n = int(input())
amount = list(map(int, input().split()))
result = [[0 for i in range(w)] for j in range(h)]
down = True
total = 0
curr_col = 0
curr_index = 0
curr_color = 1
while total < w*h:
    if down:
        while curr_index < h and amount[curr_color - 1] > 0:
            result[curr_index][curr_col] = curr_color
            curr_index += 1
            amount[curr_color - 1] -= 1
            total += 1
        if curr_index == h:
            down = False
            curr_col += 1
            curr_index -= 1
        if amount[curr_color - 1] == 0:
            curr_color += 1
    else:
        while curr_index >= 0 and amount[curr_color - 1] > 0:
            result[curr_index][curr_col] = curr_color
            curr_index -= 1
            amount[curr_color - 1] -= 1
            total += 1
        if curr_index == -1:
            down = True
            curr_col += 1
            curr_index = 0
        if amount[curr_color - 1] == 0:
            curr_color += 1
for row in result:
    print(*row)

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

c=[[0 for j in range(W)] for i in range(H)] 
cnt=0
for i,ai in enumerate(a):
    for t in range(ai):
        c[cnt//W][cnt%W]=i+1
        cnt+=1
for i in range(H):
    if i%2==1:
        c[i].reverse()

for ci in c:
    print(*ci)

Solution (Python):

from collections.abc import Iterable


def flatten(A: "Array") -> "Array":
    if isinstance(A, Iterable):
        return [i for a in A for i in flatten(a)]
    else:
        return [A]


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 = flatten([color] * cnt for color, cnt in enumerate(A, 1))
    for i, g in enumerate(zip(*[iter(grid)] * W)):
        if i & 1 == 0:
            print(*g)
        else:
            print(*g[::-1])


if __name__ == "__main__":
    main()

Solution (Python):
n,m=map(int,input().split())
x=int(input())
l=[int(i) for i in input().split()]
mat=[[0 for j in range(m)]for i in range(n)]
curr=1 
c=0 
j1=0
for i in range(n): 
    for j in range(m):
        mat[i][j]=curr 
        c+=1 
        if c==l[j1]: 
            curr+=1 
            j1+=1
            c=0 
for i in range(n):
    if i&1:
        mat[i]=mat[i][::-1]
for i in range(n):
    print(*mat[i])

Solution (Python):

import sys


def solve(H: int, W: int, N: int, a: "List[int]"):
    L = []
    for i, aa in enumerate(a):
        L = L + [i+1] * aa
    R = []
    for i in range(H):
        R.append(L[i*W:(i+1)*W])
    for i in range(H):
        if i % 2 == 0:
            K = R[i]
        else:
            K = list(reversed(R[i]))
        print(" ".join([str(s) for s in K]))
    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()))

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

memo = 0
for i in range(len(a)):
    for k in range(a[i]):
        ans[memo//w][memo%w] = i+1
        memo += 1

for i in range(1,len(ans),2):
    ans[i].reverse()


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

B = []

for i, a in enumerate(A):
    for _ in range(a):
        B.append(i+1)

M = []
for _ in range(H):
    M.append([0] * W)

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

    print(*M[h])

Solution (Python):
import numpy as np

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

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

arr = np.array(g)
arr = arr.reshape([h,w])

ans = []
for i in range(h):
    tmp = list(arr[i])
    if i%2 != 0:
        tmp.reverse()
    ans.append(tmp)

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

ans=[[0]*W for _ in range(H)]
x=0
y=0
dir=1
for a in range(1,N+1):
  for _ in range(A[a-1]):
    ans[y][x]=a
    if dir==1 and x==W-1:
      y+=1
      dir=-1
    elif dir==1:
      x+=1
    elif dir==-1 and x==0:
      y+=1
      dir=1
    elif dir==-1:
      x-=1
      
for i in ans:
  print(*i)

Solution (Python):
h, w = map(int, input().split())
n = int(input())
list_a = list(map(int, input().split()))
result = []

row = []
for i in range(n):
  temp_a = list_a[i]
  for _ in range(temp_a):
    row.append(i+1)
    if len(row) == w:
      if len(result) % 2 != 0:
        row = row[::-1]
      result.append(row)
      row = []

for one_row in result:
  print(*one_row)

Solution (Python):
h, w = map(int, input().split())
n = int(input())
list_a = list(map(int, input().split()))
result = []

row = []
for i in range(n):
  temp_a = list_a[i]
  for _ in range(temp_a):
    row.append(i+1)
    if len(row) == w:
      if len(result) % 2 != 0:
        row = row[::-1]
      result.append(row)
      row = []

for one_row in result:
  temp_str = ""
  for one_num in one_row:
    temp_str += str(one_num) + " "
  print(temp_str[:-1])

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

arr = list()
for i, a in enumerate(A):
    for j in range(a):
        arr.append(i + 1)
for i in range(H):
    sub = arr[i * W: (i + 1) * W]
    if i % 2 == 1:
        sub = list(reversed(sub))
    print(' '.join([str(x) for x in sub]))

Solution (Python):
def put(i):
    global n,row,cur
    while n>0:
        row[cur[0]] = i
        n -= 1
        if cur[1]=="R":
            cur[0] += 1
        elif cur[1]=="L":
            cur[0] -= 1
        if cur[0]>W-1 or cur[0]<0:
            B.append(row)
            row = ["." for _ in range(W)]
        if cur==[-1,"L"]:
            cur = [0,"R"]
        elif cur==[W,"R"]:
            cur = [W-1,"L"]
        
H,W = map(int,input().split())
N = int(input())
A = list(map(int,input().split()))
A.insert(0,0)
B = []
row = ["." for _ in range(W)]
cur = [0,"R"]
for i in range(1,N+1):
    n = A[i]
    put(i)
    
for i in range(H):
    print(*B[i])

Solution (Python):
H,W=map(int,input().split())
N=int(input())
a=tuple(map(int,input().split()))
l=[]
for i in range(N):
    l+=[i+1]*a[i]
for i in range(H//2):
    for j in range(W):
        print(l[2*i*W+j],end=" ")
    print()
    for j in range(W):
        print(l[2*(i+1)*W-j-1],end=" ")
    print()
if H%2:
    for i in l[(H-1)*W:]:
        print(i,end=" ")

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):
from collections import deque
H, W = map(int, input().split())
N = int(input())
a = list(map(int, input().split()))
queue = deque([])
ans = [[0] * W for _ in range(H)]

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

x, y = 0, 0
direction = True
while queue:
    ans[x][y] = queue.popleft()
    if direction:
        if 0 <= y < W - 1:
            y += 1
        else:
            x += 1
            direction = False
    else:
        if 0 < y <= W - 1:
            y -= 1
        else:
            x += 1
            direction = True

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

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

color = 1
for i in range(h):
  for j in range(w):
    if A[color-1] == 0:
      color += 1
    C[i][j] = color
    A[color-1] -= 1
    
for i in range(h):
  if i%2 == 0:
    print(*C[i])
  else:
    print(*reversed(C[i]))

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

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

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

B = []
cnt = 1
for a in A:
  for i in range(a):
    B.append(cnt)
  cnt += 1
B = np.array(B)
B = np.ndarray.reshape(B,[H,W])
for i in range(H):
  for j in range(W):

    print (B[i][j] if i % 2 == 0 else B[i][W-j-1], end = " ")
  print ()

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

Solution (Python):
import numpy as np
h, w = map(int, input().split())
n = int(input())
x = list(map(int, input().split()))

line = []
for i in range(n):
    for j in range(x[i]):
        line.append(i+1)

aa = np.array(line).reshape(h, w)
for i in range(h):
    if i % 2 == 1:
        aa[i, :] = aa[i, :][::-1]

for i in range(h):
    print(' '.join([str(_) for _ in aa[i]]))

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

Solution (Python):
height, wide = map(int, input().split())
color = int(input())
colorNum = list(map(int, input().split()))
ans = [0] * wide
heightCheck = "even"
outputCount = 0
for i in range(color):
  for j in range(colorNum[i]):
    if heightCheck == "even":
      ans[outputCount] = i + 1
      outputCount += 1
    else:
      ans[outputCount] = i + 1
      outputCount -= 1
    if outputCount == wide:
      for k in range(wide):
        if k != wide - 1:
          print(str(ans[k]) + " ", end = "")
        else:
          print(ans[k])
      heightCheck = "odd"
      outputCount -= 1
    elif outputCount == -1:
      for k in range(wide):
        if k != wide - 1:
          print(str(ans[k]) + " ", end = "")
        else:
          print(ans[k])
      heightCheck = "even"
      outputCount = 0

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)

M = [[] for _ in range(H)]

idx = 0
for i in range(H):
    for j in range(W):
        M[i].append(L[idx])
        if i%2 == 1:
            M[i].sort(reverse=True)
        idx += 1

for m in M:
    print(*m)

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


z=0
for i in range(h):
    l=c[i*w:i*w+w]
    if z==0:
        print(" ".join(map(str,l)))
        z=1
    elif z==1:
        print(" ".join(map(str,l[::-1])))
        z=0

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

uselists=[]
anslist=[]
for i in range(n):
    uselists.extend([i+1]*lists[i])

for i in range(0,h):
    if i%2==0:
        anslist.append(uselists[w*i:w*(i+1)])

    if i%2!=0:
        anslist.append(uselists[w*(i+1)-1:w*i-1:-1])
for some in anslist:
    print(*some)

Solution (Python):
z=list(map(int,input().split()))
n=int(input())
a=list(map(int,input().split()))
b,c,d,e=[],[],0,0
while True:
    c.append(d+1)
    e+=1
    if e==a[d]:
        e=0
        d+=1
    if len(c)==z[1]:
        b.append(c)
        c=[]
    if d==n:
        break
for i in range(z[0]):
    if i%2==0:
        for j in range(z[1]):
            print(b[i][j],end=' ')
    else:
        for j in range(z[1]):
            print(b[i][-j-1],end=' ')
    print('')

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

def main():
    _,W = map(int,input().split())
    N = int(input())
    A = []
    a = list(map(int, input().split()))
    for i in range(N):
        A.append((i+1, a[i]))
    A.sort(key=lambda x: x[1])

    x = 0
    y = 0
    s = ""
    for c,n in A:
        for i in range(n):
            if y%2 == 0:
                if s == "":
                    s = str(c)
                else:
                    s = s + " " + str(c)
            else:
                s = str(c) + " " + s
            x += 1
            if x == W:
                print(s)
                y += 1
                x = 0
                s = ""
    
if __name__ == '__main__':
    main()

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

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

tmp = 0
for i in range(H):
    if i % 2 == 0:
        for j in range(W):
            graph[i][j] = tmp + 1
            a[tmp] -= 1
            if a[tmp] == 0:
                tmp += 1
    if i % 2 == 1:
        for j in range(W - 1, -1, -1):
            graph[i][j] = tmp + 1
            a[tmp] -= 1
            if a[tmp] == 0:
                tmp += 1

for i in graph:
    print (*i)

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

graph = [[-1] * W for _ in range(H)]
h, w = 0, 0 

if H == W == 1:
    print (1)
    exit()


right = W - 1
left = 0
up = 0
down = H - 1

tmp = 0
count = 1
flag = True
while count <= H * W:
    while w < right and flag:
        
        graph[h][w] = tmp + 1
        a[tmp] -= 1
        if a[tmp] == 0:
            tmp += 1
        count += 1
        if count > H * W:
            flag = False
            break
        w += 1
    right -= 1
    while h < down and flag:
        
        graph[h][w] = tmp + 1
        a[tmp] -= 1
        if a[tmp] == 0:
            tmp += 1
        count += 1
        if count > H * W:
            flag = False
            break
        h += 1
    down -= 1
    while left < w and flag:
        
        graph[h][w] = tmp + 1
        a[tmp] -= 1
        if a[tmp] == 0:
            tmp += 1
        count += 1
        if count > H * W:
            flag = False
            break
        w -= 1
    left += 1
    while up < h and flag:
        
        graph[h][w] = tmp + 1
        a[tmp] -= 1
        if a[tmp] == 0:
            tmp += 1
        count += 1
        if count > H * W:
            flag = False
            break
        h -= 1
    up += 1
    h += 1
    w += 1
    
    
    
    
    
    if right == left == 1:
        right += 1
    if up == down == 1:
        down += 1
    if right == left and up == down:
        right += 1

for i in graph:
    print (*i)

Solution (Python):

import sys


def solve(H: int, W: int, N: int, a: "List[int]"):
    
    al = [[i]*a[i-1] for i in range(1,N+1)]

    import itertools
    al = list(itertools.chain.from_iterable(al))

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

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

    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()))
ans = [[0 for x in range(w)] for y in range(h)]

def getx(i):
    return i // h
def gety(i):
    if (i % (2 * h)) < h:
        return i % h
    else:
        return h - i % h - 1

aa=[]
for i in range(n):
    aa += [i + 1 for _ in range(a[i])]

for i in range(h * w):
    ans[gety(i)][getx(i)] = aa[i]

for y in range(h):
    for x in range(w):
        if x > 0: print(' ', end='')
        print(ans[y][x], end='')
    print()

Solution (Python):

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

N = int(input())

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


c = []


Wind = 0
nlis = []
for ind,num in enumerate(a):

    ind += 1

    for j in range(num):

        nlis.append(ind)

        if len(nlis) == W:

            if Wind % 2 == 1:
                nlis.reverse()

            Wind += 1
            c.append(nlis)
            nlis = []

for i in c:
    print (" ".join(map(str,i)))

Solution (Python):
inf = float('inf')

h, w = map(int, input().split())
n = int(input())
li = list(map(int, input().split()))
fi = [[inf for _ in range(w)] for _ in range(h)]

x = 0
y = 0

for color, count in enumerate(li):
     color = color + 1
     while count != 0:
        fi[x][y] = color
        count -= 1

        if x % 2 == 0:
            nexty = y+1
        else:
            nexty = y-1
        
        if nexty == -1 or nexty == w:
            x = x+1
        elif fi[x][nexty] == inf:
            y = nexty

for row in fi:
    print(*row)

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

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

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

for i in range(H):
    print(' '.join(c[i*W : (i+1)*W][::(i%2)*2-1]))

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

index = 0
total = 0
count = 0
a = ls[index]
res = [[0] * w for _ in range(h)]
for y in range(h):
    for x in range(w):
        if y % 2 == 1:
            x = w - 1 - x
        res[y][x] = index + 1
        count += 1
        total += 1
        if total < h * w and count >= a:
            index += 1
            a = ls[index]
            count = 0
for row in res:
    print(*row)

Solution (Python):

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

import numpy as np
l = []

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

l = np.array(l)
l = l.reshape(H, W)

for i in range(0, H):
    if i % 2 == 0:
        print(' '.join(map(str, l[i].tolist())))
    else:
        print(' '.join(map(str, l[i][::-1].tolist())))

Solution (Python):
def resolve():
    h, w = map(int, input().split())
    n = int(input())
    a = list(map(int, input().split()))
    colors = []
    for i, ai in enumerate(a):
        colors += [str(i + 1)] * ai
    for i in range(h):
        if i % 2 == 1:
            print(" ".join(colors[w * i:w * (i + 1)][::-1]))
        else:
            print(" ".join(colors[w * i:w * (i + 1)]))


if __name__ == "__main__":
    resolve()

Solution (Python):
def resolve():
    h, w = map(int, input().split())
    n = int(input())
    a = list(map(int, input().split()))
    colors = []
    for i, ai in enumerate(a):
        colors += [i + 1] * ai
    ans = []
    for i in range(h):
        if i % 2 == 1:
            ans.append(colors[w * i:w * (i + 1)][::-1])
        else:
            ans.append(colors[w * i:w * (i + 1)])
    for row in ans:
        print(" ".join([str(e) for e in row]))


if __name__ == "__main__":
    resolve()

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

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

h, w = 0, 0
for n in range(N):
    while A[n] > 0:
        A[n] -= 1
        grid[h][w] = n + 1
        if h % 2 == 0:
            w += 1
            if w == W:
                h += 1
                w = W - 1
        elif h % 2 == 1:
            w -= 1
            if w == -1:
                h += 1
                w = 0
for item in grid:
    print(*item)

Solution (Python):

h, w = map(int, input().split())
n = int(input())
arr = list(map(int, input().split()))
line = []
for i in range(n):
    line.extend([str(i + 1)] * arr[i])

r = 0
for y in range(h):
    output = line[r : r + w]
    r += w
    if y % 2 == 1:
        print(" ".join(output[::-1]))
    else:
        print(" ".join(output))

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

k = 1
for j in range(H):
    row = []
    for i in range(W):
        row.append(str(k))
        A[k - 1] -= 1

        if A[k - 1] == 0:
            k += 1

    print(' '.join(row[::-1 if j % 2 else 1]))

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

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

ans=[]
for i in range(H):
    l=W*i
    r=l+W
    tmp=grid[l:r]
    if i%2==0:
        ans.append(tmp)
    else:
        ans.append(tmp[::-1])

for i in ans:
    print(*i, sep=" ")

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

queue = []
for i in range(N):
  queue += [i + 1] * a[i]
  
for y in range(H):
  print(*queue[y * 2 * W:(y * 2 + 1) * W], sep=' ')
  if (y * 2 + 1 >= H):
    break
  print(*reversed(queue[(y * 2 + 1) * W:(y * 2 + 2) * W]), sep=' ')