#P16406. [Algo Beat Contest 004 B] Buying Lotteries

[Algo Beat Contest 004 B] Buying Lotteries

Background

Little T has recently fallen in love with scratch-off lotteries.

Problem Description

There is an N×MN \times M lottery ticket. In the cell at row ii and column jj, there are either two values ti,j,1t_{i,j,1} and ti,j,2t_{i,j,2}, or a character L as ti,j,1t_{i,j,1} and a value ti,j,2t_{i,j,2}. If you win the lottery at row ii, column jj, you can get ti,j,2t_{i,j,2} yuan.

If there exists at least one cell where ti,j,1t_{i,j,1} is the character L, then you can win the entire ticket.

Otherwise, for each cell, if ti,j,1t_{i,j,1} is equal to the winning code XX, then you win that cell.

You are given the winning code XX and the ticket you obtained. Ask how many yuan you can get in total.

Input Format

The first line contains three integers N,M,XN, M, X, representing the number of rows, the number of columns, and the winning code.

The next NN lines each contain MM integers or characters, representing the first element ti,j,1t_{i,j,1} of each cell.

The next NN lines each contain MM integers, representing the second value ti,j,2t_{i,j,2} of each cell.

Output Format

Output one number, representing the total amount of money you can get.

3 3 114514
1 3 2
2 5 7
114514 114514 1145145
1 2 3
3 2 1
5 8 9
13
2 2 1919810
114514 1145145
1919 L
1 3
2 4
10

Hint

Constraints

  • 1≤N,M≤2×1031 \le N, M \le 2 \times 10^3.
  • For all i,ji, j, 1≤ti,j,2,X≤1091 \le t_{i,j,2}, X \le 10^9. ti,j,1t_{i,j,1} is an integer or the character L. If ti,j,1t_{i,j,1} is an integer, then 1≤ti,j,1≤1091 \le t_{i,j,1} \le 10^9.

Translated by ChatGPT 5