Problem:
 Score : 400 points    Problem Statement We have a grid with H rows and W columns of squares.
Snuke is painting these squares in colors 1 , 2 , ... , N .
Here, the following conditions should be satisfied:   For each i ( 1 ≤ i ≤ N ), there are exactly a_i squares painted in Color i . Here, a_1 + a_2 + ... + a_N = H W .  For each i ( 1 ≤ i ≤ N ), the squares painted in Color i are 4-connected . That is, every square painted in Color i can be reached from every square painted in Color i by repeatedly traveling to a horizontally or vertically adjacent square painted in Color i .   Find a way to paint the squares so that the conditions are satisfied.
It can be shown that a solution always exists.      Constraints  1 ≤ H, W ≤ 100  1 ≤ N ≤ H W  a_i ≥ 1  a_1 + a_2 + ... + a_N = H W         Input Input is given from Standard Input in the following format:  H  W  N  a_1  a_2  ...  a_N       Output Print one way to paint the squares that satisfies the conditions.
Output in the following format:  c_{1 1}  ...  c_{1 W}  :  c_{H 1}  ...  c_{H W}   Here, c_{i j} is the color of the square at the i -th row from the top and j -th column from the left.        Sample Input 1 2 2
3
2 1 1      Sample Output 1 1 1
2 3  Below is an example of an invalid solution:  1 2
3 1  This is because the squares painted in Color 1 are not 4-connected.       Sample Input 2 3 5
5
1 2 3 4 5      Sample Output 2 1 4 4 4 3
2 5 4 5 3
2 5 5 5 3       Sample Input 3 1 1
1
1      Sample Output 3 1

Solution (Python):


import sys

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

now_n = 0

for y in range(HW[0]):
	n_array = []
	for x in range(HW[1]):
		if a[now_n] > 0:
			n_array.append(now_n+1)
			a[now_n] -= 1
		else:
			now_n += 1
			n_array.append(now_n+1)
			a[now_n] -= 1
	if y%2 == 1:
		n_array.reverse()
	for pi in n_array:
		sys.stdout.write(str(pi) + " ")
	sys.stdout.write("\n")

Solution (Python):



hw = raw_input()
hw = hw.split()
hw = map(int,hw)

n = raw_input()
n = int(n)

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

ans = []

i = 0
for j in a:
    for k in range(j):
        ans.append(i+1)
    i += 1

import numpy as np

ans = np.array(ans)
ans = ans.reshape([hw[0],hw[1]])

i = 0
while i < hw[0]:
    if i % 2 == 0:
        ans[i] = ans[i][::-1]
    i += 1

for line in ans:
    for i in line:
        print str(i),
    print ""

Solution (Python):
h, w = [int(x) for x in input().split()]
n = int(input())
a = [int(x) for x in input().split()]

matrix = []
for _y in range(h):
    row = []
    for _x in range(w):
        row.append(-1)
    matrix.append(row)

cells = h * w
x, y = 0, 0
dx = 1

for idx, ccount in enumerate(a):
    color = idx + 1
    for _ in range(ccount):
        matrix[y][x] = str(color)
        

        if x == w - 1 and dx == 1:
            y += 1
            dx = -1
        elif x == 0 and dx == -1:
            y += 1
            dx = 1
        else:
            x = x + dx

for row in matrix:
    print(' '.join(row))

Solution (Python):
def d():
    h, w = map(int, input().split())
    n = int(input())
    a = [int(ai) for ai in input().split()]
    s = ''
    tmp = ''
    for i in range(h*w):
        if (i // w) % 2 == 0:
            
            
            tmp += '{'+str(i)+'}'
            if i%w == w-1:
                tmp += '\n'
                s += tmp
                tmp = ''
            else:
                tmp += ' '
        else:
            
            
            tmp = '{'+str(i)+'}' + tmp
            if i%w == w-1:
                tmp += '\n'
                s += tmp
                tmp = ''
            else:
                tmp = ' ' + tmp
    aa = []
    for i, n in enumerate(a):
        aa += [i+1]*n
    print(s.format(*aa)[:-1])

d()

Solution (Python):
import numpy as np
input_line = map(int,raw_input().split(" "))
H = input_line[0]
W = input_line[1]
N = int(raw_input())
input_line = raw_input()
a = map(int,input_line.split(" "))
square = np.zeros((H,W))
k = 0
for j in range(H):
	for i in range(W):
		if j % 2 == 0:
			square[j][i] = k+1
			a[k] -= 1
		else :
			square[j][W-i-1] = k+1
			a[k] -= 1
		if a[k] == 0:
			k += 1

txt = ""
for s in square:
	for number,ss in enumerate(s):
		if number != len(s) - 1:
			txt += str(int(ss)) + " "
		else:
			txt += str(int(ss))
	print txt
	txt = ""

Solution (Python):
rc = input()
H, W = rc.split()
N = input()
colors = input()
cs = colors.split()

dim1_len = [1 for i in range(int(H)*int(W))]

count = 0
output = [[1 for i in range(int(W))] for j in range(int(H))]

for i in range(int(N)):
    n = int(cs[i])
    for j in range(n):
        dim1_len[count] = i+1
        count += 1

for i in range(int(H)):
    for j in range(int(W)):
        if i % 2 == 0:
            output[i][j] = dim1_len[i*int(W)+j]
        else:
            output[i][int(W)-j-1] = dim1_len[i*int(W)+j]
        
for i in range(len(output)):
    for j in range(len(output[0])):
        print(output[i][j],end=" ")
    print("\n")

Solution (Python):


import sys

def main(argv):
    line = sys.stdin.readline()
    while line:
        H, W = map(lambda x: int(x), line.split(" ", 2))
        N = int(sys.stdin.readline())
        a = map(lambda x: int(x), sys.stdin.readline().split(" ", N))
        
        color = []
        for n, i in enumerate(a):
            for j in xrange(i):
                color.append(str(n + 1))
        for i in xrange(H):
            if i % 2 == 0:
                print(" ".join(color[i * W:(i + 1) * W]))
            else:
                print(" ".join(reversed(color[i * W:(i + 1) * W])))

            

            
        line = sys.stdin.readline()

if __name__ == "__main__":
    main(sys.argv)

Solution (Python):
def printlist(l):
    s = str(l[0])
    for i in range(len(l)-1):
        s += " " + str(l[i+1])
    print(s)
h, w = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))
clist = []
for i in range(n):
    clist += [i+1 for j in range(a[i])]
for i in range(h):
    if i%2 == 0:
        printlist(clist[i*w:(i+1)*w])
    else:
        printlist(clist[(i+1)*w-1:i*w-1:-1])

Solution (Python):
import numpy as np

h, w = map(int, raw_input().split())
N = int(raw_input())
l = np.array(map(int, raw_input().split()))

z = np.zeros((h * w, ), dtype=np.int64)

c = 0
for i in range(0, N):
    z[c:c + l[i]] = i + 1
    c += l[i]

for k in range(0, h):
    if k % 2 == 0:
        print str(z[k * w:k * w + w])[1:-1]
    else:
        print str(z[k * w:k * w + w][::-1])[1:-1]

Solution (Python):
def flatten(nested_list):
    return [e for inner_list in nested_list for e in inner_list]

H, W = map(int, input().split())
N = int(input())
a = list(map(int, input().split()))
l = flatten([[i+1] * a[i] for i in range(N)])
l2 = [l[i:i + W] for i in range(0, len(l), W)]
for i in range(H):
    if not i % 2:
        print(" ".join(map(str,l2[i])))
    else:
        print(" ".join(map(str, list(reversed(l2[i])))))

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

ans = [[0 for i in range(w)]for j in range(h)]
x = 0
y = 0
rev = 0
for i in range(len(a)):
    while(a[i]):
        ans[y][x] = str(i+1)
        a[i] -= 1
        if rev == 1:
            if x - 1 < 0:
                rev = 0
                y += 1
            else:
                x -= 1
        else:
            if x + 1 == w:
                y += 1
                rev = 1
            else:
                x += 1
for i in range(h):
    print(' '.join(ans[i]))

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

line = []
ass = True
for i, a in enumerate(an, start=1):
    for j in range(a):
        line.append(i)
        if len(line) == W:
            if ass is False:
                ass = True
                line = reversed(line)
            else:
                ass = False
            print(' '.join(map(str, line)))
            line = []

Solution (Python):
h, w = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))
c = [[0 for j in range(w)] for i in range(h)]
x, y = 0, 0
dx = 1

for k in range(n):
    for l in range(a[k]):
        c[y][x] = k + 1
        nx = x + dx
        if nx >= w:
            dx = - 1
            y += 1
        elif nx < 0:
            dx = 1
            y += 1
        else:
            x = nx

for i in range(h):
    for j in range(w):
        print(c[i][j], end = "\n" if j == w - 1 else " ")

Solution (Python):

"""
Created on Sun Aug  6 21:37:48 2017

@author: syaga
"""

if __name__ == "__main__":
    H, W = list(map(int, input().split()))
    N = int(input())
    a = list(map(int, input().split()))
    lis = [[None]*W for i in range(H)]
    ins = 1
    for i in range(H):
        for j in range(W):
            if a[0] == 0:
                del a[0]
                ins += 1
            lis[i][j] = ins
            a[0] -= 1
    for i in range(H//2):
        lis[2*i+1].reverse()
    for i in lis:
        print(" ".join(map(str, i)))

Solution (Python):

H, W = map(int, input().split(' '))
N = int(input())
A = list(map(int, input().split(' ')))
M = [[0]*W for i in range(H)]
color = 1
num = 0
for i in range(H):
    for j in range(W):
        if i % 2 != 0:
            j = W - j - 1
        M[i][j] = color
        num += 1 
        if num == A[color-1]:
            color += 1
            num = 0
for i in range(H):
    print(' '.join(list(map(str, M[i]))))

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

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

plate_as_line = []

color = 1
for i in a:
    for j in range(i):
        plate_as_line.append(color)
    color += 1

start_index = 0
end_index = w
line_reversed = False
for i in range(h):
    if line_reversed:
        print(' '.join(map(str, reversed(plate_as_line[start_index:end_index]))))
        line_reversed = False
    else:
        print(' '.join(map(str, plate_as_line[start_index:end_index])))
        line_reversed = True
    start_index += w
    end_index += w

Solution (Python):
'''input
3 5
5
1 2 3 4 5
'''
h , w = map(int ,raw_input().split())
al = input()
a = map(int ,raw_input().split())
lia = []
for i in range(al):
	lia += [i+1]*a[i]
ansl = []
liac = 0
for i in range(h):
	temp = []
	for j in range(w):
		temp.append(lia[liac])
		liac += 1
	if i%2 == 0:
		ansl.append(temp)
	else:
		ansl.append(list(reversed(temp)))
for i in range(h):
	for j in range(w):
		print ansl[i][j],
	print

Solution (Python):


H, W = map(int, input().split())
N = int(input())
A = list(map(int, input().split()))
C = []
for i, j in enumerate(A):
    C += [str(i+1)] * j
for i in range(H):
    if i % 2 == 0:
        print(' '.join(C[i*W : i*W+W]))
    else:
        print(' '.join(C[i*W : i*W+W][::-1]))

Solution (Python):



def main():
    h, w = (int(x) for x in input().split())
    n = int(input())
    ax = [int(x) for x in input().split()]

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

    for i in range(h):
        if i % 2 == 0:
            items = range(0, w)
        else:
            items = range(w-1, -1, -1)

        for j in items:
            output[i][j] = cur_color + 1

            ax[cur_color] -= 1
            if ax[cur_color] == 0:
                cur_color += 1
    
    for i in range(h):
        for j in range(w):
            print(output[i][j], end=" ")
        print()


if __name__ == "__main__":
    main()

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

ans = []
for i in range(N):
    for j in range(A[i]):
        ans.append(i + 1)

for i in range(H):
    use = (ans[i*W:(i+1)*W])[::(-1)**i]
    for i in range(W):
        if not(i == W -1):
            print(use[i], end=" ")
        else:
            print(use[i])

Solution (Python):

 
def main():
	h, w = list(map(int, input().split()))
	n = int(input())
	nums = list(map(int, input().split()))
	calc(h, w, n, nums)

def calc(h, w, n, nums):
	out_raw = []
	for i, num in enumerate(nums):
		out_raw.extend([str(i + 1) for _ in range(num)])

	for i in range(h):
		tmp = out_raw[i * w:(i + 1) * w]
		if i % 2 == 0:
			print(" ".join(tmp))
		else:
			print(" ".join(tmp[::-1]))

if __name__ == '__main__':
	main()

Solution (Python):

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

from itertools import chain
b = list(chain.from_iterable( [[i+1]*a[i] for i in range(N)] ))

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

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

Solution (Python):
h,w=map(int,raw_input().split())
n=int(raw_input())
a=map(int,raw_input().split())
b=[]
for j,x in enumerate(a) :
    for i in range(x) :
        b.append(j+1)
for i in range(h) :
    s=b[0+i*w:w+i*w]
    if i%2==0 :
        print ' '.join(['%i'%j for j in s])
    else :
        print ' '.join(['%i'%j for j in reversed(s)])

Solution (Python):
h,w = input().split()
h = int(h)
w = int(w)
n = int(input())
a_list = input().split()
a_list = [int(a) for a in a_list]

c_list = []
for i,a in enumerate(a_list):
    for j in range(a):
        c_list.append(i+1)
        
for i in range(h):
    p_list = c_list[w*i:w*(i+1)]
    p_list = [str(p) for p in p_list]
    if i % 2 == 0:
        print(" ".join(p_list))
    else:
        print(" ".join(p_list[::-1]))

Solution (Python):
h, w = input().split()
h = int(h)
w = int(w)

n = input()
n = int(n)

an = input().split()
an = [int(a) for a in an]

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

k = 0
for i in range(h):
    for j in range(w):
        if i % 2 != 0:
            j = -j - 1
        if an[k] > 0:
            pass
        else:
            k += 1
        an[k] -= 1
        field[i][j] = k+1

for i in range(h):
    output = [str(c) for c in field[i]]
    print(" ".join(output))

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

hw = []

for i, ai in enumerate(a):
    for j in range(ai):
        hw.append(i+1)
        
hw = np.asarray(hw)
HW = np.reshape(hw, (H, W))

for i in range(H):

    if i % 2 == 1:
        print(*HW[i][::-1])
    else:
        print(*HW[i])

Solution (Python):
h,w=map(int,raw_input().split())
n=input()
s=raw_input().split()
a=[0 for i in range(n)]
ans=[]
for i in range(n):
    a[i]=int(s[i])
    for j in range(a[i]):
        ans.append(i+1)

p=0
for i in range(h):
    if i%2==0:
        for j in range(i*w,i*w+w):
            print ans[j],
    else:
        for j in range(i*w+w-1,i*w-1,-1):
            print ans[j],
    print