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()))
ans = [[0]*W for k in range(H)]
c = []

for k in range(N):
    c += [k+1]*a[k]
now = 0

for k in range(H):
    if k%2 == 0:
        for l in range(W):
            ans[k][l] = c[now]
            now += 1
    else:
        for l in range(W-1,-1,-1):
            ans[k][l] = c[now]
            now += 1
for e in ans:
    print(*e)

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
N = int(input())
A = list(map(int,input().split()))
A.insert(0,0)
C = [[0 for _ in range(W)] for _ in range(H)]
cur = [0,0,"R"]
for k in range(1,N+1):
#     ipdb.set_trace()
    a = A[k]
    cnt = 0
    y,x,d = cur
    for i in range(y,H):
        y = i
        if d=="R":
            for j in range(x,W):
                C[i][j]=str(k)
                cnt += 1
                if j+1<W:
                    x = j+1
                else:
                    x = j
                if cnt==a:break
            if cnt==a:break
            d="L"
        else:
            for j in range(x,-1,-1):
                C[i][j]=str(k)
                cnt += 1
                if j>=1:
                    x = j-1
                else:
                    x = 0
                if cnt==a:break
            if cnt==a:break
            d="R"
    if x==W-1:
        if C[y][x]!=0:
            y += 1
            d="L"
    elif x==0:
        if C[y][x]!=0:
            y += 1
            d="R"
    cur = [y,x,d]                
for i in range(H):
    print(*C[i])

Submission status: Accepted

Solution code:
import sys
input= lambda: sys.stdin.readline().rstrip()
def pin(type=int):
    return map(type,input().split())

#%%code
def resolve():
    H,W=pin()
    N,=pin()
    A=list(pin())
    aindex=0
    atemp=0
    for h in range(H):
        temp=[0]*W
        wren=range(W)
        if h%2!=0:wren=reversed(range(W))
        for w in wren:
            temp[w]=aindex+1
            atemp+=1
            if atemp==A[aindex]:
                aindex+=1;atemp=0
        print(*temp)
        
#%%submit!
resolve()

Submission status: Accepted

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

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

h = 0
w = 0
w_flag = True #右向き

for index, a in enumerate(A):
    count = 0
    while count < a:
        count += 1
        G[h][w] = index + 1
        if w_flag:
            if w == W - 1:
                h += 1
                w_flag = False
            else:
                w += 1
        else:
            if w == 0:
                h += 1
                w_flag = True
            else:
                w -= 1



for tmp in G:
    print (*tmp, sep = ' ')

Submission status: Accepted

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

def draw_color():
    global ans, ptr
    if A[ptr] > 0: 
        A[ptr] -= 1
        return ptr + 1
    ptr += 1
    return draw_color()


# マス目の指示
for h in range(H):
    if h % 2 == 0:
        for w in range(W):
            ans[h][w] = draw_color()
    else:
        for w in reversed(range(W)):
            ans[h][w] = draw_color()

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

Submission status: Accepted

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

X = []
for i in range(n):
    X += [i+1]*A[i]

for i in range(h):
    x = X[w*i:w*(i+1)]
    if i%2 == 0:
        print(*x)
    else:
        x.reverse()
        print(*x)

Submission status: Accepted

Solution code:
def main():
    H, W = (int(i) for i in input().split())
    _ = int(input())
    A = [int(i) for i in input().split()]
    c = [[0]*W for _ in range(H)]
    i = 1
    cnt = 0
    for h in range(H):
        rangeW = range(W) if h % 2 == 0 else range(W)[::-1]
        for w in rangeW:
            c[h][w] = i
            cnt += 1
            if cnt == A[i-1]:
                i += 1
                cnt = 0
    for row in c:
        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 = []
for i, a in enumerate(A, start=1):
    ans.extend([i] * a)
ans.sort()

for i, w in enumerate(range(0, H * W, W)):
    print(*ans[w: w + W][::(-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 i in range(h)]

k=0
for i in range(h):
    if i%2==0:
        for j in range(w):
            if a[k]==0:
                k+=1
            a[k]-=1
            ans[i][j]=k+1

    else:
        for j in range(w-1,-1,-1):
            if a[k]==0:
                k+=1
            a[k]-=1
            ans[i][j]=k+1

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

Submission status: Accepted

Solution code:
import sys
def main():
    input = sys.stdin.readline
    H,W=map(int, input().split())
    N=int(input())
    *A,=map(int, input().split())
    c=[[0] * W for _ in range(H)]
    ai=0
    for i in range(H):
        for j in range(W):
            k = j if i&1==0 else W-j-1
            c[i][k]=ai+1
            A[ai]-=1
            if A[ai]<=0: ai+=1
    for i in range(H):
        print(*c[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()))

tab_sum = [0]
sum_tmp = 0
for ai in a:
    sum_tmp += ai
    tab_sum.append(sum_tmp)

ans = [[None] * W for _ in range(H)]
    
tab_idx = 1
for idx in range(H * W):
    y = idx // W
    if y % 2 == 0:
        x = idx % W
    else:
        x = (W - 1 - idx) % W
    #
    if idx >= tab_sum[tab_idx]: tab_idx += 1
    ans[y][x] = tab_idx

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

Submission status: Accepted

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

color = []

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

flag = 1
for i in range(H):
    left = i * W
    right = left + W
    print(*color[left:right][::flag])
    flag *= -1

Submission status: Accepted

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

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

def main():
    mod=10**9+7
    H,W=MI()
    N=I()
    a=LI()
    
    ans=[[0]*W for _ in range(H)]
    now=1
    for i in range(H):
        for jj in range(W):
            if i%2==1:
                j=W-1-jj
            else:
                j=jj
                

            if a[now-1]<=0:
                now+=1
            ans[i][j]=now
            a[now-1]-=1
            
    for i in range(H):
        print(' '.join(map(str, ans[i])))

            
            

main()

Submission status: Accepted

Solution code:
def main():
    h, w = map(int, input().split())
    n = int(input())
    a = [-1] + [int(x) for x in input().split()]
    for i in range(1, n + 1):
        a[i] += a[i - 1]
    answer = ["-1" for _ in range(w * h)]
    index = 0
    for i in range(h * w):
        if not (a[index] < i <= a[index + 1]):
            index += 1
        answer[i] = str(index + 1)
    for i in range(h):
        if i % 2 == 0:
            print(" ".join(answer[i * w: (i + 1) * w]))
        else:
            print(" ".join(reversed(answer[i * w: (i + 1) * w])))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def solve():
    h,w = map(int,input().split())
    ans = [[0 for _ in range(w)] for _ in range(h)]
    n = int(input())
    a = list(map(int,input().split()))
    now = 0
    for i in range(h):
        if i%2==0:
            for j in range(w):
                if a[now]>0:
                    ans[i][j] = now+1
                    a[now]-=1
                else:
                    now+=1
                    ans[i][j] = now+1
                    a[now]-=1
        else:
            for j in range(w)[::-1]:
                if a[now]>0:
                    ans[i][j] = now+1
                    a[now]-=1
                else:
                    now+=1
                    ans[i][j] = now+1
                    a[now]-=1
    for i in range(h):
        for j in range(w):
            print(ans[i][j],end=' ')
        print()
if __name__=='__main__':
    solve()

Submission status: Accepted

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

grid = []
for h in range(H):
    x = col[h * W: (h + 1) * W]
    if h % 2 == 0:
        x.reverse()
    grid.append(x)

for g in grid:
    print(*g)

Submission status: Accepted

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

Submission status: Accepted

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

H, W = map(int, readline().split())
N = int(readline())
Arr = []
A = list(map(int, readline().split()))
for i in range(N):
    Arr.extend([i+1]*A[i])

G = [Arr[W*i:W*(i+1)] for i in range(H)]
for i in range(0, H, 2):
    G[i].reverse()
for g in G:
    print(' '.join(map(str, g)))

Submission status: Accepted

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

ans = [[None]*W for _ in range(H)]
y = x = 0
for i,a in enumerate(A):
    for j in range(a):
        ans[y][x] = str(i+1)
        if y%2:
            if x==0:
                y += 1
            else:
                x -= 1
        else:
            if x==W-1:
                y += 1
            else:
                x += 1
for row in ans:
    print(*row)

Submission status: Accepted

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

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()))
    B = []
    for i in range(n):
        for _ in range(A[i]):
            B.append(i + 1)

    res = [[0] * W for _ in range(H)]
    for b in B:
        for h in range(H):
            for w in range(W):
                if res[h][w] == 0:
                    res[h][w] = b
                    break
            else:
                continue
            break

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


if __name__ == '__main__':
    resolve()

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

x, y = 0, 0
sign = 1
for idx, a in enumerate(A):
  idx += 1
  for _ in range(a):
    if x < 0 or x >= W:
      y += 1
      sign = sign * -1
      x += 1 * sign
    ans[y][x] = idx
    x += 1 * sign

for a in ans:
  print(*a)

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
n = int(input())
a = list(map(int,input().split()))
ans = [[0]*w for i in range(h)]
i = 0
j = 0
flag = 0
cnt = 0
for p in range(n):
    color = p+1
    numberofcolor = a[p]
    while numberofcolor >0:

        if j > w-1:
            flag = 1- flag
            j = w-1
            i +=1
            continue
        if j <0:
            flag = 1-flag
            j = 0
            i+=1
            continue
        if i > h-1:break
        numberofcolor -= 1
        ans[i][j] = color
        if flag == 0:j +=1
        else:j-=1
            
for i in range(h):
    print(*ans[i])

Submission status: Accepted

Solution code:
import math  # noqa
import bisect  # noqa
import queue  # noqa


if __name__ == '__main__':
    H, W = map(int, input().split())
    N = int(input())
    A = list(map(int, input().split()))

    que = queue.Queue()
    for i, a in enumerate(A):
        que.put((a, i + 1))

    field = [[0 for _ in range(W)] for _ in range(H)]
    num, color = que.get()
    for i in range(0, H):
        if i % 2 == 0:
            j_range = range(0, W)
        else:
            j_range = range(W - 1, -1, -1)
        for j in j_range:
            if num == 0:
                num, color = que.get()
            field[i][j] = color
            num -= 1

    for row in field:
        print(*row)

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
n = int(input())
a = list(map(int,input().split()))
c = [0] * w * h
an = [[] for i in range(h)]

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

Submission status: Accepted

Solution code:
import sys
import numpy as np


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(10 ** 9)


def main():
    H, W = map(int, input().split())
    N = int(input())
    A = list(map(int, input().split()))
    C = np.zeros(H * W, dtype=np.int32)
    first = 0
    for i, a in enumerate(A, 1):
        C[first : first + a] = i
        first += a

    ans = C.reshape((H, W))
    ans[1::2, :] = ans[1::2, ::-1]

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

# print(H, W)
# print(N)
# print(A)

# 上下連結していないといけない
# 解が存在する
# 順番に置いていけばよい

build_order = []
for i in range(H):
    if i % 2 == 0:
        for j in range(W):
            build_order.append((i, j))
    else:
        for j in range(W - 1, -1, -1):
            build_order.append((i, j))

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

index = 0
color = 0

while index < W * H:
    c = A[color]
    k = 0
    while k < c:
        (i, j) = build_order[index]
        index += 1
        k += 1
        colors[i][j] = color + 1
    color += 1

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

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)]
now_h, now_w = 0, 0

rest = 0
col = 0
direction = 1
while now_h + now_w < H * W:
  if rest == 0:
    if len(a) == 0:
      break
    rest = a.pop(0)
    col += 1
  if direction == 1:
    if now_w + 1 < W:
      c[now_h][now_w] = col
      now_w += 1 
      rest -= 1
    elif now_w + 1 == W:
      c[now_h][now_w] = col
      now_h += 1
      direction = -1
      rest -= 1
  elif direction == -1:
    if now_w - 1 > -1:
      c[now_h][now_w] = col
      now_w -= 1
      rest -= 1
    elif now_w - 1 == -1:
      c[now_h][now_w] = col
      now_h += 1
      direction = 1
      rest -= 1

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

Submission status: Accepted

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

color = 1
color_count = A[color-1]

grid = [[None] * W for _ in range(H)]
for i in range(H):
    if i % 2 == 0:
        rng = range(W)
    else:
        rng = range(W-1,-1,-1)
    for j in rng:
        if color_count == 0:
            color += 1
            color_count = A[color-1]
        color_count -= 1
        grid[i][j] = color

for i in range(H):
    print(*grid[i], sep=' ')

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
n = int(input())
a = list(map(int,input().split()))
grid = [[0]*w for _ in range(h)]
cur = [0,0]
for i in range(n):
    color = str(i+1)
    remain = a[i]
    while remain > 0:
        remain -= 1
        grid[cur[0]][cur[1]] = color
        if cur[0] % 2 == 0:
            if cur[1] == w-1:
                cur[0] += 1
            else:
                cur[1] += 1
        else:
            if cur[1] == 0:
                cur[0] += 1
            else:
                cur[1] -= 1
for i in range(h):
    print(" ".join(grid[i]))

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):
    l=a[i]
    for j in range(l):
        a_.append(i+1)
ans=[]
now=0
for i in range(h):
    if (i+1)%2==1:
        for j in range(w):
            ans.append(a_[now])
            now+=1
    else:
        for j in range(now+(w-1),now-1,-1):
            ans.append(a_[j])
            now+=1
now=0
for i in range(h):
    for j in range(w):
        print(ans[now],end=" ")
        now+=1
    print()

Submission status: Accepted

Solution code:
if __name__ == '__main__':

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

    field = [0 for i in range(h * w)]
    index = 0
    for i in range(n):
        for j in range(a[i]):
            field[index] = str(i + 1)
            index += 1
    for i in range(h):
        ans = field[(i * w):(i * w + w)]
        if i % 2 == 1:
            ans.reverse()
        print(' '.join(ans))

Submission status: Accepted

Solution code:
import numpy as np
H, W = map(int, input().split())
N = int(input())
A = list(map(int, input().split()))
B = np.zeros([H,W], np.int64)
ind = 0
for i in range(H):
    for j in range(W):
        if A[ind] == 0:
            ind += 1
        B[i,j] = ind+1
        A[ind] -= 1

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

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

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
N=int(input())
A = list(map(int,input().split()))
G = [[0 for _ in range(W)] for _ in range(H)]
h,w=0,0
flag=0
for j,a in enumerate(A):
  for i in range(a):

    
    if w==W:
      h+=1;w-=1
      flag=1
    if w==-1:
      h+=1;w+=1
      flag=0

    if flag==0: 
      G[h][w]=j+1
      w+=1
    else:
      G[h][w]=j+1
      w-=1
      
for g in G:
  print(*g)

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(W)] for i in range(H)]
indexH = 0
indexW = 0
cnt = 0
for i in range(N):
    for j in range(a[i]):
        ans[indexH][indexW] = i + 1
        if cnt % 2 == 0:
            if indexW == W - 1:
                indexW = -1
                indexH += 1
                cnt += 1
            else:
                indexW += 1
        else:
            if indexW == -W:
                indexW = 0
                indexH += 1
                cnt += 1
            else:
                indexW -= 1

for i in ans:
    print(*i)

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
n = int(input())
A = list(map(int,input().split()))
area = [[0]*w for i in range(h)]
cnt = 1
inv = 1
for i in range(h):
    for j in range(w):
        if inv ==1:
            area[i][j] = str(cnt)
        else:
            area[i][-j-1] = str(cnt)
        if A[cnt-1] != 1:
            A[cnt-1] -=1
        else:
            cnt+=1
    inv *= -1
for i in range(h):
    print(" ".join(area[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)]

idx_ls = []

flip = 0
for i in range(h):
    if not flip:
        for j in range(w):
            idx_ls.append((i, j))
    else:
        for j in range(w-1, -1, -1):
            idx_ls.append((i, j))
    flip ^= 1


for color, a in enumerate(A):
    color += 1
    for _ in range(a):
        i, j = idx_ls.pop()
        ans[i][j] = color

for x in ans:
    print(*x)

Submission status: Accepted

Solution code:
def main():
    H,W = map(int,input().split())
    N = int(input())
    a = list(map(int,input().split()))
    a.reverse()
    ans = []
    i = 1
    tmp = []
    k = -1
    cnt = 1
    while a:
        num = a.pop()
        l = len(tmp)
        if num > W-l:
            tmp += [i]*(W-l)
            if i-1 == k and cnt%2 == 1:
                tmp.reverse()
                cnt += 1
            ans.append(tmp)
            num -= (W-l)
            while num >= W:
                ans.append([i]*W)
                num -= W
            tmp = [i]*num
            k = i

        else:
            tmp += [i]*num
            if len(tmp) == W:
                if i-1 == k and cnt%2==1:
                    tmp.reverse()
                    cnt += 1
                ans.append(tmp)
                tmp = []
        i += 1
    for i in range(1,H):
        if ans[i][0] == ans[i-1][-1]:
            ans[i].reverse()
    for v in ans:
        print(*v)
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
n=int(input())
ans=sum([[i+1]*j for i,j in enumerate(map(int,input().split()))],[])
for i in range(h):
  print(*ans[w*i:w*i+w][::(-1)**(i%2)],sep=" ")

Submission status: Accepted

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

b = []
for idx,a in enumerate(A):
  for i in range(a):
    b += [idx+1]

for i in range(H):
  subset = b[i*W:(i+1)*W]
  if i % 2 == 1:
    subset = list(reversed(subset))
  string = " ".join(list(map(str,subset)))
  print(string.strip())

Submission status: Accepted

Solution code:
H,W = map(int, input().split())
N = int(input())
As = list(map(int, input().split()))
li = []
for i,a in enumerate(As):
    li += [i+1]*a
# print(li)
fields = []
for i in range(H):
    ind_s = i*W
    ind_y = (i+1)*W
    if i % 2 == 1:
        fields.append(li[ind_s:ind_y][::-1])
    else:
        fields.append(li[ind_s:ind_y])
for r in fields:
    print(*r)

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
N=int(input())
la=list(map(int,input().split()))
lans=[[0 for _ in range(W)] for _ in range(H)]
count=0
tmpi=0
for i in range(H):
    if i%2==0:
        for j in range(W):
            if count==0:
                tmpi+=1
                count=la[tmpi-1]
            lans[i][j]=tmpi
            count-=1
    else:
        for j in range(W-1,-1,-1):
            if count==0:
                tmpi+=1
                count=la[tmpi-1]
            lans[i][j]=tmpi
            count-=1
for i in range(H):
    for j in range(W-1):
        print(lans[i][j],end=" ")
    print(lans[i][W-1])

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
N=int(input())
A=list(map(int,input().split()))
k=1
L=[]
for i in range(N):
	for j in range(A[i]):
		L.append(k)
	k=k+1
k=0
c=1
for j in range(H):
	if c==-1:
		tmp=L[k:k+W]
		ans=tmp[::-1]
	else:
		ans=L[k:k+W]
	for i in ans:
		print(i,end=" ")
	print()
	k+=W
	c=-1*c

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/arc080/tasks/arc080_b

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

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

flag = False
for i in range(h):
    bgn=i*w
    end=bgn+w
    res = hw[bgn:end]
    if flag:
        res=res[::-1]
        flag = False
    else:
        flag = True
    print(*res)

Submission status: Accepted

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

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

i = 0
for k in range(H):
    for l in range(W):
        if a[i] != 0:
            G[k][l] = i+1
            a[i] -= 1
        else:
            i += 1
            G[k][l] = i+1
            a[i] -= 1
for k in range(H):
    if k%2 == 0:
        print(*G[k])
    else:
        print(*G[k][::-1])

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]
tmp=[]
tmp=[[0]*w for i in range(h)]

for i,aa in enumerate(color):
    tmp[i//w][i%w]=color[i]

ans=[]
for i,t in enumerate(tmp):
    if i%2==0:
        ans.append(t)
    else:
        t.sort(reverse=True)
        ans.append(t)

for aa in ans:
    print(*aa)

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
n=int(input())
lis=list(map(int,input().split()))
ans=[]
cnt=0
s=""
hh=0
for i in range(n):
  for j in range(lis[i]):
    if hh==0:
      s=s+str(i+1)+" "
    else:
      s=str(i+1)+" "+s
    cnt+=1
    if cnt==w:
      ans.append(s)
      s=""
      cnt=0
      hh=(hh+1)%2
for i in range(h):
  print(ans[i])

Submission status: Accepted

Solution code:
def main():
    H, W = (int(i) for i in input().split())
    N = int(input())
    A = [int(i) for i in input().split()]
    c = [[0]*W for _ in range(H)]
    i = 0
    color = 1

    for h in range(H):
        if h % 2 == 0:
            range_W = range(W)
        else:
            range_W = range(W)[::-1]
        for w in range_W:
            c[h][w] += color
            A[i] -= 1
            if A[i] == 0:
                i += 1
                color += 1

    for i in range(H):
        print(*c[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]
#print(L)

for i in range(h):
    l = L[i*w:(i+1)*w]
    if i%2 == 1:
        l.reverse()
    print(*l)

Submission status: Accepted

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

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

ans = []
rev = 0

add = []
for i in range(1, H * W + 1):
    add.append(tmp[i-1])
    if i % W == 0:
        if rev:
            add = add[::-1]
        ans.append(add)
        rev = 1 - rev
        add = []

for a in ans:
    print(*a)

Submission status: Accepted

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

c = [[0]*w for i in range(h)]
i = j = 0
tor = True
for k in range(n):
        for x in range(a[k]):
                c[i][j] = k+1

                if tor:
                        j += 1
                        if j>=w:
                                i += 1
                                j = w-1
                                tor = False
                else:
                        j -= 1
                        if j<0:
                                i += 1
                                j = 0
                                tor = True
for i in c: print(*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 _ in range(H)]
i = j = a = 0
while A:
    n = len(A)
    a = A.pop()
    while a:
        ans[i][j] = n
        if i%2:
            if j:
                j -= 1
            else:
                i += 1
        else:
            if j==W-1:
                i += 1
            else:
                j += 1
        a -= 1
for row in ans:
    print(*row)

Submission status: Accepted

Solution code:
import sys
def main():
    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)]
    c_idx = 0
    for i in range(H):
        for j in range(W):
            if i&1: j = W-1-j
            if A[c_idx] == 0: c_idx += 1
            c[i][j] = c_idx + 1
            A[c_idx] -= 1
    
    for i in range(H):
        print(*c[i])


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
N = int(input())
X = list(map(int, input().split()))
Y = [[-1]*W for _ in range(H)]
u = 0
for i in range(H):
    s, e, d = (0, W, 1) if i%2 else (W-1, -1, -1)
    for j in range(s, e, d):
        if X[u]:
            X[u]-=1
            Y[i][j] = u+1
        else:
            u+=1
            X[u] -= 1
            Y[i][j] = u+1
for i in range(H):
    print(*Y[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 fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
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 = sys.maxsize
mod = 10 ** 9 + 7

h, w = MAP()
n = INT()
a = LIST()
ans = [[0 for _ in range(w)] for _ in range(h)]
index = 0
cnt = 0
for i in range(h):
    if i % 2 == 0:
        for j in range(w):
            if cnt < a[index]:
                ans[i][j] = index + 1
                cnt += 1
            else:
                index += 1
                cnt = 0
                if cnt < a[index]:
                    ans[i][j] = index + 1
                    cnt += 1
    else:
        for j in reversed(range(w)):
            if cnt < a[index]:
                ans[i][j] = index + 1
                cnt += 1
            else:
                index += 1
                cnt = 0
                if cnt < a[index]:
                    ans[i][j] = index + 1
                    cnt += 1
for i in range(h):
    print(' '.join(map(str, ans[i])))