#ABC147E. Balanced Path

Balanced Path

题目描述

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

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

我们有一个横向有 HH 行,纵向有 WW 列的网格。让 (i,j)(i,j) 表示从上往下第 ii 行和从左往上第 jj 列的正方形。

正方形 (i,j)(i, j) 上写着两个数字 AijA_{ij}BijB_{ij}

首先,高桥在每个正方形上将写有数字的一个涂成红色,另一个涂成蓝色。

然后,他从 (1,1)(1, 1) 走到 (H,W)(H, W) 。在一次移动中,他可以从 (i,j)(i, j) 移动到 (i+1,j)(i+1, j)(i,j+1)(i, j+1) 。他不得离开网格。

让_不平衡度_是写在高桥路径上的方格(包括方格 (1,1)(1, 1)(H,W)(H, W) )的红色数字之和与蓝色数字之和的绝对差。

高桥希望通过适当地涂画网格和在网格上行进,使不平衡度尽可能小。

请找出尽可能小的不平衡度。

输入格式

输入内容按以下格式标准输入:

HH WW
A11A_{11} A12A_{12} \ldots A1WA_{1W}
::
AH1A_{H1} AH2A_{H2} \ldots AHWA_{HW}
B11B_{11} B12B_{12} \ldots B1WB_{1W}
::
BH1B_{H1} BH2B_{H2} \ldots BHWB_{HW}

输出格式

打印可能的最小不平衡度。

样例 #1

样例输入 #1

2 2
1 2
3 4
3 4
2 1

样例输出 #1

0

样例 #2

样例输入 #2

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

样例输出 #2

2

说明

数据规模与约定

  • 2H802 \leq H \leq 80
  • 2W802 \leq W \leq 80
  • 0Aij800 \leq A_{ij} \leq 80
  • 0Bij800 \leq B_{ij} \leq 80
  • 所有输入值均为整数。

样例 11 解释

如下图所示,通过绘制网格并在网格上移动,红色数字之和和蓝色数字之和分别为 3+3+1=73+3+1=71+2+4=71+2+4=7 ,不平衡度为 00

image.png