A Farewell to Holds
View as PDFThe 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 layout.
For each pair , the hold in row
and column
has colour
and visibility score
.
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 among the holds whose colour was removed. If no colour is removed, the cost is
.
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 (
).
The next lines contain the colour matrix
. The value in row
and column
is
(
).
The next lines contain the value matrix
. The value in row
and column
is
(
).
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