eolymp
bolt
Try our new interface for solving problems
Problems

Puzzle

Puzzle

You're given \textbf{N^2} decimal digits from \textbf{1} to \textbf{9}. Consider some arrangement of these digits in the cells of a square board \textbf{N}×\textbf{N}, one digit per cell. In each of the rows on the board, reading from left to right, we get a decimal representation of some \textbf{N}-digit number. In each of the columns, reading from top to bottom, we also get a decimal representation of an \textbf{N}-digit number. Let \textbf{S} be the sum of all \textbf{N} numbers in rows and all \textbf{N} numbers in columns. Arrange the given digits on the board to make maximally possible. \InputFile The first line of input contains integer \textbf{N} (\textbf{1} ≤ \textbf{N} ≤ \textbf{8}). In the second line, there are \textbf{N^2} decimal digits from \textbf{1} to \textbf{9}. written with no delimiters. \OutputFile Print the maximal possible value of the sum \textbf{S}.
Time limit 1 second
Memory limit 64 MiB
Input example #1
2
9174
Output example #1
303
Author Ivan Metelsky
Source Winter School, Kharkov, 2011, Day 7