#C1987. Maximize Grid Sum

    ID: 45252 Type: Default 1000ms 256MiB

Maximize Grid Sum

Maximize Grid Sum

Given an \(n \times m\) grid and a list of \(n \times m\) integers, your task is to arrange the numbers in the grid such that the sum of all rows and columns is maximized. The approach is to sort the numbers in descending order and fill the grid in row-major order.

Note: Although the problem statement mentions that each row and column should contain unique values, the optimal strategy is simply to place the largest numbers first to maximize the overall sum.

inputFormat

The input is read from stdin and consists of two parts. The first line contains two integers (n) and (m) representing the number of rows and columns respectively. The second line contains (n \times m) space-separated integers.

outputFormat

The output should be written to stdout. The first line must display the maximum sum of the grid. The next (n) lines should each contain (m) space-separated integers representing the grid configuration.## sample

2 3
3 7 2 5 4 6
27

7 6 5 4 3 2

</p>