You should write a program or function which receives a list of digits as input and outputs or returns the largest sum achievable by putting these digits in a square.
Input will always contain a square number of digits. An example square arrangement for the input 9 1 2 3 4 5 6 7 7
could be
677
943
125
The sum is calculated as the sum of all rows and columns. For the above arrangement the sum would be 677 + 943 + 125 + 691 + 742 + 735 = 3913
. Note that this is not the maximal sum so this isn't the expected output.
Input
- A list with length
n^2
(n>=1
) containing non-zero digits (1-9
).
Output
- An integer, the largest sum achievable with the input digits put in a square.
Examples
Example format is input => output
.
5 => 10
1 2 3 4 => 137
5 8 6 8 => 324
9 1 2 3 4 5 6 7 7 => 4588
2 4 9 7 3 4 2 1 3 => 3823
8 2 9 4 8 1 9 3 4 6 3 8 1 5 7 1 => 68423
5 4 3 6 9 2 6 8 8 1 6 8 5 2 8 4 2 4 5 7 3 7 6 6 7 => 836445
This is code golf so the shortest entry wins.