101474: [AtCoder]ABC147 E - Balanced Path

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Score : $500$ points

Problem Statement

We have a grid with $H$ horizontal rows and $W$ vertical columns. Let $(i,j)$ denote the square at the $i$-th row from the top and the $j$-th column from the left.

The square $(i, j)$ has two numbers $A_{ij}$ and $B_{ij}$ 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 $(1, 1)$ to the square $(H, W)$. In one move, he can move from a square $(i, j)$ to the square $(i+1, j)$ or the square $(i, j+1)$. 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 $(1, 1)$ and $(H, W)$.

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

  • $2 \leq H \leq 80$
  • $2 \leq W \leq 80$
  • $0 \leq A_{ij} \leq 80$
  • $0 \leq B_{ij} \leq 80$
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

$H$ $W$
$A_{11}$ $A_{12}$ $\ldots$ $A_{1W}$
$:$
$A_{H1}$ $A_{H2}$ $\ldots$ $A_{HW}$
$B_{11}$ $B_{12}$ $\ldots$ $B_{1W}$
$:$
$B_{H1}$ $B_{H2}$ $\ldots$ $B_{HW}$

Output

Print the minimum unbalancedness possible.


Sample Input 1

2 2
1 2
3 4
3 4
2 1

Sample Output 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 $3+3+1=7$ and $1+2+4=7$, respectively, for the unbalancedness of $0$.

Figure


Sample Input 2

2 3
1 10 80
80 10 1
1 2 3
4 5 6

Sample Output 2

2

Input

题意翻译

## 题目描述 高桥君有一个 $H$ 行 $W$ 列的棋盘,第 $i$ 行 $j$ 列记为 ( $i$ , $j$ ),每个格子里有两个整数, $a_{i,j}$ 和 $b_{i,j}$。 高桥君从( 1,1 )出发要走到( $H$ , $W$ )。每一次只能向左或向下走一格,每一次走过的方格,高桥君会把它上面的数一个染成红色,一个染成蓝色,求做过路径中红色数字的总和减蓝色数字总和的绝对值的最小值。 ## 输入格式 第一行两个整数 $H$ , $W$ 。 从2到 $H + 1$ 行每行 $W$ 个整数 ,第 $i$ 个是 $A_{i,j}$。 从 $H+2$ 到 $2H+1$ 行每行 $W$ 个整数,第 $i$ 个是 $B_{i,j}$。 输出格式 一行一个整数表示答案

加入题单

上一题 下一题 算法标签: