#abc264f. F - Monochromatic Path

F - Monochromatic Path

Score : 500500 points

问题描述

我们有一个包含 HH 行和 WW 列的网格。每个格子要么涂成白色,要么涂成黑色。对于满足条件 1iH1 \leq i \leq H1jW1 \leq j \leq W 的每一对整数 (i,j)(i, j),第 ii 行从上到下、第 jj 列从左至右的格子(我们简单地记作 Square (i,j)(i, j))的颜色由 Ai,jA_{i, j} 表示。如果 Ai,j=0A_{i, j} = 0,则 Square (i,j)(i, j) 为白色;如果 Ai,j=1A_{i, j} = 1,则为黑色。

你可以按任意顺序执行以下操作任意次(可能为 00 次):

  • 选择一个满足 1iH1 \leq i \leq H 的整数 ii,支付 RiR_i 日元(日本货币),然后将网格中第 ii 行从上到下的每个格子颜色反转。(白色格子变为黑色,黑色格子变为白色。)
  • 选择一个满足 1jW1 \leq j \leq W 的整数 jj,支付 CjC_j 日元,然后将网格中第 jj 列从左至右的每个格子颜色反转。

请输出满足以下条件所需的最小总费用:

  • 存在一个从 Square (1,1)(1, 1) 到 Square (H,W)(H, W) 的路径,该路径可以通过重复向下或向右移动得到,并且路径中所有格子(包括 Square (1,1)(1, 1) 和 Square (H,W)(H, W))颜色相同。

在本题的约束条件下,我们可以证明总是可以通过有限次操作满足上述条件。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

We have a grid with HH rows and WW columns. Each square is painted either white or black. For each integer pair (i,j)(i, j) such that 1iH1 \leq i \leq H and 1jW1 \leq j \leq W, the color of the square at the ii-th row from the top and jj-th column from the left (we simply denote this square by Square (i,j)(i, j)) is represented by Ai,jA_{i, j}. Square (i,j)(i, j) is white if Ai,j=0A_{i, j} = 0, and black if Ai,j=1A_{i, j} = 1.

You may perform the following operations any number of (possibly 00) times in any order:

  • Choose an integer ii such that 1iH1 \leq i \leq H, pay RiR_i yen (the currency in Japan), and invert the color of each square in the ii-th row from the top in the grid. (White squares are painted black, and black squares are painted white.)
  • Choose an integer jj such that 1jW1 \leq j \leq W, pay CjC_j yen, and invert the color of each square in the jj-th column from the left in the grid.

Print the minimum total cost to satisfy the following condition:

  • There exists a path from Square (1,1)(1, 1) to Square (H,W)(H, W) that can be obtained by repeatedly moving down or to the right, such that all squares in the path (including Square (1,1)(1, 1) and Square (H,W)(H, W)) 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

  • 2H,W20002 \leq H, W \leq 2000
  • 1Ri1091 \leq R_i \leq 10^9
  • 1Cj1091 \leq C_j \leq 10^9
  • Ai,j{0,1}A_{i, j} \in \lbrace 0, 1\rbrace
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

HH WW

R1R_1 R2R_2 \ldots RHR_H

C1C_1 C2C_2 \ldots CWC_W

A1,1A1,2A1,WA_{1, 1}A_{1, 2}\ldots A_{1, W}

A2,1A2,2A2,WA_{2, 1}A_{2, 2}\ldots A_{2, W}

\vdots

AH,1AH,2AH,WA_{H, 1}A_{H, 2}\ldots A_{H, W}

Output

Print the answer.

Sample Input 1

3 4
4 3 5
2 6 7 4
0100
1011
1010

Sample Output 1

9

We denote a white square by 0 and a black square by 1. On the initial grid, you can pay R2=3R_2 = 3 yen to invert the color of each square in the 22-nd row from the top to make the grid:

0100
0100
1010

Then, you can pay C2=6C_2 = 6 yen to invert the color of each square in the 22-nd row from the left to make the grid:

0000
0000
1110

Now, there exists a path from Square (1,1)(1, 1) to Square (3,4)(3, 4) 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 3+6=93+6 = 9 yen, which is the minimum possible.

Sample Input 2

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

Sample Output 2

125

update @ 2024/3/10 11:10:13