Fillomino 2
Contest: Contest 2050 and Codeforces Round 718 (Div. 1 + Div. 2)
Difficulty rating: 1400
Tags: constructive algorithms, dfs and similar, greedy, implementation

Fillomino is a classic logic puzzle. (You do not need to know Fillomino in order to solve this problem.) In one classroom in Yunqi town, some volunteers are playing a board game variant of it:

Consider an $$$n$$$ by $$$n$$$ chessboard. Its rows are numbered from $$$1$$$ to $$$n$$$ from the top to the bottom. Its columns are numbered from $$$1$$$ to $$$n$$$ from the left to the right. A cell on an intersection of $$$x$$$-th row and $$$y$$$-th column is denoted $$$(x, y)$$$. The main diagonal of the chessboard is cells $$$(x, x)$$$ for all $$$1 \le x \le n$$$.

A permutation of $$$\{1, 2, 3, \dots, n\}$$$ is written on the main diagonal of the chessboard. There is exactly one number written on each of the cells. The problem is to partition the cells under and on the main diagonal (there are exactly $$$1+2+ \ldots +n$$$ such cells) into $$$n$$$ connected regions satisfying the following constraints:

1. Every region should be connected. That means that we can move from any cell of a region to any other cell of the same region visiting only cells of the same region and moving from a cell to an adjacent cell.
2. The $$$x$$$-th region should contain cell on the main diagonal with number $$$x$$$ for all $$$1\le x\le n$$$.
3. The number of cells that belong to the $$$x$$$-th region should be equal to $$$x$$$ for all $$$1\le x\le n$$$.
4. Each cell under and on the main diagonal should belong to exactly one region.

Input format:
The first line contains a single integer $$$n$$$ ($$$1\le n \le 500$$$) denoting the size of the chessboard.

The second line contains $$$n$$$ integers $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$. $$$p_i$$$ is the number written on cell $$$(i, i)$$$. It is guaranteed that each integer from $$$\{1, \ldots, n\}$$$ appears exactly once in $$$p_1$$$, ..., $$$p_n$$$.

Output format:
If no solution exists, output $$$-1$$$.

Otherwise, output $$$n$$$ lines. The $$$i$$$-th line should contain $$$i$$$ numbers. The $$$j$$$-th number on the $$$i$$$-th line should be $$$x$$$ if cell $$$(i, j)$$$ belongs to the the region with $$$x$$$ cells.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
The solutions to the examples are illustrated in the following pictures:

Example input:
3
2 3 1

Example output:
2
2 3
3 3 1

Example input:
5
1 2 3 4 5

Example output:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

Editorial:
The answer is unique and always exists. There are two ways to construct the answer.
Construction 1: Start with the main diagonal. There is one cell $$$(x, x)$$$ with number $$$1$$$ on it. That cell must form a region by itself. For each cell $$$(y, y)$$$ on the main diagonal that is above $$$(x, x)$$$, the cell $$$(y+1, y)$$$ belongs to the same region as $$$(y, y)$$$. We write the number on $$$(y, y)$$$ minus $$$1$$$ on the cell $$$(y+1, y)$$$ and make $$$(y+1, y)$$$ belong to the same region as $$$(y, y)$$$. For each cell $$$(y, y)$$$ on the main diagonal that is below $$$(x, x)$$$, the cell $$$(y, y-1)$$$ belongs to the same region as $$$(y, y)$$$. We write the number on $$$(y, y)$$$ minus $$$1$$$ on the cell $$$(y, y-1)$$$ and make $$$(y, y - 1)$$$ belong to the same region as $$$(y, y)$$$.
Then a permutation of $$$1, \ldots, n-1$$$ are written on the cells $$$(2, 1), (3, 2), \ldots, (n, n-1)$$$. We can continue this process on this subdiagonal. Then a oermutation of $$$1, 2, \ldots, n-2$$$ will be written on the cells $$$(3, 1), (4, 2), \ldots, (n, n-2)$$$. Repeat until all cells have numbers written on them. Note that the numbers written on the cells are not the output. Put $$$x$$$ for the region with size $$$x$$$ after constructing the regions.
This construction proves that the solution always exists and is unique.
Construction 2: Construct the region for $$$(1, 1), (2, 2), \ldots, (n, n)$$$ in order. When starting at $$$(i, i)$$$, we walk from $$$(i, i)$$$ for $$$x$$$ steps, where $$$x$$$ is the number written on $$$(i, i)$$$. For each step, if the cell to the left of your current cell is empty, we go to that cell and write $$$x$$$ on it. Otherwise we go down and write $$$x$$$ there.
We can prove that the second construction produces exactly the same answer as the first construction. They only differ in their orders of processing.
The regions are always paths.