#abc344f. F - Earn to Advance

F - Earn to Advance

Score: 550550 points

问题陈述

存在一个 NN 行和 NN 列的网格。令 (i,j)(i,j) 表示从上到下的第 ii 行和从左到右的第 jj 列的方格。

高桥最初在方格 (1,1)(1,1) 处,此时他的钱为零。

当高桥处于方格 (i,j)(i,j) 时,他可以在一个 行动 中执行以下操作之一:

  • 保持在原地,并将他的钱增加 Pi,jP_{i,j}
  • 从他的钱中支付 Ri,jR_{i,j},然后移动到方格 (i,j+1)(i,j+1)
  • 从他的钱中支付 Di,jD_{i,j},然后移动到方格 (i+1,j)(i+1,j)

他不能做出使他的钱变为负数或使他超出网格范围的移动。

如果高桥采取最优策略,他需要多少个行动才能到达方格 (N,N)(N,N)

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

Problem Statement

There is a grid with NN rows and NN columns. Let (i,j)(i,j) denote the square at the ii-th row from the top and jj-th column from the left.

Takahashi is initially at square (1,1)(1,1) with zero money.

When Takahashi is at square (i,j)(i,j), he can perform one of the following in one action:

  • Stay at the same square and increase his money by Pi,jP_{i,j}.
  • Pay Ri,jR_{i,j} from his money and move to square (i,j+1)(i,j+1).
  • Pay Di,jD_{i,j} from his money and move to square (i+1,j)(i+1,j).

He cannot make a move that would make his money negative or take him outside the grid.

If Takahashi acts optimally, how many actions does he need to reach square (N,N)(N,N)?

Constraints

  • 2N802 \leq N \leq 80
  • 1Pi,j1091 \leq P_{i,j} \leq 10^9
  • 1Ri,j,Di,j1091 \leq R_{i,j},D_{i,j} \leq 10^9
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN

P1,1P_{1,1} \ldots P1,NP_{1,N}

\vdots

PN,1P_{N,1} \ldots PN,NP_{N,N}

R1,1R_{1,1} \ldots R1,N1R_{1,N-1}

\vdots

RN,1R_{N,1} \ldots RN,N1R_{N,N-1}

D1,1D_{1,1} \ldots D1,ND_{1,N}

\vdots

DN1,1D_{N-1,1} \ldots DN1,ND_{N-1,N}

Output

Print the answer.

Sample Input 1

3
1 2 3
3 1 2
2 1 1
1 2
4 3
4 2
1 5 7
5 3 3

Sample Output 1

8

Figure

It is possible to reach square (3,3)(3,3) in eight actions as follows:

  • Stay at square (1,1)(1,1) and increase money by 11. His money is now 11.
  • Pay 11 money and move to square (2,1)(2,1). His money is now 00.
  • Stay at square (2,1)(2,1) and increase money by 33. His money is now 33.
  • Stay at square (2,1)(2,1) and increase money by 33. His money is now 66.
  • Stay at square (2,1)(2,1) and increase money by 33. His money is now 99.
  • Pay 44 money and move to square (2,2)(2,2). His money is now 55.
  • Pay 33 money and move to square (3,2)(3,2). His money is now 22.
  • Pay 22 money and move to square (3,3)(3,3). His money is now 00.

Sample Input 2

3
1 1 1
1 1 1
1 1 1
1000000000 1000000000
1000000000 1000000000
1000000000 1000000000
1000000000 1000000000 1000000000
1000000000 1000000000 1000000000

Sample Output 2

4000000004