atcoder#ABC147E. [ABC147E] Balanced Path
[ABC147E] Balanced Path
Score : points
Problem Statement
We have a grid with horizontal rows and vertical columns. Let denote the square at the -th row from the top and the -th column from the left.
The square has two numbers and written on it.
First, for each square, Takahashi paints one of the written numbers red and the other blue.
Then, he travels from the square to the square . In one move, he can move from a square to the square or the square . He must not leave the grid.
Let the unbalancedness be the absolute difference of the sum of red numbers and the sum of blue numbers written on the squares along Takahashi's path, including the squares and .
Takahashi wants to make the unbalancedness as small as possible by appropriately painting the grid and traveling on it.
Find the minimum unbalancedness possible.
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the minimum unbalancedness possible.
2 2
1 2
3 4
3 4
2 1
0
By painting the grid and traveling on it as shown in the figure below, the sum of red numbers and the sum of blue numbers are and , respectively, for the unbalancedness of .
2 3
1 10 80
80 10 1
1 2 3
4 5 6
2