#K39527. Maximize Diagonals in a Matrix

    ID: 26440 Type: Default 1000ms 256MiB

Maximize Diagonals in a Matrix

Maximize Diagonals in a Matrix

You are given a single integer n representing the size of a square matrix. Your task is to construct an n × n matrix filled with distinct integers from 1 to n2 in row-major order, and then perform a series of swaps to maximize the sum of the diagonal elements.

The procedure is as follows:

  1. Fill the matrix with numbers 1 through n2 in row-major order.
  2. For each pair of indices (i, j) where i < j, swap matrix[i][j] with matrix[j][i].

The final matrix will then have its diagonal sum maximized. Mathematically, if the resulting matrix is \(M\), the goal is to maximize \(\sum_{i=1}^{n} M_{ii}\).

inputFormat

Input is provided via standard input and consists of a single integer n (1 ≤ n ≤ 1000), which specifies the dimensions of the matrix.

outputFormat

Output the final n × n matrix to standard output, where each row is printed on a new line and the numbers in a row are separated by a single space. There should be no trailing spaces at the end of any line.

## sample
1
1