atcoder#ABC264F. [ABC264F] Monochromatic Path
[ABC264F] Monochromatic Path
Score : points
Problem Statement
We have a grid with rows and columns. Each square is painted either white or black. For each integer pair such that and , the color of the square at the -th row from the top and -th column from the left (we simply denote this square by Square ) is represented by . Square is white if , and black if .
You may perform the following operations any number of (possibly ) times in any order:
- Choose an integer such that , pay yen (the currency in Japan), and invert the color of each square in the -th row from the top in the grid. (White squares are painted black, and black squares are painted white.)
- Choose an integer such that , pay yen, and invert the color of each square in the -th column from the left in the grid.
Print the minimum total cost to satisfy the following condition:
- There exists a path from Square to Square that can be obtained by repeatedly moving down or to the right, such that all squares in the path (including Square and Square ) have the same color.
We can prove that it is always possible to satisfy the condition in a finite number of operations under the Constraints of this problem.
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the answer.
3 4
4 3 5
2 6 7 4
0100
1011
1010
9
We denote a white square by 0
and a black square by 1
.
On the initial grid, you can pay yen to invert the color of each square in the -nd row from the top to make the grid:
0100
0100
1010
Then, you can pay yen to invert the color of each square in the -nd row from the left to make the grid:
0000
0000
1110
Now, there exists a path from Square to Square such that all squares in the path have the same color (such as the path $(1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (2, 3) \rightarrow (2, 4) \rightarrow (3, 4)$). The total cost paid is yen, which is the minimum possible.
15 20
29 27 79 27 30 4 93 89 44 88 70 75 96 3 78
39 97 12 53 62 32 38 84 49 93 53 26 13 25 2 76 32 42 34 18
01011100110000001111
10101111100010011000
11011000011010001010
00010100011111010100
11111001101010001011
01111001100101011100
10010000001110101110
01001011100100101000
11001000100101011000
01110000111011100101
00111110111110011111
10101111111011101101
11000011000111111001
00011101011110001101
01010000000001000000
125