100 atcoder#ABC099D. [ABC099D] Good Grid
[ABC099D] Good Grid
Score : points
Problem Statement
There is a grid with rows and columns of squares. Let be the square at the -th row from the top and the -th column from the left.
These squares have to be painted in one of the colors from Color to Color . Initially, is painted in Color .
We say the grid is a good grid when the following condition is met for all satisfying :
- If , the color of and the color of are the same.
- If , the color of and the color of are different.
Here, represents modulo .
We will repaint zero or more squares so that the grid will be a good grid.
For a square, the wrongness when the color of the square is before repainting and after repainting, is .
Find the minimum possible sum of the wrongness of all the squares.
Constraints
- $1 \leq D_{i,j} \leq 1000 (i \neq j),D_{i,j}=0 (i=j)$
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
If the minimum possible sum of the wrongness of all the squares is , print .
2 3
0 1 1
1 0 1
1 4 0
1 2
3 3
3
- Repaint to Color . The wrongness of becomes .
- Repaint to Color . The wrongness of becomes .
- Repaint to Color . The wrongness of becomes .
In this case, the sum of the wrongness of all the squares is .
Note that is possible.
4 3
0 12 71
81 0 53
14 92 0
1 1 2 1
2 1 1 2
2 2 1 3
1 1 2 2
428