eolymp
bolt
Try our new interface for solving problems
Problems

MaxSum (visit all columns by chess knight)

MaxSum (visit all columns by chess knight)

There is a rectangular table with \textbf{N} rows and \textbf{M} columns. There is an integer in each cell. It is necessary to pass it from the top to bottom. You can start from any cell in top row, then you can move as chess knight, but down only, and you should end route in any cell of the bottom row. In other words, from cell \textbf{(i, j)} you can move to \textbf{(i+1, j-2)}, or \textbf{(i+2, j-1)}, or \textbf{(i+2, j+1)}, or \textbf{(i+1, j+2)}, excepting cases which lead out of table. Write a program that will find the greatest possible sum of the values of passed cells among all admissible paths that pass through all columns. \InputFile The first line contains \textbf{N}\textit{ }and \textbf{M}\textit{ -} the number of rows and number of columns (\textbf{1} ≤ \textbf{N} ≤ \textbf{42}, \textbf{1} ≤ \textbf{M} ≤ \textbf{17}), then each of the \textbf{N}\textit{ }lines contains exactly \textbf{M}\textit{ }space-separated integers (each not exceeding \textbf{10^6} by absolute value) - values of table's cells. \OutputFile Print a single integer - the maximum possible sum among admissible paths that pass through all columns. In case of no admissible path that is formed by knight's moves and passes trough all columns, print line "\textbf{impossible}" without quotes, small Latin characters). \textbf{Note}: For table \textbf{4}×\textbf{3} there are four ways to pass each column with admissible moves: Maximum sum achieved with third way: \textbf{25 = 15 + 4 + 6}. For table \textbf{3}×\textbf{3} there aren't admissible ways.
Time limit 4 seconds
Memory limit 64 MiB
Input example #1
4 3
1 15 2
9 7 5
9 2 4
6 9 -1
Output example #1
25
Input example #2
3 3
1 15 2
9 7 5
9 2 4
Output example #2
impossible
Author Илья Порублёв
Source Летняя школа Севастополь 2013, Волна 1, День 2