#P8050. Determining the Missing Cell Value on an L‐Shaped Chessboard

    ID: 21234 Type: Default 1000ms 256MiB

Determining the Missing Cell Value on an L‐Shaped Chessboard

Determining the Missing Cell Value on an L‐Shaped Chessboard

A unique L‐shaped chessboard is constructed by joining two rectangles: an upper rectangle of size \(n_1 \times m_1\) and a lower rectangle of size \(n_2 \times m_2\). The board is arranged such that the top \(n_1\) rows have \(m_1\) columns and the bottom \(n_2\) rows have \(m_2\) columns. Initially, every cell on the board holds the integer \(k\).

Then, a sequence of operations is performed. In each operation, you choose two adjacent cells (cells sharing a common side) and either add 1 to both or subtract 1 from both. Consequently, the board changes while maintaining a special invariant.

The invariant comes from a bipartite coloring of the board in a chessboard pattern: color cell \((r,c)\) black if \(r+c\) is even and white if \(r+c\) is odd. Since every operation adds (or subtracts) 1 to exactly one black and one white cell, the difference between the sum of the black cells and the sum of the white cells, that is,

[ D = (#\text{black} - #\text{white})\cdot k, ]

remains unchanged throughout all operations.

After all operations are performed, every cell contains its resulting value except one which, due to an accident, is marked with 999999 (and its true value is forgotten). Your task is to determine what the missing number should be.

Input/Output Format:

inputFormat

The first line contains five space-separated integers \(n_1\), \(m_1\), \(n_2\), \(m_2\), and \(k\).

The next (n_1) lines describe the top rectangle. Each of these lines contains (m_1) integers representing the row values.

Following that, the next \(n_2\) lines describe the bottom rectangle. Each of these lines contains \(m_2\) integers.

It is guaranteed that exactly one cell in the entire board has the value 999999.

outputFormat

Output a single integer, which is the correct number that should replace the erroneous 999999 on the board.

sample

2 2 3 4 0
1 2
3 4
5 6 7 8
9 10 999999 12
13 14 15 16
9