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 Rust (1.15.1) (judge verdict: Accepted)

#![allow(unused_imports, unused_variables, dead_code, non_snake_case, unused_macros)]
use std::io::{stdin, Read, StdinLock};
use std::str::FromStr;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

fn getline() -> String{
    let mut res = String::new();
    std::io::stdin().read_line(&mut res).ok();
    res
}

macro_rules! readl {
    ($t: ty) => {
        {
            let s = getline();
            s.trim().parse::<$t>().unwrap()
        }
    };
    ($( $t: ty),+ ) => {
        {
            let s = getline();
            let mut iter = s.trim().split(' ');
            ($(iter.next().unwrap().parse::<$t>().unwrap(),)*) 
        }
    };
}

macro_rules! readlvec {
    ($t: ty) => {
        {
            let s = getline();
            let iter = s.trim().split(' ');
            iter.map(|x| x.parse().unwrap()).collect::<Vec<$t>>()
        }
    }
}

macro_rules! mvec {
    ($v: expr, $s: expr) => {
        vec![$v; $s]
    };
    ($v: expr, $s: expr, $($t: expr),*) => {
        vec![mvec!($v, $($t),*); $s]
    };
}

macro_rules! debug {
    ($x: expr) => {
        println!("{}: {:?}", stringify!($x), $x)
    }
}

fn printiter<'a, T>(v: &'a T)
where
    &'a T: std::iter::IntoIterator, 
    <&'a T as std::iter::IntoIterator>::Item: std::fmt::Display {
    for (i,e) in v.into_iter().enumerate() {
        if i != 0 {
            print!(" ");
        }
        print!("{}", e);
    }
    println!("");
}

struct ContestPrinter {
    s: String,
}

impl ContestPrinter {
    fn new() -> ContestPrinter {
        ContestPrinter {
        s: String::new(),
        }
    }

    fn print<T>(&mut self, x: T)
        where T: std::fmt::Display {
        self.s.push_str(format!("{}", x).as_str());
    }

    fn println<T>(&mut self, x: T)
        where T: std::fmt::Display {
        self.s.push_str(format!("{}\n", x).as_str());
    }
}

impl std::ops::Drop for ContestPrinter {
    fn drop(&mut self) {
        print!("{}", self.s);
    }
}

// euclidの互除法
fn gcd(a: i64, b: i64) -> i64 {
    if b == 0 { a }
    else { gcd(b, a % b) }
}
// ab = lcm(a, b)gcd(a, b)
fn lcm(a: i64, b: i64) -> i64 {
    let g = gcd(a, b);
    (a / g) * b
}
// 約数列挙 昇順ではない
fn divisors(n: i64) -> Vec<i64> {
    let mut ret = Vec::new();
    if n == 1 { ret.push(1); return ret; }
    let mut i = 1;
    loop {
        if (n % i) == 0 {
            ret.push(i);
            if (i != 1) & ((i*i) != n) {
                ret.push(n / i);
            }
        }
        i += 1; if n < (i*i) { break; }
    }
    ret.push(n);
    ret
}

fn is_max_i64(num: i64) -> bool { if num == i64::max_value() { true } else { false } }

// 2部グラフかどうか，片方の色の数，もう片方の色の数
fn is_bipartite(graph: &Vec<Vec<usize>>) -> (bool, usize, usize) {
    let size = graph.len();
    let mut color: Vec<i32> = vec![0; size];
    fn dfs(g: &Vec<Vec<usize>>, color: &mut Vec<i32>, v: usize, c: i32) -> bool {
        color[v] = c;
        for i in 0..g[v].len() {
            let nb = g[v][i];
            let nbc = color[nb];
            if nbc == c { return false; }
            if nbc == 0 { if !dfs(g, color, nb, -c) { return false; } }
        }
        return true;
    }
    for i in 0..size {
        if color[i] == 0 { if !dfs(graph, &mut color, i, 1) { return (false, 0, 0); } }
    }
    
    let (mut c1, mut c2) = (0, 0);
    for c in color { match c {
        1 => c1 += 1,
        _ => c2 += 1,
    } }
    (true, c1, c2)
}

#[derive(Debug)]
struct UnionFind {
    parent: Vec<usize>,
    rank: Vec<usize>,
}

impl UnionFind {
    fn new(size: usize) -> UnionFind {
        UnionFind{ parent: (0..size).collect(), rank: vec![0; size] }
    }

    fn find_root(&mut self, index: usize) -> usize {
        let p = self.parent[index];
        if p == index {
            return index;
        } else {
            self.parent[index] = self.find_root(p);
            return self.parent[index];
        }
    }

    // xとyの属する木を併合
    fn unite(&mut self, x: usize, y: usize) {
        let x = self.find_root(x);
        let y = self.find_root(y);
        if x != y {
            if self.rank[x] < self.rank[y] {
                self.parent[x] = y;
            } else {
                self.parent[y] = x;
                if self.rank[x] == self.rank[y] { self.rank[x] += 1; }
            }
        }
    }

    fn same(&mut self, x: usize, y: usize) -> bool {
        self.find_root(x) == self.find_root(y)
    }
}

// 無向グラフ　最小全域木
struct MinimumSpanningTree {
    pub node_num: usize,
    pub edges: Vec<(i64, usize, usize)>, // cost, node_idx, node_idx
}

impl MinimumSpanningTree {
    fn new(size: usize) -> MinimumSpanningTree {
        MinimumSpanningTree{ node_num: size, edges: Vec::new() }
    }

    // O(|E|log|V|)
    fn calc(&mut self) -> i64 {
        self.edges.sort();
        let mut uf = UnionFind::new(self.node_num);
        let mut sum = 0;
        for i in 0..self.edges.len() {
            let (cost, u, v) = self.edges[i];
            if uf.same(u, v) { continue; }
            uf.unite(u, v);
            sum += cost;
        }
        sum
    }
}

fn main() {
    let mut pr = ContestPrinter::new();
    
    let (H, W) = readl!(usize, usize);
    let N = readl!(usize);
    let an = readlvec!(usize);
    let mut out = vec![vec![0; W]; H];
    let mut cnt = 0;
    let mut idx = 0;
    for i in 0..H {
        if i % 2 == 0 {
            for j in 0..W {
                out[i][j] = idx + 1;
                cnt += 1;
                if cnt == an[idx] {
                    cnt = 0;
                    idx += 1;
                }
            }
        } else {
            for j in (0..W).rev() {
                out[i][j] = idx + 1;
                cnt += 1;
                if cnt == an[idx] {
                    cnt = 0;
                    idx += 1;
                }
            }
        }
    }

    for i in 0..H {
        for j in 0..W {
            if j == (W-1) {
                pr.println(out[i][j]);
            } else {
                pr.print(out[i][j]);
                pr.print(" ");
            }
        }
    }
}