P16406 [Algo Beat Contest 004 B] Buying Lotteries

Background

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

Description

There is an $N \times M$ lottery ticket. In the cell at row $i$ and column $j$, there are either two values $t_{i,j,1}$ and $t_{i,j,2}$, or a character `L` as $t_{i,j,1}$ and a value $t_{i,j,2}$. If you win the lottery at row $i$, column $j$, you can get $t_{i,j,2}$ yuan. If there exists at least one cell where $t_{i,j,1}$ is the character `L`, then you can win the entire ticket. Otherwise, for each cell, if $t_{i,j,1}$ is equal to the winning code $X$, then you win that cell. You are given the winning code $X$ and the ticket you obtained. Ask how many yuan you can get in total.

Input Format

The first line contains three integers $N, M, X$, representing the number of rows, the number of columns, and the winning code. The next $N$ lines each contain $M$ integers or characters, representing the first element $t_{i,j,1}$ of each cell. The next $N$ lines each contain $M$ integers, representing the second value $t_{i,j,2}$ of each cell.

Output Format

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

Explanation/Hint

#### Constraints - $1 \le N, M \le 2 \times 10^3$. - For all $i, j$, $1 \le t_{i,j,2}, X \le 10^9$. $t_{i,j,1}$ is an integer or the character `L`. If $t_{i,j,1}$ is an integer, then $1 \le t_{i,j,1} \le 10^9$. Translated by ChatGPT 5