#P3236. [HNOI2014] 画框

[HNOI2014] 画框

Description

Xiao T (小 T) plans to place several paintings at home. He has bought NN paintings and NN frames. To show his taste, he wants to match paintings with frames so that the result is neither too plain nor too jarring.

For the pairing of painting ii with frame jj, Xiao T assigns a plainness value Ai,jA_{i, j} and an incompatibility value Bi,jB_{i, j}. The overall disharmony of a complete matching is defined as the product of the sum of plainness values over all matched pairs and the sum of incompatibility values over all matched pairs. Specifically, let painting ii be matched with frame pip_i; then the overall disharmony is

$$\mathrm{disharmony}=\sum_{i=1}^{N}A_{i,p_i}\times \sum_{i=1}^{N}B_{i,p_i}.$$

Xiao T wants to know the minimum possible overall disharmony.

Input Format

The first line contains a positive integer TT, the number of test cases. For each test case:

  • The first line contains a positive integer NN, the number of paintings and frames.
  • Lines 22 through N+1N+1 each contain NN non-negative integers; on line i+1i+1, the jj-th number is Ai,jA_{i, j}.
  • Lines N+2N+2 through 2N+12N+1 each contain NN non-negative integers; on line i+N+1i+N+1, the jj-th number is Bi,jB_{i, j}.

Output Format

Output TT lines, each containing one integer: the minimum overall disharmony.

1
3
4 3 2
2 3 4
3 2 1
2 3 2
2 2 4
1 1 3
30

Hint

If painting 11 is matched with frame 33, painting 22 with frame 11, and painting 33 with frame 22, then the overall disharmony is 3030.

Constraints: For 100%100\% of the testdata, N70N \leq 70, T3T \leq 3, Ai,j200A_{i, j} \leq 200, Bi,j200B_{i, j} \leq 200.

Translated by ChatGPT 5