We need to produce a grid painting with H rows, W columns, N colors, each color i appears exactly a_i times, and each color's squares are 4-connected (i.e., the set forms a connected region under orthogonal adjacency). It is guaranteed that a solution exists.
We need to output any valid configuration.
Approach: This is a typical problem. We can fill the grid in a snake-like pattern (alternating rows) with numbers 1..N according to their counts, ensuring that each color's cells form a contiguous block? But the colors need to be connected, not necessarily a rectangle. However, if we fill row by row sequentially (like left to right, top to bottom) assigning colors in order, each color's cells might be split across rows and not necessarily connected. For example, if a color appears in two separate rows separated by other colors, might be disconnected. But we can arrange them in a connected way by ensuring that cells of each color are contiguous in the scanning order if we traverse in a snake pattern that ensures connectivity? Actually, if we fill the grid in a single continuous path (like a Hamiltonian path) and assign colors in contiguous segments along that path, then each color's cells will be contiguous along the path, and the path being a connected set in the grid, the cells for each color will be connected as a subpath? However, a contiguous segment along a path that visits all cells ensures that the set of cells assigned to a given color is a contiguous subpath, which is a connected set under adjacency (since the path steps are adjacent). Because the path visits adjacent cells, the subpath is itself a connected region (since it's a continuous sequence of adjacent cells). But careful: If the path goes back and forth, the cells in the segment are all connected along the path; they form a connected subgraph (since each consecutive pair is adjacent). However, the path may revisit cells? No, it's a simple path covering all cells exactly once (Hamiltonian path). In a grid, a Hamiltonian path exists (snake path). So if we order the cells along a snake path (say row 1 left-to-right, row 2 right-to-left, etc.) then assign color 1 to first a_1 cells, color 2 to next a_2 cells, etc., then each color's cells form a contiguous block along the snake. Since each consecutive cells are adjacent, the set of cells for each color is connected (by the path adjacency). However, we must also ensure that the set is connected in the grid beyond just the path edges, but the path edges are exactly the adjacency relations between cells. The contiguous block of the path is a path itself, which is connected. So this yields a valid solution. But careful: The contiguous block might be not connected if the path is such that the block is not induced? Actually, if the block is a set of cells that are consecutive along the path, then the graph induced by these cells includes the edges connecting consecutive cells in the block, which are all present (adjacent). However, does connectivity require that every pair of cells in the set be connected via a sequence of cells all in the set? Yes, and the subpath provides that. So it's valid.
Thus the solution: traverse the grid in a snake order: for i from 1 to H: if i is odd, go from 1 to W; if i is even, go from W to 1. This yields a list of cells in order. Then create an array color assignments: for each cell in order, assign the current color, decreasing its count; when count reaches 0, move to next color.
Finally, output the grid.
But wait: Is there any case where this might break connectivity? For a contiguous segment along the snake, the set is connected, yes. So it works.
However, there is nuance: In the snake ordering, consecutive cells are adjacent horizontally or vertically. For row i even (right-to-left), when moving from the last cell of the previous row (which was row i-1, odd) to the first cell of row i (even), the adjacency: For row 1 left-to-right, ends at (1,W). Then next cell is (2,W) if we start row 2 right-to-left; (1,W) and (2,W) are adjacent vertically. So yes, the snake ordering ensures that consecutive cells are adjacent. So the set of cells for each color is a contiguous segment of this path, thus connected.
Thus solution works.
But there's a catch: The problem statement says "the squares painted in Color i are 4-connected" meaning that the subgraph induced by cells of color i is connected. For a contiguous segment of the snake, it's indeed connected. So that is valid.
Check examples: For first example H=2,W=2,N=3,a=[2,1,1]. Snake order: (1,1), (1,2), (2,2), (2,1). Assign color 1 to first 2 cells: (1,1),(1,2). Color 2 to next 1: (2,2). Color 3 to last: (2,1). That yields grid:
Row1:1 1
Row2:3 2? Wait (2,2) is color2, (2,1) is color3. So grid:
(1,1)=1, (1,2)=1
(2,1)=3, (2,2)=2
Output would be:
1 1
3 2
But example output is:
1 1
2 3
So that's a different arrangement. But both are valid? Check connectivity: In our output, color2 is only one cell at (2,2) - connected trivially. Color3 is one cell at (2,1) - connected trivially. So it's valid. The example output uses colors 1,2,3 with counts 2,1,1 and connectivity? In example: row1:1 1, row2:2 3. So color1 cells: (1,1),(1,2) adjacent -> connected. Color2: (2,1) alone -> connected. Color3: (2,2) alone -> connected. So both valid. So our method yields a valid output, may not match example but that's fine.
Second example: H=3,W=5,N=5,a=[1,2,3,4,5]. Snake order: row1 left-to-right: (1,1),(1,2),(1,3),(1,4),(1,5). row2 right-to-left: (2,5),(2,4),(2,3),(2,2),(2,1). row3 left-to-right: (3,1),(3,2),(3,3),(3,4),(3,5). Assign: color1:1 cell -> (1,1)=1. color2:2 cells -> (1,2),(1,3)=2? Actually (1,2),(1,3) are next. color3:3 cells -> (1,4),(1,5),(2,5). color4:4 cells -> (2,4),(2,3),(2,2),(2,1). color5:5 cells -> (3,1),(3,2),(3,3),(3,4),(3,5). This yields grid:
Row1:1,2,2,3,3
Row2:4,4,4,4,3? Wait: (2,1)=4, (2,2)=4, (2,3)=4, (2,4)=4? Actually assign: after color3 took (1,4),(1,5),(2,5), the next 4 cells for color4: (2,4),(2,3),(2,2),(2,1) all become 4. Then color5: (3,1),(3,2),(3,3),(3,4),(3,5) become 5. So grid:
Row1:1 2 2 3 3
Row2:4 4 4 4 3 (since (2,5)=3)
Row3:5 5 5 5 5
But example output is different. But this is valid: check connectivity: Color1 alone. Color2: cells (1,2),(1,3) adjacent horizontally -> connected. Color3: (1,4),(1,5),(2,5) - (1,4)-(1,5) adjacent, (1,5)-(2,5) adjacent -> connected. Color4: (2,1),(2,2),(2,3),(2,4) all adjacent -> connected. Color5: entire row3 all 5s -> connected. So valid.
Thus the snake method works.
But is there any scenario where connectivity fails for a color that appears in more than one row but the segment might be broken by the snake pattern? Actually, the segment is contiguous along the snake, meaning it includes cells in order, but if the snake goes back and forth, the set may be connected via the path. However, consider a case where a color's cells span across the boundary between rows, but the snake order at row transitions is vertical adjacency. So still connected.
Thus algorithm is trivial.
We need to output the grid with colors numbers.
Constraints: H,W up to 100, total cells up to 10k, N up to 10k, but a_i sum = 10k. So we can fill easily.
Implementation: read H,W,N, then list a[1..N] (maybe 0-index). Then create 2D array grid[H][W]. We'll generate order: for i in range(H): if i%2==0: columns = range(W); else: columns = reversed(range(W)). For each j, assign color index. Keep current color idx = 0, remaining = a[idx]. For each cell (i,j), assign idx+1 (since colors 1-indexed), decrement remaining. If remaining==0: idx += 1; if idx < N: set remaining = a[idx]. After loop, output grid.
Edge Cases: H=1, W=1 trivial.
Check N may be 1: all cells one color, connected trivially.
Thus done.
But wait: The problem statement says "It can be shown that a solution always exists." So this simple method indeed yields a valid solution. However, there's a nuance: Are there constraints that a_i >= 1? Yes, all a_i >= 1. So no zero.
Thus solution accepted.
But maybe there is a pitfall: If we use snake order, each color's cells are contiguous along the snake, but is it guaranteed that they are connected in the grid? For a contiguous segment of a path, the induced subgraph might have missing edges between non-consecutive cells, but connectivity only requires that any two cells are connected via a path that stays within the set. Since the path order is a Hamiltonian path, any two cells in the segment can be connected by the subpath of the Hamiltonian path that lies entirely within the segment, because the segment is contiguous, the subpath from one to the other is a sequence of cells all in the segment (the portion of the Hamiltonian path between them). However, is it possible that the Hamiltonian path visits cells in an order that is not adjacent in the grid? No, the path steps are adjacent. So the subpath is a valid path within the set. So yes, it's connected.
Thus solution is fine.
We can implement.
Now, alternative: There may be a condition that colors must be 4-connected and the snake method works. Many official solutions for similar problems (like ABC 069 B? Actually AtCoder problem "Grid Coloring" from ARC? I recall a problem: "Grid Coloring" from AtCoder (maybe ABC 113? ) but anyway.
So we'll code.
Complexities: O(H*W).
Implementation details:
Python:
def main():
import sys
input = sys.stdin.readline
H,W = map(int,input().split())
N = int(input())
a = list(map(int,input().split()))
grid = [[0]*W for _ in range(H)]
idx = 0
rem = a[0]
for i in range(H):
if i%2 == 0:
cols = range(W)
else:
cols = range(W-1, -1, -1)
for j in cols:
grid[i][j] = idx+1
rem -= 1
if rem == 0 and idx+1 < N:
idx += 1
rem = a[idx]
for row in grid:
print(' '.join(map(str,row)))
That's it.
But we must ensure that after the last cell, we don't try to access a[idx] beyond N. The condition if rem == 0 and idx+1 < N: idx += 1; rem = a[idx]. Since sum equals H*W, after the last cell rem will become zero exactly at the last cell, and idx+1 may equal N (i.e., the last color). In that case we should not increment. So it's safe.
Test with provided examples (maybe outputs differ but valid). We can run through.
First example: H=2,W=2,N=3,a=[2,1,1]. Our output: row1:1 1; row2:3 2 (since even row right-to-left: (2,1)=3 (after assigning first two cells for color1, then color2: (1,2) is second cell? Actually careful: order: i=0 even -> cols 0,1: (0,0): assign 1 rem=1; (0,1): assign 1 rem=0 -> idx becomes 1 rem=a[1]=1; i=1 odd -> cols = range(W-1,-1,-1) => j=1,0: (1,1): assign idx+1=2 rem=0 -> idx becomes 2 rem=a[2]=1; (1,0): assign idx+1=3 rem=0. So grid: row0:1,1; row1: (1,0)=3, (1,1)=2 -> row1:3 2. Output: "1 1\n3 2". That's valid.
Second example: H=3,W=5,N=5,a=[1,2,3,4,5]. Order: i=0 even: j=0..4: (0,0): color1 rem=0-> idx=1 rem=2; (0,1): color2 rem=1; (0,2): color2 rem=0-> idx=2 rem=3; (0,3): color3 rem=2; (0,4): color3 rem=1; i=1 odd: j=4,3,2,1,0: (1,4): color3 rem=0-> idx=3 rem=4; (1,3): color4 rem=3; (1,2): color4 rem=2; (1,1): color4 rem=1; (1,0): color4 rem=0-> idx=4 rem=5; i=2 even: j=0..4: (2,0): color5 rem=4; (2,1): rem=3; (2,2): rem=2; (2,3): rem=1; (2,4): rem=0. So grid: row0:1,2,2,3,3? Actually row0: (0,0)=1; (0,1)=2; (0,2)=2; (0,3)=3; (0,4)=3. Row1: (1,0)=4? Wait (1,0) assigned color4? Actually (1,0) we assigned when idx=3 and rem after (1,1) etc. Let's recalc carefully.
Better to simulate:
Initialize idx=0, rem=a[0]=1.
i=0 (even):
j=0: assign 1, rem=0 => if rem==0 and idx+1 < N (0+1<5? yes) => idx=1, rem=a[1]=2.
j=1: assign idx=1 => 2, rem=1
j=2: assign idx=1 => 2, rem=0 => idx=2, rem=a[2]=3.
j=3: assign idx=2 => 3, rem=2
j=4: assign idx=2 => 3, rem=1
i=1 (odd):
j=4: assign idx=2 => 3, rem=0 => idx=3, rem=a[3]=4.
j=3: assign idx=3 => 4, rem=3
j=2: assign idx=3 => 4, rem=2
j=1: assign idx=3 => 4, rem=1
j=0: assign idx=3 => 4, rem=0 => idx=4, rem=a[4]=5.
i=2 (even):
j=0: assign idx=4 => 5, rem=4
j=1: assign 5, rem=3
j=2: assign 5, rem=2
j=3: assign 5, rem=1
j=4: assign 5, rem=0 (end)
Thus grid:
row0: [1,2,2,3,3]
row1: [4,4,4,4,3]? Wait row1 indices: (1,4) = 3 (from j=4 assignment before idx incremented? Actually at i=1, j=4 assigned 3 (color3) before idx incremented? Let's check: i=1, j=4: at start idx=2, rem=1? Wait after finishing i=0, we had idx=2, rem=1? Actually after i=0 we had idx=2, rem=1 (since after j=4 we had rem=1). So at i=1 j=4, we assign color idx+1 = 3, then rem=0? Wait rem was 1 before assignment? Actually we need to track rem correctly. After i=0 j=4, we had idx=2, rem=1 (since we assigned j=4 and decremented rem from 2? Let's recompute stepwise with a table.
Better: use code.
But from manual: after i=0 j=3: rem=2? Actually let's recount from start:
a = [1,2,3,4,5]
Initial idx=0, rem=1.
Step:
(0,0): assign 1, rem=0 -> idx=1, rem=2.
(0,1): assign 2, rem=1.
(0,2): assign 2, rem=0 -> idx=2, rem=3.
(0,3): assign 3, rem=2.
(0,4): assign 3, rem=1.
Now after (0,4): idx=2, rem=1.
i=1 odd:
(1,4): assign 3, rem=0 -> idx=3, rem=4.
(1,3): assign 4, rem=3.
(1,2): assign 4, rem=2.
(1,1): assign 4, rem=1.
(1,0): assign 4, rem=0 -> idx=4, rem=5.
i=2 even:
(2,0): assign 5, rem=4.
(2,1): assign 5, rem=3.
(2,2): assign 5, rem=2.
(2,3): assign 5, rem=1.
(2,4): assign 5, rem=0.
Thus grid:
Row0: (0,0)=1, (0,1)=2, (0,2)=2, (0,3)=3, (0,4)=3.
Row1: (1,0)=4, (1,1)=4, (1,2)=4, (1,3)=4, (1,4)=3? Actually (1,4)=3 assigned before idx increment, so (1,4)=3.
Row2: all 5.
So output:
1 2 2 3 3
4 4 4 4 3
5 5 5 5 5
Check connectivity: color3: cells (0,3),(0,4),(1,4) are connected: (0,3)-(0,4) adjacent, (0,4)-(1,4) adjacent -> fine. So valid.
Thus our output differs from example but still correct.
Therefore answer.
Potential pitfalls: None.
Thus final answer.
But wait: Could there be a scenario where a color's contiguous segment along the snake is not 4-connected because the path uses adjacency but the segment might be split by other colors? The segment is contiguous along the path, so the cells are consecutive steps. That yields connectivity along the path. However, is it possible that the path goes like: cell A, then cell B (adjacent), then cell C (adjacent to B), but A and C are not adjacent (they are diagonal or not directly adjacent). That's fine; connectivity requires a path, not direct adjacency. So the path from A to C goes A->B->C which is valid.
Thus fine.
Thus solution.
Now, ensure output formatting: space-separated integers per row.
Time O(HW). Memory O(HW).
Edge Cases:
- H=100,W=100 -> 10k cells, fine.
- N=1 -> a_1 = 10000, snake will assign all cells to color 1, connectivity holds.
- N=10000 -> each a_i=1. Then each cell gets distinct color, each color's region is single cell -> trivially connected. So fine.
Thus ready.
But wait: The problem may require that we output colors from 1 to N exactly as given order? It says "colors 1,2,...,N", and we assign in order. So yes.
One more nuance: The input order of a_i may be such that the largest color count might be huge, but that's fine.
Thus final solution.
Now, as per instructions, we need to output the final answer in a code block. We'll produce Python solution.
But let's also consider if there is any alternative required for C++ but we are to output in any language? The user asked "Find a way to paint the squares so that the conditions are satisfied." and we need to provide solution. But in this chat we should provide explanation and final code. Usually for these problems, we give a solution in Python.
Thus final.
Now, I'll write final answer. We can always construct a valid painting by traversing the grid in a snake-like path and assigning colors consecutively.
We order the cells as follows:
- For each row from top to bottom:
- If the row index is odd (1‑based) or even (0‑based), go left to right.
- Otherwise go right to left.
This ordering visits every cell exactly once, and consecutive cells are always orthogonally adjacent.
Now we fill the grid along this order:
- Start with color 1 and use its required count
a₁.
- Assign the current color to each cell, decreasing its remaining count.
- When the count for the current color reaches 0, move to the next color.
Because the cells of each color occupy a contiguous segment of the snake path, they form a connected set (the segment itself is a path of adjacent cells).
Hence all conditions are satisfied.
Time complexity: O(H·W)
Memory: O(H·W)
import sys
def main():
input = sys.stdin.readline
H, W = map(int, input().split())
N = int(input())
a = list(map(int, input().split()))
grid = [[0] * W for _ in range(H)]
idx = 0 # index of current color (0‑based)
rem = a[0] # remaining cells to paint with this color
for i in range(H):
# even row (0‑based) → left to right, odd row → right to left
cols = range(W) if i % 2 == 0 else range(W - 1, -1, -1)
for j in cols:
grid[i][j] = idx + 1 # colors are 1‑based
rem -= 1
if rem == 0 and idx + 1 < N:
idx += 1
rem = a[idx]
for row in grid:
print(' '.join(map(str, row)))
if __name__ == "__main__":
main()
The code reads the input, builds the painting using the snake traversal, and prints the result. It works within the given limits and always produces a correct configuration.