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



Sample input:
2 2
3
2 1 1


Sample output:
1 1
2 3

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.buffer.readline

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode()

def main():
	h,w=MI()
	n=II()
	A=LI()

	G=[]

	for i,v in enumerate(A):
		G+=[i+1]*v

	GG=[]

	for i in range(h):
		if i%2==0:
			GG.append(G[i*w:i*w+w])
		else:
			GG.append(G[i*w:i*w+w][::-1])

	for a in GG:
		print(*a)














if __name__ == "__main__":
	main()

Submission status: Accepted

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

now = a.pop()
now_i = N
for i in range(H):
    if i % 2 == 0:
        for j in range(W):
            if now == 0:
                now = a.pop()
                now_i -= 1
            ans[i][j] = str(now_i)
            now -= 1
    else:
        for j in range(W-1, -1, -1):
            if now == 0:
                now = a.pop()
                now_i -= 1
            ans[i][j] = str(now_i)
            now -= 1
for i in range(H):
    print(" ".join(ans[i]))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
n=int(input())
a=list(map(int,input().split()))
ans=[[]for i in range(h)]
t=0
tmp=0
for i in range(n):
    for j in range(a[i]):
        if tmp!=w:
            ans[t].append(i+1)
            tmp+=1
            if tmp==w:
                tmp=0
                t+=1
flg=True
for i in range(h):
    if flg:
        print(*ans[i])
        flg=False
    elif flg==False:
        print(*ans[i][::-1])
        flg=True

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_nl = lambda: list(map(int, readline().split()))
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')


def main():
    H, W = in_nn()
    _ = in_n()
    a = in_nl()
    a = [(i + 1, t) for i, t in enumerate(a)]

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

    for y in range(H):
        for x in range(W):
            if y % 2 == 1:
                x = W - x - 1
            c, num = a[-1]
            ans[y][x] = c
            if num - 1 == 0:
                a.pop(-1)
            else:
                a[-1] = (c, num - 1)

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
n = int(input())
alst = list(map(int, input().split()))

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

num = 0
apos = 0
for pos in range(h * w):
    x = pos // w
    y = pos % w
    if x % 2 == 1:
        y = w - y - 1
    c[x][y] = apos + 1
    num += 1
    if alst[apos] == num:
        apos += 1
        num = 0
for lst in c:
    print(*lst)

Submission status: Accepted

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

c = []
for n, a in enumerate(A, 1):
  c += [n]*a

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

Submission status: Accepted

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

c = [0]*W
i, j = 0, 0
for n, a in enumerate(A, 1):
  for _ in range(a):
    c[j] = n
    if (i % 2 == 0) and (j != W-1):
      j += 1
    elif (i % 2 == 1) and (j != 0):
      j -= 1
    else:
      i += 1
    if c[0] != 0 and c[W-1] != 0:
      print(*c)
      c = [0]*W

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
n = int(input())
A = list(map(int,input().split()))
C = [[0]*w for _ in range(h)]
idx = 0
for _h in range(h):
    if _h%2==0:
        loop = list(range(w))
    else:
        loop = list(range(w-1,-1,-1))
    for _w in loop:
            C[_h][_w] = idx+1
            A[idx] -= 1
            if A[idx] == 0:
                idx += 1
for c in C:
    print(" ".join([str(_c) for _c in c]))

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 
#mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

H, W = MAP()
N = INT()
a = LIST()

cnt = 0
ans = [[-1]*W for _ in range(H)]
tmp = a.pop(0)
color = 1
flg = 0

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



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

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 
#mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

H, W = MAP()
N = INT()
a = LIST()

cnt = 0
ans = [[-1]*W for _ in range(H)]
tmp = a.pop(0)
color = 1
flg = 0

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



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

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 
#mod = 998244353
from decimal import *
import numpy as np
#decimal.getcontext().prec = 10

H, W = MAP()
N = INT()
a = LIST()

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

ans = []
for i in range(H):
	ans.append(A[i*W:(i+1)*W])

for i in range(H):
	if i%2:
		ans[i] = ans[i][::-1]

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

Submission status: Accepted

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

l=list(itertools.chain.from_iterable([[i+1]*a[i] for i in range(n)]))
c=0
for i in range(h):
    if c%2==0:
      print(*l[i*w:(i+1)*w])
    else:
      print(*(l[(i+1)*w-1:i*w-1:-1]))
    c+=1

Submission status: Accepted

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

for i,v in enumerate(A):
    check += [i + 1] * v

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

Submission status: Accepted

Solution code:
def readinput():
    h,w=list(map(int,input().split()))
    n=int(input())
    a=list(map(int,input().split()))
    return h,w,n,a

def main(h,w,n,a):
    cmat=[]
    for _ in range(h):
        cmat.append([0]*w)
    i=0
    j=0
    jstep=1
    for c in range(n):
        k=a[c]
        while k>0: 
            cmat[i][j]=c+1
            if (j==0 and jstep==-1) or (j==w-1 and jstep==1):
                i=i+1
                jstep=jstep*(-1)
            else:
                j=j+jstep
            k-=1
    return cmat

if __name__=='__main__':
    h,w,n,a=readinput()
    ans=main(h,w,n,a)
    for i in range(h):
        print(' '.join(map(str,ans[i])))

Submission status: Accepted

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

N = int(input())

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

res = []
j = 0
st = 0 # リスト添え字管理

for i in range(H):
    j = 0
    while j < W:
        while 0 < a[st] and j < W:
            res.append(str(st+1))
            j += 1
            a[st] -= 1
        if a[st] == 0:
            st += 1
    if i % 2 == 1:
        res.reverse()
    print(' '.join(res))
    res = []

Submission status: Accepted

Solution code:
h,w,n,*a = map(int,open(0).read().split())
b = []
for i in range(len(a)):
  b += [i+1]*a[i]
for i in range(h):
  if i % 2 == 0:
    print(*b[i*w:(i+1)*w])
  else:
    print(*b[i*w:(i+1)*w][::-1])

Submission status: Accepted

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

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

num = 1
cnt = A[0]
for i in range(H):
    for j in range(W):
        if cnt == 0:
            num += 1
            cnt = A[num-1]
        if i % 2 == 0:
            Ans[i][j] = num
            cnt -= 1
        else:
            k = W - j - 1
            Ans[i][k] = num
            cnt -= 1

for A in Ans:
    for i, a in enumerate(A):
        if i == W - 1:
            print(a)
        else:
            print(a, end=' ')

Submission status: Accepted

Solution code:
import math

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

res = [[0]*W for i in range(H)]
cur = [0, 0]

for i, ai in enumerate(a):
    while ai > 0:
        res[cur[0]][cur[1]] = (i + 1)
        if cur[1] % 2 == 0:
            if cur[0] == (H-1):
                cur[1] += 1
            else:
                cur[0] += 1
        else:
            if cur[0] == 0:
                cur[1] += 1
            else:
                cur[0] -= 1
        ai -= 1

str_n = int(math.log10(N)) + 2

for i in range(H):
    for j in range(W):
        print(str(res[i][j]).ljust(str_n), end="")
    print("")

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
n = int(input())
A = list(map(int, input().split()))
grid = []
for i, num in enumerate(A, 1):
    grid += [i]*num


def split_list(l, n):
    for i in range(0, len(l), n):
        yield l[i:i+n]


ans = list(split_list(grid, w))
for i in range(h):
    if i % 2 == 0:
        print(*ans[i])
    else:
        print(*ans[i][::-1])

Submission status: Accepted

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

from collections import deque

d=deque([])
for i in range(n):
    number=i+1
    cnt=a[i]
    for _ in range(cnt):
        d.append(number)

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

flg_h=0
i=0
j=0
while d:
    v=d.popleft()
    ans[i][j]=v
    if flg_h==0:
        if i<h-1:
            i+=1
        else:
            j+=1
            flg_h=1
    elif flg_h==1:
        if i>0:
            i-=1
        else:
            j+=1
            flg_h=0

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

Submission status: Accepted

Solution code:
from collections import deque

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

tmp = 0
rlt = []
h = deque([])

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

for k,i in enumerate(As):
  for _ in range(i):
    tmp += 1
    if ((tmp - 1) // W) % 2 == 0:
      h.append(k+1)
    else:
      h.appendleft(k+1)
    if tmp % W == 0:
      rlt.append(h)
      h = deque([])

for q in rlt:
  print(" ".join(map(str, q)))

Submission status: Accepted

Solution code:
H,W=list(map(int,input().split()))
N=int(input())
data=list(map(int,input().split()))
ans=[[0]*W for i in range(H)]
n_w,n_h=0,0
color=1
dire=1
for i in data:
   while i>0:
      if dire==1:
         i-=1
         ans[n_h][n_w]=color
         if n_w==W-1:
            dire=-1
            n_h+=1
         else:
            ans[n_h][n_w]=color
            n_w+=1
      else:
         i-=1
         ans[n_h][n_w]=color
         if n_w==0:
            dire=1
            n_h+=1
         else:
            ans[n_h][n_w]=color
            n_w-=1
   color+=1
for i in ans:
   print(" ".join(map(str,i)))

Submission status: Accepted

Solution code:
h, w, n, *aa = map(int, open(0).read().split())

line = list()

order = False

for a, i in zip(aa, range(1, len(aa)+1)):
    if order:
        line = line + [i]*a
    else:
        line = [i]*a + line
        
    while len(line) >= w:
#         print(line, w)
        if order:
            p = line[:w]
            line = line[w:]
        else:
            p = line[-w:]
            line = line[:-w]
        order = not order
        print(*p)

Submission status: Accepted

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

color = []
for i in range(n):
    color += [i+1]*a[i]
    
#print(color)

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

now = 0
for row in range(h):
    for col in range(w):
        if row%2 == 0:
            ans[row][col] = color[now]
        else:
            ans[row][-(col+1)] = color[now]
        now += 1
        
for r in ans:
    print(*r)

Submission status: Accepted

Solution code:
def main():
    # input
    H, W = map(int, input().split())
    _ = input()
    A = tuple(map(int, input().split()))

    # preprocess
    color_seq = [0] * (H*W)
    now = 0
    for i, num in enumerate(A):
        for j in range(num):
            color_seq[now+j] = i+1
        now += num

    # zig-zag coloring
    field = [[0]*W for _ in range(H)]
    for i in range(H):
        if i%2 == 0:
            for j in range(W):
                field[i][j] = color_seq[W*i + j]
        else:
            for j in range(W):
                field[i][W-1-j] = color_seq[W*i + j]
    
    # output
    for i in range(H):
        print(*field[i], sep=" ")

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
from collections import deque

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

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

res = [[-1] * W for _ in range(H)]
for i in range(H):
    if i % 2 == 0:
        for j in range(W):
            b = B.popleft()
            res[i][j] = b
    else:
        for j in reversed(range(W)):
            b = B.popleft()
            res[i][j] = b

for row in res:
    print(*row)

Submission status: Accepted

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

A = [0] + A


from collections import deque

i = 1
for h in range(H):
    C = deque([])
    for w in range(W):
        if h%2 == 0:
            C.append(str(i))
        else:
            C.appendleft(str(i))
        A[i] -= 1
        if A[i] == 0:
            i += 1
            
    print(' '.join(C))

Submission status: Accepted

Solution code:
from collections import deque
h,w = map(int, input().split())
n = int(input())
A = list(map(int, input().split()))

ans = deque()
for i,a in zip(list(range(1,n+1)), A):
    for _ in range(a):
        ans.append(i)
check = 0
for i in range(h):
    a = []
    for _ in range(w):
      a.append(ans.popleft())
    if check%2==0: print(" ".join(map(str,a)))
    else:print(" ".join(map(str,a[::-1])))
    check += 1

Submission status: Accepted

Solution code:
from collections import deque
from itertools import accumulate
from bisect import bisect_left
H, W = map(int, input().split())
N = int(input())
a = [int(i) for i in input().split()]
m = list(accumulate(a))

grid = []
for i in range(H+2):
    if i == 0 or i == H+1:
        grid.append('#'*(W+2))
    else:
        grid.append('#' + '.'*W + '#')
ans = [[0 for i in range(W+2)] for j in range(H+2)]
st = deque([])
st.append((1,1))
visited = [[0 for i in range(W+2)] for j in range(H+2)]
cur = 0
visited[1][1] = 1
while st:
    x, y = st.pop()
    # 色を塗る操作
    cur += 1
    ans[y][x] = cur
    if grid[y+1][x] == '.'and visited[y+1][x] == 0:
        st.append((x, y+1))
        visited[y+1][x] = 1
    elif grid[y][x+1] == '.'and visited[y][x+1] == 0:
        st.append((x+1, y))
        visited[y][x+1] = 1
    elif grid[y-1][x] == '.'and visited[y-1][x] == 0:
        st.append((x, y-1))
        visited[y-1][x] = 1
    elif grid[y][x-1] == '.'and visited[y][x-1] == 0:
        st.append((x-1, y))
        visited[y][x-1] = 1
cur = 1
a = [[0 for i in range(W)] for j in range(H)]
for i in range(1,H+1):
    ls = []
    for j, x in enumerate(ans[i][1:-1], 0):
        a[i-1][j] = bisect_left(m, x)+1
for i in range(H):
    print(*a[i])

Submission status: Accepted

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

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

for ans_i in ans:
    print(*ans_i)

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
n=int(input())
a=list(map(int,input().split()))
colors=[]
for i in range(n):
    for _ in range(a[i]):
        colors.append(i+1)
ans=[[0 for _ in range(w)] for _ in range(h)]
temp=0
for i in range(h):
    for j in range(w):
        if i%2!=0:
            j = w-j-1
        ans[i][j]=colors[temp]
        temp+=1

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

Submission status: Accepted

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

ans_list = []
ans = []

for i,j in enumerate(a):
    tmp = [str(i+1)] * j
    ans_list.extend(tmp)

for _ in range(H//2):
    ans.append(ans_list[:W])
    ans_list = ans_list[W:]
    ans.append(list(reversed(ans_list[:W])))
    ans_list = ans_list[W:]

if H % 2 == 1:
    ans.append(ans_list)

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

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
n = int(input())
arr = [0] + list(map(int, input().split()))

res = []
for i in range(1, n+1):
    res.extend([i] * arr[i])

tmp = 0
for i in range(h):
    s = res[tmp:tmp+w]
    if i % 2 == 1:
        s.reverse()
    print(*s)
    tmp += w

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

def main():
    H, W = map(int, input().split())
    N = int(input())
    a = list(map(int, input().split()))
    ans = [[None]*W for _ in range(H)]
    now = 0
    for i in range(N):
        while a[i] > 0:
            # print(now//W, now%W)
            ans[now//W][now%W]=(i+1)
            now += 1
            a[i] -= 1
    for i in range(H):
        if i % 2 == 1:
            print(*ans[i][::-1])
        else:
            print(*ans[i])

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
n = int(input())
color = list(map(int, input().split()))
colors = []
for c, num in enumerate(color):
    colors += [c+1]*num
mat = [[0]*w for _ in range(h)]
color_idx = 0

for row in range(h):
    if row %2 == 0:
        for col in range(w):
            mat[row][col] = colors[color_idx]
            color_idx += 1
    else:
        for col in range(w-1, -1, -1):
            mat[row][col] = colors[color_idx]
            color_idx += 1

for row in mat:
    print(' '.join(map(str, row)))

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
N = int(input())
A = list(map(int, input().split()))
colors = []
for i in range(1, N + 1):
    colors += [i] * A[i - 1]
for i in range(H):
    if i % 2 == 0:
        print(*colors[i * W : (i + 1) * W])
    else:
        print(*colors[i * W : (i + 1) * W][::-1])

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

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

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

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

Submission status: Accepted

Solution code:
H, W = map(int,input().split())
N = int(input())
al = list(map(int,input().split()))

ans = []
num = 1
for i in range(N):
    for _ in range(al[i]):
        ans.append(i+1)
#print(*ans)

left = 0

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

Submission status: Accepted

Solution code:
from collections import deque
h,w = map(int,input().split())
n = int(input())
A = list(map(int,input().split()))
dx = [0,1,0,-1]
dy = [1,0,-1,0]
ans = [["." for _ in range(w)]for _ in range(h)]
dq = deque([(0,0)])
i = 0
cnt = 0
while i != n:
    p,q = dq.pop()
    ans[q][p] = str(i+1)
    cnt += 1
    for j in range(4):
        nx = p + dx[j]
        ny = q + dy[j]
        if 0<=nx<=w-1 and 0<=ny<=h-1 and ans[ny][nx] == ".":
            dq.append((nx,ny))
            break
    if cnt == A[i]:
        cnt = 0
        i += 1
for i in ans:
    print(" ".join(i))

Submission status: Accepted

Solution code:
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)
for i in range(h):
 c=l[i*w:i*w+w]
 if i%2==0:
  print(*c)
 else:
  print(*c[::-1])

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
n = int(input())
a = (list(map(int,input().split())))
l = []
for i in range(n):
    s = i+1
    t = a[i]
    #s:色　t:その色の個数
    for j in range(t):
        l.append(s)
for i in range(h):
    a = l[w*i:w*i+w]
    if i%2==0:
        print(*a)
    else:
        print(*a[::-1])

Submission status: Accepted

Solution code:
def solve():
    H,W = map(int, input().split())
    N = int(input())
    A = map(int, input().split())
    i,j = 0,0
    delta = 1
    ret = [[0]*W for _ in range(H)]
    for k,a in enumerate(A):
        while a > 0:
            ret[i][j] = k+1
            a -= 1
            if 0 <= j+delta < W:
                j += delta
            else:
                i += 1
                delta = delta*(-1)
                
    print('\n'.join([' '.join(map(str, r)) for r in ret]))
    
solve()

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


def update_coord(H, W, i, j):
    # --->
    if i % 2 == 0:
        if j == W - 1:
            i += 1
        else:
            j += 1
    # <---
    else:
        if j == 0:
            i += 1
        else:
            j -= 1
    return i, j


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

    ans = [[0] * W for _ in range(H)]
    i, j = 0, 0
    for k, aa in enumerate(a):
        for _ in range(aa):
            ans[i][j] = k + 1
            i, j = update_coord(H, W, i, j)

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import itertools as it

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

N = int(input())

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

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

h = 0
w = 0
rotation = it.cycle([(0, 1), (1, 0), (0, -1), (-1, 0)])
dy, dx = next(rotation)
for j, x in enumerate(a, 1):
    for _ in range(x):
        grid[h][w] = str(j)
        y = h + dy
        x = w + dx
        if not(0 <= y < H and 0 <= x < W) or grid[y][x] != -1:
            dy, dx = next(rotation)
            h += dy
            w += dx
        else:
            h = y
            w = x

for row in grid:
    print(" ".join(row))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# D - Grid Coloring

H, W = map(int, input().split())
N = int(input())
A = list(int(a) for a in input().split())
color = []
for i, a in enumerate(A, 1):
    color.extend([i] * a)

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

k = 0
for i in range(H):
    for j in range(W):
        if i%2:
            j = (W - 1) - j
        grid[i][j] = color[k]
        k += 1

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

Submission status: Accepted

Solution code:
from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations # (string,3) 3回
from collections import deque
from collections import defaultdict
import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
h,w = readInts()
n = I()
a = readInts()
i = 0
for j in range(h):
    row = []
    for _ in range(w):
        row += [i+1]
        a[i] -= 1
        if a[i] == 0:
            i += 1
    if j % 2 == 1:
        row = reversed(row)
    print(*row)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Fri Jun  5 09:50:00 2020

@author: NEC-PCuser
"""

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

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

count = 0
start_H = 0
start_W = 0
direction = 0
for i in range(len(a)):
    for j in range(a[i]):
        gazo[start_H][start_W] = i + 1
        if (start_W < W - 1 and direction == 0):
            start_W += 1
        elif (start_W == W - 1 and direction == 0):
            start_H += 1
            direction = 1
        elif (start_W > 0 and direction == 1):
            start_W -= 1
        elif (start_W == 0 and direction == 1):
            start_H += 1
            direction = 0
    
for i in range(len(gazo)):
    print(' '.join(map(str, gazo[i])))

Submission status: Accepted

Solution code:
h,w,n,*a=map(int,open(0).read().split())
b=[]
for i in range(n):
    b+=[i+1]*a[i]
for i in range(0,h*w,w):
    if i//w%2==1:
        print(*b[i:i+w])
    else:
        print(*b[i:i+w][::-1])

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
n = int(input())
li = list(map(int, input().split()))
line = 1
L =[]
for i, a in enumerate(li, 1):
    while a > 0:
        L.append(i)
        a -= 1
        if len(L) == w:
            if line % 2:
                print(*L)
            else:
                print(*reversed(L))
            L = []
            line += 1

Submission status: Accepted

Solution code:
from collections import deque
import itertools

H , W = map(int,input().split())
N = int(input())
a = list(map(int,input().split()))
acu = [0] + list(itertools.accumulate(a))
ans = [[0] * W for _ in range(H)]
cnt = 1
jdg = 1

for h in range(H):
    if h % 2 == 1:
        for w in range(W):
            #print(jdg)
            ans[h][w] = jdg
            if cnt >= acu[jdg]:
                jdg += 1
            cnt += 1
    if h % 2 == 0:
        for w in reversed(range(W)):
            #print(jdg)
            ans[h][w] = jdg
            if cnt >= acu[jdg]:
                jdg += 1
            cnt += 1
for i in range(H):
    print(*ans[i])

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
maze = []
for _ in range(h):
    l = [None] * w
    maze.append(l)
n = int(input())
dat = list(map(int, input().split()))

curcol = 0
curpasted = 0

for hh in range(h):
    if hh % 2 == 0:
        for ww in range(0, w):
            maze[hh][ww] = curcol + 1
            curpasted += 1
            if curpasted >= dat[curcol]:
                curcol += 1
                curpasted = 0
    else:
        for ww in range(w-1, -1, -1):
            maze[hh][ww] = curcol + 1
            curpasted += 1
            if curpasted >= dat[curcol]:
                curcol += 1
                curpasted = 0
from pprint import pprint
for i in range(h):
    print(" ".join(list(map(str, maze[i]))))