Neowise Labs Contest 1 (Codeforces Round 1018, Div. 1 + Div. 2)
C
Wonderful City
Codeforces
1700
原题
← BD →
翻译中…

You are the proud leader of a city in Ancient Berland. There are $n^2$ buildings arranged in a grid of $n$ rows and $n$ columns. The height of the building in row $i$ and column $j$ is $h_{i, j}$.

翻译中…

The city is beautiful if no two adjacent by side buildings have the same height. In other words, it must satisfy the following:

翻译中…
  • There does not exist a position $(i, j)$ ($1 \leq i \leq n$, $1 \leq j \leq n - 1$) such that $h_{i, j} = h_{i, j + 1}$.
  • There does not exist a position $(i, j)$ ($1 \leq i \leq n - 1$, $1 \leq j \leq n$) such that $h_{i, j} = h_{i + 1, j}$.
翻译中…

There are $n$ workers at company A, and $n$ workers at company B. Each worker can be hired at most once.

翻译中…

It costs $a_i$ coins to hire worker $i$ at company A. After hiring, worker $i$ will:

翻译中…
  • Increase the heights of all buildings in row $i$ by $1$. In other words, increase $h_{i, 1}, h_{i, 2}, \ldots, h_{i, n}$ by $1$.
翻译中…

It costs $b_j$ coins to hire worker $j$ at company B. After hiring, worker $j$ will:

翻译中…
  • Increase the heights of all buildings in column $j$ by $1$. In other words, increase $h_{1, j}, h_{2, j}, \ldots, h_{n, j}$ by $1$.
翻译中…

Find the minimum number of coins needed to make the city beautiful, or report that it is impossible.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows.

翻译中…

The first line of each test case contains a single integer $n$ ($2 \le n \le 1000$) — the size of the grid.

翻译中…

The $i$-th of the next $n$ lines of each test case contains $n$ integers $h_{i, 1}, h_{i, 2}, \ldots, h_{i, n}$ ($1 \le h_{i, j} \le 10^9$) — the heights of the buildings in row $i$.

翻译中…

The next line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the costs of hiring the workers at company A.

翻译中…

The next line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_j \le 10^9$) — the costs of hiring the workers at company B.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $1000$.

翻译中…

Output

翻译中…

For each test case, output a single integer — the minimum number of coins needed, or $-1$ if it is impossible.

翻译中…

Note

翻译中…

For the first test case, we can see that the city is already beautiful. Thus, the answer is $0$.

翻译中…

For the second test case, we can hire worker $2$ from company A, worker $4$ from company A, and worker $4$ from company B:

翻译中…

$1$$2$$1$$\color{red}2$$\implies$$1$$2$$1$$\color{red}3$$\color{red}3$$\color{red}2$$\color{red}1$$\color{red}2$$\color{red}4$$\color{red}3$$\color{red}2$$\color{red}4$$1$$2$$1$$\color{red}1$$1$$2$$1$$\color{red}2$$\color{red}1$$\color{red}3$$\color{red}1$$\color{red}2$$\color{red}2$$\color{red}4$$\color{red}2$$\color{red}4$

翻译中…

The cost of hiring the workers is $2 + 4 + 8 = 14$. This is the minimum possible cost.

翻译中…

For the third test case, no matter what we do, it is impossible to make the city beautiful. Thus, the answer is $-1$.

翻译中…

样例

Input #1
4
2
1 2
2 1
100 100
100 100
4
1 2 1 2
3 2 1 2
1 2 1 1
1 3 1 2
1 2 3 4
5 6 7 8
3
1 2 2
2 2 1
2 1 1
100 100 100
100 100 100
6
8 7 2 8 4 8
7 7 9 7 1 1
8 3 1 1 8 5
6 8 3 1 1 4
1 4 5 1 9 6
7 1 1 6 8 2
11 23 20 79 30 15
15 83 73 57 34 63
Output #1
0
14
-1
183
InputCopy OutputCopy
题目信息
题号
2096C
时间限制
2 s
内存限制
256 MB
标签
动态规划
模拟

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。