Scanf.scanf "%d %d %d" (fun h w n ->
    let a = Array.init n (fun _ -> Scanf.scanf " %d" (fun a -> a)) in
    let mat = Array.make_matrix h w 0 in
    let rec loop y x i r =
        if r > 0 then (
            mat.(y).(x) <- i;
            let vx = if y mod 2 = 0 then 1 else -1 in
            let x = x + vx in
            let y, x = if x >=0 && x < w then y, x else y + 1, x - vx in
            loop y x i (r - 1)
        ) else (
            if i = n then () else loop y x (i + 1) a.(i)
        )
    in
    loop 0 0 0 0;
    Array.iter (fun a ->
        Array.iter (Printf.printf "%d ") a;
        print_newline ()
    ) mat
)