#P4701. 粘骨牌

粘骨牌

Description

On an n×mn \times m board, all cells except one are covered by 1×21 \times 2 dominoes, so there are nm12\frac{nm - 1}{2} dominoes in total.

Alice may make arbitrarily many moves. After each move, all dominoes must remain within the board, and no two dominoes may overlap.

There are several special cells on the board. Once any of them becomes exposed, you lose, so you must prevent Alice’s moves from exposing these cells.

You may choose to fix any number of dominoes in place. Fixing one domino costs a certain amount. As Bob, you want to pay the minimum total cost so that no matter how Alice moves, those special cells will never be exposed. Find this minimum cost.

If it is impossible no matter how you fix the dominoes, output "GG".

Input Format

The first line contains three integers $n, m, k(1 \leq n, m \leq 1001, 0 \leq k \leq n \times m)$, representing the size of the board and the number of special cells. It is guaranteed that nn and mm are both odd.

The next line contains nm12\frac{nm - 1}{2} numbers. The ii-th number ai(1ai109)a_i(1 \leq a_i \leq 10^9) denotes the cost to fix the ii-th domino.

The next kk lines each contain two integers x,y(1xn,1ym)x, y(1 \leq x \leq n, 1 \leq y \leq m), indicating that (x,y)(x, y) is a special cell. The kk special cells are not guaranteed to be distinct; if duplicates occur, ignore any later occurrence.

The next nn lines each contain mm numbers vi,j(0vi,jnm12)v_{i, j}(0 \leq v_{i, j} \leq \frac{nm - 1}{2}), describing the coverage of the board. If vi,j=0v_{i, j} = 0, that cell is uncovered; otherwise, it is covered by the domino numbered vi,jv_{i, j}.

Output Format

Output a single line with the answer: either a single integer representing the minimum total cost, or GG if it is impossible.

3 3 1
5 5 5 5
1 1
0 1 1
2 3 4
2 3 4
GG
3 3 2
5 5 5 1
3 1
3 3
0 1 1
2 3 4
2 3 4
6
3 3 2
1 5 5 5
3 1
3 3
0 1 1
2 3 4
2 3 4
6

Hint

Translated by ChatGPT 5