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: Runtime Error

Solution code:
import numpy as np
H, W = map(int, input().split())
N = int(input())
A = list(map(int, input().split()))
s = ''
for i in range(N):
    s += str(i+1) * A[i]
B = np.array(list(map(int, s)), np.int64).reshape([H,W])

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

Submission status: Runtime Error

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 = [(0, 0)]

i = j = 0
flip = 0
flag = 1
for _ in range(h*w-1):
    if (j == w-1 or j == 0)and not flag:
        i += 1
        flip ^= 1
        flag = 1
        idx_ls.append((i, j))
        continue
    flag = 0
    j = j+[1, -1][flip]
    idx_ls.append((i, j))


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: Runtime Error

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

m = np.argsort(a)[::-1]
n = [str(i+1) for i in m]
c = [a[i] for i in m]

ans = ["" for _ in range(H)]
j = 0
for i in range(H):
    count = 0
    while count != W:
        if count + c[i] <= W:
            ans[i] += n[j] * c[j]
            count += c[j]
            j += 1
        else:
            count = W - count
            ans[i] += n[j] * count
            c[j] -= count 
            count = W

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

Submission status: Runtime Error

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

m = np.argsort(a)[::-1]
n = [str(i+1) for i in m]
c = [a[i] for i in m]

ans = ["" for _ in range(H)]
j = 0
for i in range(H):
    count = 0
    while count != W:
        if count + c[i] <= W:
            ans[i] += n[j] * c[j]
            count += c[j]
            j += 1
        else:
            count = W - count
            ans[i] += n[j] * count
            count = W

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

Submission status: Runtime Error

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

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

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

Submission status: Runtime Error

Solution code:
H,W=map(int,input().split())
N=int(input())
L=list(map(int,input().split()))
col=0
M=W*[[0]*H]
for i in range(H):
  if i%2==0:
    for j in range(W):
      if L[col]>0:
        M[i][j]=col+1
        L[col]-=1
      if L[col]==0:
        col+=1
  else:
    for j in range(W-1,0,-1):
      if L[col]>0:
        M[i][j]=col+1
        L[col]-=1
      if L[col]==0:
        col+=1
for q in range(H):
  for w in range(W):
    print(M[q][w],end=" ")
  print()

Submission status: Runtime Error

Solution code:
H,W=map(int,input().split())
N=int(input())
L=list(map(int,input().split()))
col=0
M=W*([0]*H)
for i in range(H):
  if i%2==0:
    for j in range(W):
      if L[col]>0:
        M[i][j]=col+1
        L[col]-=1
      if L[col]==0:
        col+=1
  else:
    for j in range(W-1,0,-1):
      if L[col]>0:
        M[i][j]=col+1
        L[col]-=1
      if L[col]==0:
        col+=1
for q in range(H):
  for w in range(W):
    print(M[q][w],end=" ")
  print()

Submission status: Runtime Error

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

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

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

res_se = []
for st in res:
    res_se.append(list(st))

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

Submission status: Runtime Error

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

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

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

length = len(res)
for i in range(length):
    print(res[i])

Submission status: Runtime Error

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

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

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

print('\n'.join(map(str, res)))

Submission status: Runtime Error

Solution code:
H, W = list(map(int, input().split()))
N = int(input())
A = list(map(int, input().split()))
res = [["" for _ in range(W)] for h in range(H)]
start_y, start_x = 0, -1
pos_y, pos_x = start_y, start_x
dy, dx = 0, 1
for idx, a in enumerate(A, 1):
    for i in range(a):
        pos_x += dx
        pos_y += dy
        res[pos_y][pos_x] = str(idx)
        if pos_x == W - 1:
            if dy == 0:
                dx = 0
                dy = 1
            else:
                dx = -1
                dy = 0
        elif not pos_y == 0 and pos_x == 0:
            if dy == 0:
                dx = 0
                dy = 1
            else:
                dx = 1
                dy = 0
res = ["".join(x) for x in res]
res = "\n".join(res)
print(res)

Submission status: Runtime Error

Solution code:
H, W = [int(i) for i in input().split()]
N = int(input())
a = [int(i) for i in input().split()]
c = [[0 for j in range(W)] for i in range(H)]
w = 0
h = 0
for i in range(N):
    for j in range(a[i]):
        c[h][w] = i+1
        if h%2 == 0:
            if w < W - 1:
                w += 1
            else:
                h += 1
        else:
            if w > 0:
                w -= 1
            else:
                h += 1
for i in range(H):
    print(*c[i]

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

ans_list = ([[i+1] * a[i] for i in range(N)])
ans_list = reduce(lambda a, b: a + b, ans_list)

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

Submission status: Runtime Error

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

i = 0
buf = [0] * W
k = 1
for x, ai in aip(range(1, N+1), map(int, input().split())):
  for _ in range(ai)
	buf[i] = x
    i += 1
    if i >= W:
      print(" ".join(buf[::k]))
      i = 0
      k = -k

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
N = int(input())
a = list(map(int, input().split()))
K = []
for i in range(N):
    for j in range(a[i]):
        K.append(i+1)
maze = [[0] * W for _ in range(H)]

def dfs(x, y, p, i):
    if y < 0 or y >= H or i >= H*W:
        return
    if 0 <= x < W:
        maze[y][x] = K[i]
    
    if x < 0:
        p = 0
        dfs(x+1, y+1, p, i)
        return
    elif x >= W:
        p = 1
        dfs(x-1, y+1, p, i)
        return
        
    if p == 0:
        dfs(x+1, y, p, i+1)
    else:
        dfs(x-1, y, p, i+1)

    return

dfs(0, 0, 0, 0)

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

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
N = int(input())
a = list(map(int, input().split()))
K = []
for i in range(N):
    for j in range(a[i]):
        K.append(i+1)
maze = [[0] * W for _ in range(H)]

def dfs(x, y, p, i):
    if y < 0 or y >= H:
        return
    if 0 <= x < W:
        maze[y][x] = K[i]
    
    if x < 0:
        p = 0
        dfs(x+1, y+1, p, i)
        return
    elif x >= W:
        p = 1
        dfs(x-1, y+1, p, i)
        return
        
    if p == 0:
        dfs(x+1, y, p, i+1)
    else:
        dfs(x-1, y, p, i+1)

    return

dfs(0, 0, 0, 0)

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

Submission status: Runtime Error

Solution code:
from collections import deque
import sys
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def MI(): return map(int, sys.stdin.readline().split())
def II(): return int(sys.stdin.readline())


H, W = MI()
N = II()
a = LI()
color = deque()
ans = [[0] * W for _ in range(H)]
# visited = [[0] * W for _ in range(H)]
x_diff = [-1, 0, 1, 0]
y_diff = [0, 1, 0, -1]

for i, num in enumerate(a):
    for j in range(num):
        color.append(i + 1)

# coordinate_q = deque()
# coordinate_q.append([0, 0])
# visited[0][0] = 1
# ans[0][0] = color.popleft()

"""
while len(coordinate_q) != 0:
    y_tmp, x_tmp = coordinate_q.popleft()
    for i in range(4):
        y, x = y_tmp + y_diff[i], x_tmp + x_diff[i]
        if 0 <= y <= H-1 and 0 <= x <= W-1 and visited[y][x] == 0:
            ans[y][x] = color.popleft()
            visited[y][x] = 1
            coordinate_q.append([y, x])
"""

"""
for i in range(H):
    for j in range(W):
        if i % 2 == 0:
            ans[i][j] = color.popleft()
        else:
            ans[i][-(j+1)] = color.popleft()
"""

def tansaku(y, x):
    if visited[y][x] == 1:
        return
    visited[y][x] = 1
    ans[y][x] = color.popleft()
    for i in range(4):
        ny = y + y_diff[i]
        nx = x + x_diff[i]
        if 0 <= ny <= H-1 and 0 <= nx <= W-1:
            tansaku(ny, nx)

visited = [[0] * W for _ in range(H)]
tansaku(0, 0)

for i in range(H):
    for j in range(W):
        print(ans[i][j], end=' ')
    print('')

Submission status: Runtime Error

Solution code:
height, wide = map(int, input().split())
n = int(input())
anstr = ''
a = list(map(int, input().split()))
for i in range(n):
    for j in range(a[i]):
        anstr += str(i)
for k in range(n/wide):
    if k % 2 == 0:
        print(anstr[k:k+wide])
    else:
        print(anstr[k:k+wide:-1])

Submission status: Runtime Error

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

L = [[0 for i in range(W)] for j in range(H)]
for i in range(H * W):
    if int((i // W)) % 2 == 0:
        L[int((i // W))][i % W] = A[i]
    else:
        L[int((i // W))][-1 - i % W] = A[i]

for i in range(H):
    L[i] = list(map(str, L[i]))


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

Submission status: Runtime Error

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

x = 0
y = 0
for i in range(N):
    if x + A[i] <= W:
        for j in range(A[i]):
            res[y][x + j] = i + 1
            
        x = x + A[i]
            
    else:
        for j in range(x, W):
            res[y][j] = i + 1
            
        y = y + 1
        
        for j in range(A[i] - (W - x) + 1):
            res[y][j] = i + 1
            
        x = 0
        
for i in range(H):
    result = ''
    for j in range(W):
        result =  result + str(res[i][j])
        if j != W - 1:
            result += ' '
        
    print(result)

Submission status: Runtime Error

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

nc=1
nct=A[0]
ans=[]
for y in range(H):
  if y%2==0:
    st=""
    for x in range(W):
      nct-=1
      st+=str(nc)
      if nct==0 and nc!=N:
        nct=A[nc]
        nc+=1
  else:
    st=""
    for x in range(W-1,-1,-1):
      nc-=1
      st+=str(nc)
      if nct==0 and nc!=N:
        nct=A[nc]
        nc+=1
    st="".join(list(reversed(st)))
  ans.append(st)
        
      
for y in range(H):
  print(ans[y])

Submission status: Runtime Error

Solution code:
H, W = (int(i) for i in input().split())
N = int(input())
A = [int(i) for i in input().split()]
C = []
for i in range(N):
  for j in range(A[i]):
    C.append(i+1)
ans = [[0]*W for i in range(H)]
for i in range(H*W):
  y = i // W
  if y%2==0:
    x = i%W
  else:
    x = W-1-i%W
  ans[x][y] = C[i]
for i in range(H):
  ans[i] = [str(a) for a in ans[i]]
  print(" ".join(ans[i]))

Submission status: Runtime Error

Solution code:
H, W = (int(i) for i in input().split())
N = int(input())
A = [int(i) for i in input().split()]
C = []
for i in range(N):
  for j in range(a[i]):
    C.append(i+1)
ans = [[0]*W for i in range(H)]
for i in range(HW):
  y = i // W
  if y%2==0:
    x = i%W
  else:
    x = W-1-i%W
  ans[x][y] = C[i]
for i in range(H):
  ans[i] = [str(a) for a in ans]
  print(" ".join(ans[i]))

Submission status: Runtime Error

Solution code:
a,b= map(int,input().split(" "))
b = int(input())
ar = list(map(int,input().split(" ")))
br = [[0 for i in range(b)] for j in range(a)]
go = "M"
ap = 1
for i in range(a):
    if go == "M":
        for j in range(b):
            if ar[0] == 0:
                del ar[0]
                ap += 1
            br[i][j] = ap
            ar[0] -= 1
        go = "U"
    else:
        for j in range(b-1,-1,-1):
            if ar[0] == 0:
                del ar[0]
                ap += 1
            br[i][j] = ap
            ar[0] -= 1
for r in br:
    for i in range(b-1):
        print(r[i],end=" ")
    print(r[b-1])

Submission status: Runtime Error

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

c = [[0 for i in range(w)] for j in range(h)]
fromL = True
x = 0
y = 0

for i in range(1, n+1) :
    for j in range(a[i-1]) :
        c[y][x] = i
        if w == 1 :
            y += 1
        elif j > 0 and x == w - 1 :
            y += 1
            fromL = False
        elif j > 0 and x == 0 :
            y += 1
            fromL = True
        elif fromL :
            x += 1
        else :
            x -= 1

for ci in c :
    print(*ci)

Submission status: Runtime Error

Solution code:

def draw(i):
    global p,q,hd,wd
    B[p][q] = i
    if (q < W-1 and hd != -1):
        hd = 1
        wd = 0
    elif (hd == 1 and q == W-1) or (hd == -1 and q == 0):
        hd = 0
        wd = 1
    else:
        hd = -1
        wd = 0
    p += wd
    q += hd

if __name__ == '__main__':
    p = 0
    q = 0
    hd = 1
    wd = 0
    H, W = list(map(int, input().split()))
    N = int(input())
    As = list(map(int, input().split()))
    B = [[0 for i in range(W)] for j in range(H)]

    for i in range(N):
        for j in range(As[i]):
            draw(i+1)

    for i in range(H):
        s = list(map(str, B[i]))
        print(' '.join(s))

Submission status: Runtime Error

Solution code:

def draw(i):
    global p,q,hd,wd
    B[p][q] = i
    if (q < W-1 and hd != -1):
        hd = 1
        wd = 0
    elif (hd == 1 and q == W-1) or (hd == -1 and q == 0):
        hd = 0
        wd = 1
    else:
        hd = -1
        wd = 0
    p += wd
    q += hd

if __name__ == '__main__':
    p = 0
    q = 0
    hd = 1
    wd = 0
    H, W = list(map(int, input().split()))
    N = int(input())
    As = list(map(int, input().split()))
    B = [[0 for i in range(W)] for j in range(H)]

    for i in range(N):
        for j in range(As[i]):
            draw(i+1)

    for i in range(H):
        s = list(map(str, B[i]))
        print(' '.join(s))

Submission status: Runtime Error

Solution code:
def draw(i):
    global p,q,hd,wd
    B[p][q] = i
    if (q < W-1 and hd != -1):
        hd = 1
        wd = 0
    elif (hd == 1 and q == W-1) or (hd == -1 and q == 0):
        hd = 0
        wd = 1
    else:
        hd = -1
        wd = 0

    p += wd
    q += hd

if __name__ == '__main__':
    p = 0
    q = 0
    hd = 1
    wd = 0
    H, W = list(map(int, input().split()))
    N = int(input())
    As = list(map(int, input().split()))
    B = [[0 for i in range(W)] for j in range(H)]

    for i in range(N):
        for j in range(As[i]):
            draw(i+1)

    for i in range(H):
        s = list(map(str, B[i]))
        print(' '.join(s))

Submission status: Runtime Error

Solution code:
h, w = map(int, raw_input().split())
n = int(raw_input())
aa = map(int, raw_input().split())
p = dict()

for a in aa:
    if a in p:
        p[a] += a
    else:
        p[a] = a

b = [[None]*w]*h
print p

f = True
ks = set(p.keys())
for i in range(h):
    for j in range(w):
        c = ks[0]
        if f:
            b[i][w-j] = c
        else:
            b[i][j] = c
        p[c] -= 1
        if p[c] == 0:
            ks.remove(c)
    f = not f

for i in range(h):
    for j in range(w):
        print b[i][j],
    print

Submission status: Runtime Error

Solution code:
def grid_coloring():
	h,w = map(int, raw_input().split(" "))
	n = int(raw_input())
	a = map(int, raw_input().split(" "))
	_current_color = 1
	ans = np.zeros((h,w), dtype=int)
	for i in range(h):
		if i%2==0:
			for j in reversed(range(w)):
				if a[_current_color-1]!=0:
					ans[i][j]=_current_color
					a[_current_color - 1]-=1
				else:
					_current_color +=1
					ans[i][j] = _current_color
					a[_current_color - 1] -= 1
		else:
			for j in range(w):
				if a[_current_color-1]!=0:
					ans[i][j]=_current_color
					a[_current_color - 1]-=1
				else:
					_current_color +=1
					ans[i][j] = _current_color
					a[_current_color - 1] -= 1
	for i in range(h):
		print " ".join(map(str, ans[i]))


if __name__ == "__main__":
	grid_coloring()

Submission status: Runtime Error

Solution code:
[H,W]=[int(x) for x in raw_input().split()]
N=int(raw_input())
A=[int(x) for x in raw_input().split()]
B=[[0 for x in range(W)] for y in range(H)]
p=0
q=0
d=1
k=0
for i in range(1,N+1):
    for j in  range(A[i-1]):
        B[p][(q+k)*d]=i
        q+=1
        if q==N:
            p+=1
            q=0
            d*=-1
            k= not k
for i in range(H):
    print "".join([str(x) for x in B[i]])