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 Java8 (OpenJDK 1.8.0) (judge verdict: Runtime Error)

package reg.c80.D;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;

public class Main {
    private static String solve(Scanner scanner) {
        Main solver = new Main();
        int[][] ret = solver.solveInner(scanner);
        return solver.getRetString(ret);
    }

    private int[] a;
    private int ia;
    private int N;
    private int HW;

    private int[][] solveInner(Scanner scanner) {
        String line = scanner.nextLine();
        int H = Integer.parseInt(line.split(" ")[0]);
        int W = Integer.parseInt(line.split(" ")[1]);
        N = Integer.parseInt(scanner.nextLine());
        a = lineTointNums(scanner.nextLine());
        ia = 0;
        HW = H * W;

        int[][] ret =  new int[H][W];

        int rowMin = 1;
        int colMin = 0;
        int rowMax = H;
        int colMax = W;

        int colDir = 1;
        int rowDir = 0;

        int i = 0;
        int j = 0;

        while (true) {
//            if (rowMin == rowMax && colMin == colMax) break;

            mark(i, j, ret);
            HW --;
            if (HW == 0) return ret;

            j += colDir;
            i += rowDir;

            if (i == rowMax && rowDir == 1) {
                i--;
                j--;
                rowMax--;
                rowDir = 0;
                colDir = -1;
            } else if (j == colMax && colDir == 1) {
                j--;
                i++;
                colMax--;
                rowDir = 1;
                colDir = 0;
            } else if (i == rowMin - 1 && rowDir == -1) {
                i++;
                j++;
                rowMin++;
                rowDir = 0;
                colDir = 1;
            } else if (j == colMin - 1 && colDir == -1) {
                j++;
                i--;
                colMin++;
                rowDir = -1;
                colDir = 0;
            }

            System.out.println(getRetString(ret));
        }

//        return ret;
    }

    private String getRetString(int[][] ret) {
        StringBuilder sb = new StringBuilder();
        for (int[] row: ret) {
            for (int col: row) {
                sb.append(col);
            }
            sb.append("\n");
        }

        return new String(sb);
    }

    private void mark(int i, int j, int[][] ret) {
        if (a[ia] == 0) {
            ia ++;
        }
        ret[i][j] = ia + 1;
        a[ia] --;
    }

    public static void main(String[] args) {
//        tests();

        Scanner scanner = new Scanner(System.in);
        System.out.println(solve(scanner));
    }

    private static String ex1 = "2 2\n" +
            "3\n" +
            "2 1 1";
    private static String ex2 = "3 5\n" +
            "5\n" +
            "1 2 3 4 5";
    private static String ex3 = "1 1\n" +
            "1\n" +
            "1";

    private static void tests() {
        System.out.println(solve(new Scanner(testCaseToInputStream(ex1))));
        System.out.println(solve(new Scanner(testCaseToInputStream(ex2))));
        System.out.println(solve(new Scanner(testCaseToInputStream(ex3))));
    }

    private static ByteArrayInputStream testCaseToInputStream(String ex) {
        try {
            return new ByteArrayInputStream(ex.getBytes("utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    private static int[] lineTointNums(String line) {
        String[] numStrs = line.split(" ");
        int[] nums = new int[numStrs.length];
        for (int i = 0; i < nums.length; i ++) {
            nums[i] = Integer.parseInt(numStrs[i]);
        }
        return nums;
    }
}