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

Submission status: Accepted

Solution code:
import numpy as np

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

for i, v in enumerate(a):
    for _ in range(v):
        ans.append(i+1)
ans = np.array(ans).reshape(H, W)

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

for j in ans:
    print(*list(j))

Submission status: Accepted

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

arr = []
for i in range(1, n + 1):
    for j in range(a[i - 1]):
        arr.append(i)

arr = np.array(arr)
arr = arr.reshape(h, -1)

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

Submission status: Accepted

Solution code:
import sys

# import re
import math
import collections
# import decimal
import bisect
import itertools
import fractions
# import functools
import copy
import heapq
import decimal
# import statistics
import queue

sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===


def main():
    h, w = ns()
    n = ni()
    a = na()

    mat = [[-1 for i in range(w)] for j in range(h)]

    toLeft = True
    idx = 0
    for i in range(h):
        toLeft = not toLeft
        for j in range(w):
            a[idx] -= 1
            dirc = -1 if toLeft else 1
            adj = -1 if toLeft else 0
            mat[i][dirc * j + adj] = idx + 1
            if a[idx] == 0:
                idx += 1

    for m in mat:
        print(*m, sep=" ")


if __name__ == '__main__':
    main()

Submission status: Accepted

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


def read():
    H, W = map(int, input().strip().split())
    N = int(input().strip())
    A = list(map(int, input().strip().split()))
    return H, W, N, A


def solve(H, W, N, A):
    X = []
    for i in range(N):
        X.extend([i+1] * A[i])
    for i in range(H):
        if i % 2 == 1:
            line = X[W*i:W*(i+1)][::-1]
        else:
            line = X[W*i:W*(i+1)]
        print(' '.join(map(str, line)))


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
N=int(input())
a=list(map(int,input().split()))
acnt=cnt=0
Hcnt=Wcnt=0
ans = [[str(-1)]*W for i in range(H)]

for i in range(W*H):
    ans[Hcnt][Wcnt]=str(acnt+1)
    if Hcnt % 2 ==0:
        if Wcnt == W-1:
            Hcnt+=1
        else:
            Wcnt+=1
    else:
        if Wcnt == 0:
            Hcnt+=1
        else:
            Wcnt-=1

    cnt+=1
    if cnt == a[acnt]:
        cnt=0
        acnt+=1

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

Submission status: Accepted

Solution code:
from sys import stdin

def main():
    input = stdin.readline
    h, w = map(int, input().split())
    n = int(input())
    a = list(map(int, input().split()))
    m = [[0] * w for _ in range(h)]
    x, y, LR, v = 0, 0, 1, 0
    for n in a:
        v += 1
        for _ in range(n):
            m[y][x] = str(v)
            x += LR
            if x == w:
                y += 1
                x = w - 1
                LR = -1
            elif x < 0:
                y += 1
                x = 0
                LR = 1
    for i in range(h):
        print(" ".join(m[i]))
main()

Submission status: Accepted

Solution code:
import math
import bisect
from queue import Queue
from collections import deque

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

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

x = 0
y = 0
z = 0
s = 0


for i in range(h*w):
    table[y][x] = z + 1

    if(s < a[z]-1):
        s += 1
    else:
        s = 0
        z += 1

    if(y % 2 == 0):
        if(x == w-1):
            y += 1
        else:
            x += 1
    else:
        if(x == 0):
            y += 1
        else:
            x -= 1

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

Submission status: Accepted

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

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

i = j = 0
foward = True
for k in range(len(a)):
    num = a[k]
    while num > 0:
        colors[i][j] = k + 1
        if foward is True:
            j += 1
            if j == len(colors[0]):
                j -= 1
                i += 1
                foward = False
        else:
            j -= 1
            if j == -1:
                j += 1
                i += 1
                foward = True
        num -= 1

for i in range(len(colors)):
    for j in range(len(colors[0])):
        print(colors[i][j], end=' ')
    print()

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
n = int(input())
a = list(map(int,input().split()))
i = 0
ans = [[0 for _ in range(w)] for _ in range(h)]
for j in range(n):
  for _ in range(a[j]):
    hi = i//w
    if hi%2 == 0:
      ans[hi][i%w] = j+1
    else:
      ans[hi][w-1-i%w] = j+1
    i += 1
for ansi in ans:
  for j in range(w-1):
    print(ansi[j],end=" ")
  print(ansi[w-1])

Submission status: Accepted

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

ans = [[] for _ in range(h)]
k = 1
for i in range(h):
    row = []
    for j in range(w):
        if not a[k]: k += 1
        row.append(k)
        a[k] -= 1
    if i % 2 == 1:
        ans[i] = list(reversed(row))
    else:
        ans[i] = row

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

Submission status: Accepted

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

ans = [[None] * W for i in range(H)]
AA = []
for i, a in enumerate(A):
    AA.append((a,i))
AA.sort(reverse=True)
now_num = 0
now_row = 0
now_col = 0
for i, a in enumerate(AA):
    num = a[0]
    while num > 0:
        flg = False
        for j in range(now_col, W):
            # print(i,j,num,a[0],a[1],now_row)
            if num != 0:
                num -= 1
                ans[now_row][j] = str(a[1]+1)
            else:
                flg = True
                break
        if flg:
            now_col = j
            break
        else:
            now_col = 0
            now_row += 1

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

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

Submission status: Accepted

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


import numpy as np

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

lis = np.array(lis).reshape((h,w))

for i in range(1,h,2):
    lis[i] = lis[i][::-1]

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

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline

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

    ans = [[0] * W for _ in range(H)]
    h = 0; w = 0
    popflag = True
    backflag = False
    for _ in range(H*W):
        if popflag:
            tmp = a.pop()
            tmplist = [len(a)+1]*tmp
            popflag = False
        if backflag:
            ans[h][w] = tmplist.pop()
            if w-1 == -1:
                backflag = False
                h += 1
            else:
                w -= 1
        else:
            ans[h][w] = tmplist.pop()
            if w+1 == W:
                backflag = True
                h += 1
            else:
                w += 1
        if len(tmplist) == 0:
            popflag = True
    
    for i in range(H):
        print(*ans[i])

if __name__ == "__main__":
    main()

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):
    l += [i+1]*a[i]

for i in range(H):
    row = l[W*i:W*i+W]
    if i%2:
        row = row[::-1]

    for v in row:
        print(v, end=' ')
    print()

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
n = int(input())
a = list(map(int,input().split()))
A = []
for i in range(n):
    for j in range(a[i]):
        A.append(i+1)
# C = [[0]*w]*h
C = [[0 for j in range(w)] for i in range(h)]
for i in range(h):
    for j in range(w):
        if i%2 == 0:
            C[i][j] = A[i*w+j]
        else:
            C[i][-j-1] = A[i*w+j]
for i in range(h):
    for j in range(w):
        print(C[i][j],end="")
        print(" ",end="")
    print("")

Submission status: Accepted

Solution code:
H, W, N, *a = map(int, open(0).read().split())
a = [None] + a
mapp = [[-1] * W for _ in range(H)]
x = 0
y = 0
c = 1
for _ in range(W):
    for _ in range(H):
        if a[c] == 0:
            c += 1
        mapp[y][x] = c
        a[c] -= 1
        y += 1 * [+1, -1][x % 2]
    y -= 1 * [+1, -1][x % 2]
    x += 1

for y in range(H):
    print(*mapp[y])

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():


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

        


    route = []
    for h in range(H):
        if h % 2 == 0:
            route.extend([(h, w) for w in range(W)])
        else:
            route.extend([(h, w) for w in reversed(range(W))])
    
    ans=[[0]*W for _ in range(H)]
    val=1
    i=0
    cnt=0
    for i in range(N):
        for j in range(A[i]):
            ans[route[cnt][0]][route[cnt][1]]=i+1
            cnt+=1
    for i in ans:
        print(*i)



resolve()

Submission status: Accepted

Solution code:
#!python3

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

# input
H, W = LI()
N = int(input())
A = LI()


def main():
    l = []
    for i in range(N):
        for _ in range(A[i]):
            l.append(i + 1)
    
    for i in range(H):
        x = l[(i * W):(i * W + W)]
        if i % 2 == 1:
            x = x[::-1]
        print(*x)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))
field = [[0 for j in range(w)] for i in range(h)]
#[y, x]
d = [[0, 1], [1, 0], [0, -1], [-1, 0]]
y, x = 0, 0
if h == 1 and w == 1:
    print(1)
else:
    for i in range(n):
        cnt = a[i]
        while cnt > 0:
            for j in range(len(d)):
                if 0 <= y + d[j][0] and y + d[j][0] < h and 0 <= x + d[j][1] and x + d[j][1] < w:
                    if field[y+d[j][0]][x+d[j][1]] == 0: 
                        dy, dx = d[j][0], d[j][1]
                        break
            field[y][x] = i+1
            y += dy
            x += dx
            cnt -= 1
    for i in range(h):
        for j in range(w):
            print(field[i][j], end=' ')
        print()

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
n = int(input())
a = list(map(int,input().split()))
B = [[0]*w for _ in range(h)]
Bi = 0
Bj = 0
for i in range(n):
    cnt = a[i]
    while cnt>0:
        B[Bi][Bj] = str(i+1)
        cnt-=1
        if Bi%2==0:
            if Bj<w-1:
                Bj+=1
            else:
                if Bi<h-1:
                    Bi+=1
                else:
                    break
        else:
            if Bj>0:
                Bj-=1
            else:
                if Bi<h-1:
                    Bi+=1
                else:
                    break
[print(' '.join(B[i]),end='\n') for i in range(h)]

Submission status: Accepted

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

align = [(i+1) for i, a in enumerate(A) for _ in range(a)]
# print("align: {}".format(align))
count = 0
ans = []
for i in range(H):
    tmp = align[W*i:W*(i+1)]
    if i % 2 != 0:
        tmp = tmp[::-1]
    print(*tmp)

Submission status: Accepted

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

if h <= w:
    yokonaga = True
else:
    yokonaga = False
    h, w = w, h

board = [[0 for i in range(w)] for j in range(h)]
#print(bord)
longer_side = max(h, w)

pointer_h = 0
pointer_w = 0

for color in dic_color.keys():
    num = dic_color[color]
    for i in range(num):
        board[pointer_h][pointer_w] = color
        #print(pointer_h, pointer_w, color, num)
        if pointer_h % 2 == 0 and pointer_w < w - 1:
            pointer_w += 1
            #print(1)
        elif pointer_h % 2 == 0 and pointer_w == w - 1:
            pointer_h += 1
            #print(2)
        elif pointer_h % 2 == 1 and pointer_w > 0:
            pointer_w -= 1
            #print(3)
        elif pointer_h % 2 == 1 and pointer_w == 0:
            pointer_h += 1
            #print(4)

if yokonaga:
    for i in range(h):
        print(*board[i], sep=" ")
else:
    board = [[board[i][j] for i in range(h)] for j in range(w)]
    for i in range(w):
        print(*board[i], sep=" ")

Submission status: Accepted

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

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

color = 1
filled = 0
for h in range(H):
    if h%2==0: sec = range(W)
    else: sec = range(W-1, -1, -1)
    for w in sec:
        area[h][w] = color
        filled+=1
        if filled == a[color-1]:
            color+=1
            filled = 0
    print(*area[h], sep=' ')

Submission status: Accepted

Solution code:
n,w = map(int,input().split(" "))
c = int(input())
ar = [[0 for i in range(w)] for j in range(n)]
br = list(map(int,input().split(" ")))
z = 1
for i in range(n):
    if i % 2 ==0:
        for j in range(w):
            ar[i][j] = z
            br[z-1] -= 1
            if br[z-1] == 0:
                z += 1
    else:
        for j in range(w-1,-1,-1):
            ar[i][j] = z 
            br[z-1] -= 1
            if br[z-1] == 0:
                z += 1
for r in ar:
    for i in range(w-1):
        print(r[i],end=" ")
    print(r[w-1])

Submission status: Accepted

Solution code:
#dpでできないかな？
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod=10**9+7

h,w=MI()
n=I()
colors=LI()
col=[]
for i in range(n):
    for j in range(colors[i]):
        col.append(i+1)
#print(col)
lis=[[0 for i in range(w)] for j in range(h)]
count=0
for i in range(h):
    for j in range(w):
        if i%2==0:
            k=j
        else:
            k=w-j-1
        lis[i][k]=col[count]
        count+=1
#print(lis)
for i in range(h):
    for j in range(w):
        print(lis[i][j],end=" ")
    print()

Submission status: Accepted

Solution code:
h,w= map(int, input().split())
n=int(input())
a=list(map(int, input().split()))
maze=[[0]*w for i in range(h)]
for i in range(h):
  for j in range(w):
    maze[i][j]=n
    a[n-1]-=1
    if a[n-1]==0:
      a.pop(-1)
      n-=1
for i in range(h):
  if i%2==1:
    print(*maze[i][::-1])
  else:
    print(*maze[i])

Submission status: Accepted

Solution code:
def main():
    from itertools import cycle

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

    g = [[-1] * (W + 2) for _ in range(H + 2)]
    for r in range(H + 1):
        g[r][0] = g[r][W + 1] = 0
    for c in range(W + 1):
        g[0][c] = g[H + 1][c] = 0

    def next_pos():
        dir_ = cycle([(1, 0), (0, 1), (-1, 0), (0, -1)])
        r, c = 1, 1
        cnt = 0

        dr, dc = next(dir_)
        while cnt < H * W:
            g[r][c] = cnt
            cnt += 1
            yield r - 1, c - 1
            if ~g[r + dr][c + dc]:
                dr, dc = next(dir_)
            r += dr
            c += dc

    gen_np = next_pos()

    ans = [[-1] * W for _ in range(H)]
    for i, x in enumerate(a, start=1):
        for _ in range(x):
            r, c = next(gen_np)
            ans[r][c] = i

    for row in ans:
        print(*row)


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 for j in range(w)] for i in range(h)]
nextColor = 0
H = 0
W = 0

while nextColor < n:
    # ai回マスを塗る
    for j in range(a[nextColor]):
        ans[H][W] = nextColor+1
        W += 1

        # 行末まで来たら次の行の先頭へ
        if W >= w:
            H += 1
            W = 0
    
    # 塗り終えたら次の色へ
    nextColor += 1

for i in range(h):
    # 奇数行は反転させる
    if i%2 != 0:
        ans[i] = ans[i][::-1]
    print(*ans[i])

Submission status: Accepted

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

# ans = [[0] * w] * h
ans = [[0 for j in range(w)] for i in range(h)]
nextColor = 0
H = 0
W = 0

while nextColor < n:
    # ai回マスを塗る
    for j in range(a[nextColor]):
        ans[H][W] = nextColor+1
        # print("H: " + str(H))
        # print("W: " + str(W))
        # print("ans[H][W]: " + str(ans[H][W]))
        # print(ans)
        W += 1

        # 行末まで来たら次の行の先頭へ
        if W >= w:
            H += 1
            W = 0
    # print("-----------------")
    
    # 塗り終えたら次の色へ
    nextColor += 1

for i in range(h):
    # 奇数行は反転させる
    if i%2 != 0:
        ans[i] = ans[i][::-1]
    print(*ans[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)]
h = -1
w = -1
flag = False
for i in range(N):
    for j in range(a[i]):
        w+=1
        w%=W
        if w==0:
            w=0
            h+=1
            if flag:
                flag = False
            else:
                flag = True
        if flag:
            ans[h][w] = i+1
        else:
            ans[h][W-1-w] = i+1
    
for i in range(H):
    print(*ans[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)]
S = 0
for i, a in enumerate(A, start=1):
    for _ in range(a):
        h, w = divmod(S, W)
        ans[h][w] = i
        S += 1

for i, a in enumerate(ans):
    print(*a[::(-1)**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)]
f=1
k=0
for i in range(h):
    for j in range(w):
        if f==1:
            if a[k]<=0:
                k+=1
            ans[i][j]=k+1
            a[k]-=1
        else:
            if a[k]<=0:
                k+=1
            ans[i][w-1-j]=k+1
            a[k]-=1
        if j==w-1:
            f=f*(-1)
for i in range(h):
  print(*ans[i])

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from heapq import heapify, heappop, heappush
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

H, W = MAP()
N = INT()
A = LIST()
C = [[0] * W for i in range(H)]
num = 0
for h in range(H):
    if h % 2 == 0:
        for w in range(W):
            if A[num] > 0:
                A[num] -= 1
            else:
                num += 1
                A[num] -= 1
            C[h][w] = num + 1
    else:
        for w in range(W-1, -1, -1):
            if A[num] > 0:
                # C[h][w] = num + 1
                A[num] -= 1
            else:
                num += 1
                A[num] -= 1
            C[h][w] = num + 1

for c in C:
    print(*c, end="\n")

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)
MOD = 10 ** 9 + 7

H, W = map(int, input().split())
N = int(input())
A = list(map(int, input().split())) + [-1]

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

flag = True #右向き
h = 0
w = 0
k = 1
next_ = A[0]

while next_ > 0:
    G[h][w] = k
    next_ -= 1
    if flag:
        if w == W - 1:
            flag = False
            h += 1
        else:
            w += 1
    else:
        if w == 0:
            flag = True
            h += 1
        else:
            w -= 1


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

for g in G:
    print (*g)

Submission status: Accepted

Solution code:
from sys import stdin
def main():
    #入力
    readline=stdin.readline
    h,w=map(int,readline().split())
    n=int(readline())
    a=list(map(int,readline().split()))

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

    x=0
    y=0
    color=1
    cnt=0
    total=0
    flag=True
    while total<h*w:
        grid[y][x]=color
        cnt+=1
        total+=1
        if flag:
            if x+1<w:
                x+=1
            else:
                y+=1
                flag=False
        else:
            if x-1>=0:
                x-=1
            else:
                y+=1
                flag=True
        if cnt==a[color-1]:
            color+=1
            cnt=0

    for i in range(h):
        print(*grid[i])
        
if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

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

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

h,w = inpl()
n = inp()
a = inpl()
ind = 0
now = a[0]
res = [[None]*w for _ in range(h)]
for i in range(h):
    for j in range(w):
        res[i][j] = ind+1
        now -= 1
        if now == 0 and ind < n-1:
            ind += 1
            now = a[ind]
for i in range(h):
    if i%2:
        tmp = [str(x) for x in res[i][::-1]]
    else:
        tmp = [str(x) for x in res[i]]  
    print(' '.join(tmp))

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    H, W = map(int, input().split())
    n = int(input())
    A = list(map(int, input().split()))

    grid = [[0 for _ in range(W)] for _ in range(H)]
    h, w = 0, 0
    for i in range(n):
        cnt = A[i]
        while cnt:
            grid[h][w] = i + 1
            cnt -= 1
            if w + 1 < W:
                w += 1
            else:
                h += 1
                w = 0

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


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

h,w = iim()
n = ii()
A = iil()
ans = []
l = []
for i,item in enumerate(A):
    l += [i+1]*A[i]

for i in range(h):
    p = l[i*w:(i+1)*w]
    if i%2 == 1:
        ans.append(p[::-1])
    else:
        ans.append(p)

for i in ans:
    print(*i)

Submission status: Accepted

Solution code:
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

h,w = map(int,input().split())
n = int(input())
A = list(map(int,input().split()))
C = [[0] * w for i in range(h)]
color = 0
for i in range(h):
  if i % 2 == 0:
    for j in range(w):
      C[i][j] = color + 1
      A[color] -= 1
      if A[color] == 0:color += 1
  else:
    for j in range(w-1,-1,-1):
      C[i][j] = color + 1
      A[color] -= 1
      if A[color] == 0:color += 1
for ci in C:
  print(*ci)

Submission status: Accepted

Solution code:
import sys
from collections import Counter
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))

h,w=mp()
n=int(input())
a=lmp()
l=[]
for i in range(n):
    for k in range(a[i]):
        l.append(i+1)


ans=[]
for i in range(h):
    if i%2==0:
        ans.append(l[w*i:w*(i+1)])
    else:
        ans.append(reversed(l[w*i:w*(i+1)]))
for i in range(h):
    print(*ans[i])

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
H, W = map(int, input().split())
N = int(input())
a = list(map(int, input().split()))
res = [[0] * W for _ in range(H)]
s = []
for i in range(N): s += [i + 1] * a[i]
for i in range(H):
  if i % 2:
    for j in range(W - 1, -1, -1): res[i][j] = s.pop()
  else:
    for j in range(W): res[i][j] = s.pop()
for r in res: print(*r)

Submission status: Accepted

Solution code:
def main():
    H, W = (int(_) for _ in input().split())
    N = int(input())
    A = [int(_) for _ in input().split()]
    lst = []
    for i, a in enumerate(A, 1):
        lst += [i] * a
    for i in range(H):
        x = lst[i*W:(i+1)*W] if i%2 == 0 else lst[i*W:(i+1)*W][::-1]
        print(*x)
    return

if __name__ == '__main__':
    main()

Submission status: Accepted

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

c = [[0] * w for i in range(h)]
cnt = 0
flag = 0
for i in range(n):

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

Submission status: Accepted

Solution code:
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)]
if H%2==1:
    END=[H-1,W-1]
else:
    END=[H-1,0]
h=0
w=0
col=1
cnt=0

while h<H:
    ans[h][w]=col
    cnt+=1
    if cnt>=a[col-1]:
        cnt=0
        col+=1
    if (h%2==1 and w==0) or (h%2==0 and w==W-1):
        h+=1
    else:
        w+=(-1)**h

if W>1:
    for A in ans:
        for B in A:
            print(B,end=" ")
        print()
else:
    for A in ans:
        print(A[0])

Submission status: Accepted

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

ans = []
tmp = []
count = 0
for i in range(n):
    num = i+1
    itr = 0
    while itr < a[i]:
        tmp.append(num)
        if len(tmp) == w:
            count += 1
            if count %2 != 0:
                tmp.reverse()
            ans.append(tmp)
            tmp=[]
            
        itr+=1
for i in ans:
    for j in range(len(i)):
        print(i[j],end=" ")
    print()