#include <iostream>
#include <climits>
using namespace std;
int n;
int boy[21][21], girl[21][21]; // Stores the competitiveness of male and female athletes
int Max = INT_MIN; // Represents the maximum total competitiveness for both genders
int sum = 0; // Temporary sum
int data[21][21]; // data[i][] stores the competitiveness when male athlete i pairs with a female
int maxSum[21]; // Records the maximum competitiveness achievable by each male athlete when paired
int book[21]; // Used to mark whether a female athlete is already paired: book[0] for unpaired, book[1] for paired
void dfs(int t) {
if (t >= n) {
// When t reaches n, all athletes have been marked, and we've found the maximum competitiveness
Max = max(Max, sum);
return;
}
int ctn = 0; // Pruning function: check if sum of already matched athletes (t to n-1) and the maximum competitiveness of t to n-1 males with females is less than Max
for (int i = t; i < n; i++) {
ctn += maxSum[i];
}
if (sum + ctn < Max) {
return; // If sum + ctn is less than Max, prune this branch and set Max to the maximum found so far
}
for (int i = 0; i < n; i++) {
if (!book[i]) {
// Female athlete i is not yet paired
book[i] = 1; // Pair male athlete t with female athlete i
sum += data[t][i]; // Add the competitiveness of male athlete t with female athlete i to the sum
dfs(t + 1); // Match the next male athlete
book[i] = 0; // If the sum for male athlete t and female athlete i doesn't exceed Max, backtrack
sum -= data[t][i];
}
}
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
// Input competitiveness of male athletes
for (int j = 0; j < n; j++)
cin >> boy[i][j];
}
for (int i = 0; i < n; i++) {
// Input competitiveness of female athletes
for (int j = 0; j < n; j++)
cin >> girl[i][j];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
data[i][j] = boy[i][j] * girl[j][i]; // Calculate competitiveness for each male athlete with each female athlete
maxSum[i] = max(maxSum[i], data[i][j]); // Record the maximum competitiveness for each male athlete after pairing
}
}
dfs(0); // Start finding the maximum competitiveness
cout << Max << endl; // Output the maximum competitiveness
return 0;
}