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 in C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using static Input;

public class Prog
{
    private const int INF = 1000000007;
    private const long INFINITY = 9223372036854775807;
    private struct Pair { public int x, y; }
    public static void Main()
    {
        //input
        int H = NextInt();
        int W = NextInt();
        int N = NextInt();
        int[] a = LineInt();
        PriorityQueue<int, int> PQ = new PriorityQueue<int, int>();
        for (int i = 0; i < N; i++)
            for (int j = 0; j < a[i]; j++) PQ.Add(i + 1, i + 1);
        int[,] ans = new int[H, W];
        for (int i = 0; i < H; i++)
        {
            bool f = i % 2 == 0;
            for (int j = (f ? 0 : W - 1); j != (f ? W : -1); j += (f ? 1 : -1))
            {
                ans[i, j] = PQ.Take();
            }
        }

        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) Console.Write(ans[i,j]+" ");
            Console.WriteLine();
        }
    }
}


public class PriorityQueue<Tkey, Tvalue> where Tkey : IComparable
{
    private static List<Tkey> keys = new List<Tkey>();
    private static List<Tvalue> values = new List<Tvalue>();
    private int size = 0;
    private static bool Descending;
    private static int Compare(Tkey a, Tkey b) { return (Descending ? a.CompareTo(b) : b.CompareTo(a)); }

    public PriorityQueue(bool b = true) { Clear(); Descending = b; }
    public int Count() { return size; }
    public void Clear() { keys = new List<Tkey>(); values = new List<Tvalue>(); size = 0; }
    public void Add(Tkey e, Tvalue v)
    {
        keys.Add(e);
        values.Add(v);
        int x = size;
        size += 1;
        while (x != 0)
        {
            int par = (x % 2 == 0 ? x - 2 : x - 1) / 2;
            if (Compare(keys[par], e) <= 0) break;
            Tkey a = keys[x]; keys[x] = keys[par]; keys[par] = a;
            Tvalue b = values[x]; values[x] = values[par]; values[par] = b;
            x = par;
        }
    }
    public void Poll()
    {
        Tkey k = keys[size - 1];
        Tvalue v = values[size - 1];
        int x = 0;
        size--;
        if (size == 0) { Clear(); return; }
        keys.RemoveAt(size);
        values.RemoveAt(size);
        while (true)
        {
            int l = x * 2 + 1;
            int r = x * 2 + 2;
            if (size - 1 < l) break;
            int y = 0;
            if (size - 1 == l) { y = l; }
            else if (Compare(keys[l], keys[r]) == 1) { y = r; }
            else { y = l; }
            if (Compare(keys[y], k) > 0) break;
            keys[x] = keys[y];
            values[x] = values[y];
            x = y;
        }
        keys[x] = k;
        values[x] = v;
    }
    public Tvalue Take() { Tvalue ret = values[0]; Poll(); return ret; }
    public Tvalue Peek() { return values[0]; }
}

public class Input
{
    private static Queue<string> q = new Queue<string>();
    private static void Confirm() { if (q.Count == 0) foreach (var s in Console.ReadLine().Split(' ')) q.Enqueue(s); }
    public static int NextInt() { Confirm(); return int.Parse(q.Dequeue()); }
    public static long NextLong() { Confirm(); return long.Parse(q.Dequeue()); }
    public static char NextChar() { Confirm(); return char.Parse(q.Dequeue()); }
    public static string NextString() { Confirm(); return q.Dequeue(); }
    public static double NextDouble() { Confirm(); return double.Parse(q.Dequeue()); }
    public static int[] LineInt() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); }
    public static long[] LineLong() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); }
    public static string[] LineString() { return Console.ReadLine().Split(' ').ToArray(); }
    public static double[] LineDouble() { return Console.ReadLine().Split(' ').Select(double.Parse).ToArray(); }
}