A Farewell to Holds

View as PDF

Submit solution


Points: 100
Time limit: 10.0s
Memory limit: 1G

Author:
Problem type

The MAPS marketing team has been assigned to design an Olympic bouldering wall. Since the team goes bouldering after every MAPS workshop, they wanted the wall to follow a very particular n \times n layout.

For each pair (i,j), the hold in row i and column j has colour c_{i,j} and visibility score a_{i,j}.

The broadcast crew has one rule: after the final design is chosen, no remaining colour may appear more than once in any row or in any column. Repainting would require another meeting, so the marketing team may instead remove the colour from some holds.

The removed holds must also be well spread out: no two removed holds may be in the same row or in the same column.

The cost of a removal set is the maximum a_{i,j} among the holds whose colour was removed. If no colour is removed, the cost is 0.

Determine whether it is possible to make the wall valid. If it is possible, find the minimum possible cost.

Input

The first line contains one integer n (1 \leq n \leq 10^3).

The next n lines contain the colour matrix c. The value in row i and column j is c_{i,j} (1 \leq c_{i,j} \leq 10^9).

The next n lines contain the value matrix a. The value in row i and column j is a_{i,j} (1 \leq a_{i,j} \leq 10^9).

Output

If it is possible to make the wall valid, print Yes; otherwise print No.

If the answer is Yes, print the minimum possible cost on the second line.

Example 1

Input
3
1 4 3
1 2 3
6 2 5
1 9 1
3 5 7
9 1 9
Output
Yes
3

Example 2

Input
3
1 2 3
1 2 3
1 2 3
1 9 1
3 5 7
9 1 9
Output
No

Comments

There are no comments at the moment.