Yet Another Minimization Problem
Contest: Codeforces Global Round 19
Difficulty rating: 1800
Tags: dp, greedy, math

You are given two arrays $$$a$$$ and $$$b$$$, both of length $$$n$$$.

You can perform the following operation any number of times (possibly zero): select an index $$$i$$$ ($$$1 \leq i \leq n$$$) and swap $$$a_i$$$ and $$$b_i$$$.

Let's define the cost of the array $$$a$$$ as $$$\sum_{i=1}^{n} \sum_{j=i + 1}^{n} (a_i + a_j)^2$$$. Similarly, the cost of the array $$$b$$$ is $$$\sum_{i=1}^{n} \sum_{j=i + 1}^{n} (b_i + b_j)^2$$$.

Your task is to minimize the total cost of two arrays.

Input format:
Each test case consists of several test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 40$$$) — the number of test cases. The following is a description of the input data sets.

The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 100$$$) — the length of both arrays.

The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$) — elements of the first array.

The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \leq b_i \leq 100$$$) — elements of the second array.

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

Output format:
For each test case, print the minimum possible total cost.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the second test case, in one of the optimal answers after all operations $$$a = [2, 6, 4, 6]$$$, $$$b = [3, 7, 6, 1]$$$.

The cost of the array $$$a$$$ equals to $$$(2 + 6)^2 + (2 + 4)^2 + (2 + 6)^2 + (6 + 4)^2 + (6 + 6)^2 + (4 + 6)^2 = 508$$$.

The cost of the array $$$b$$$ equals to $$$(3 + 7)^2 + (3 + 6)^2 + (3 + 1)^2 + (7 + 6)^2 + (7 + 1)^2 + (6 + 1)^2 = 479$$$.

The total cost of two arrays equals to $$$508 + 479 = 987$$$.

Example input:
3
1
3
6
4
3 6 6 6
2 7 4 1
4
6 7 2 4
2 5 3 5

Example output:
0
987
914

Editorial:
The $$$cost$$$ of the array $$$a$$$ equals to $$$\sum_{i=1}^{n} \sum_{j=i + 1}^{n} (a_i + a_j)^2 = \sum_{i=1}^{n} \sum_{j=i + 1}^{n} (a_i^2 + a_j^2 + 2a_i a_j)$$$.
Let $$$s = \sum_{i=1}^{n} a_i$$$.
Then $$$cost = (n - 1) \cdot \sum_{i=1}^{n} a_i^2 + \sum_{i=1}^n (a_i \cdot (s - a_i)) = (n - 1) \cdot \sum_{i=1}^{n} a_i^2 + s^2 - \sum_{i=1}^{n} a_i^2 = (n - 2) \cdot \sum_{i=1}^{n} a_i^2 + (\sum_{i=1}^n a_i)^2$$$.
Then the total cost of two arrays equals to $$$(n - 2) \cdot \sum_{i=1}^{n} (a_i^2 + b_i^2) + (\sum_{i=1}^n a_i)^2 + (\sum_{i=1}^n b_i)^2$$$. The first term is a constant $$$\Rightarrow$$$ we need to minimize $$$(\sum_{i=1}^n a_i)^2 + (\sum_{i=1}^n b_i)^2$$$.
There are two continuations of the solution, but the idea of both is to iterate over all possible sum of the array $$$a$$$, then calculate the sum of the array $$$b$$$ and update the answer using formula written above:
1. Let $$$dp_{i, w}$$$ is true if it's possible to make some operations so that the sum of first $$$i$$$ elements in the array $$$a$$$ equals to $$$w$$$, otherwise $$$dp_{i, w}$$$ is false. Then $$$dp_{1, a_1} = dp_{1, b_1} =$$$ true. For $$$i > 1$$$: $$$dp_{i, w} = dp_{i-1, w-a_i}$$$ or $$$dp_{i-1, w-b_i}$$$. Then to iterate over all possible sums of the array $$$a$$$ you need to consider such $$$s$$$, that $$$dp_{n, s} =$$$ true.
2. Assume, that we have $$$n$$$ items, where $$$i$$$-th item has a weight of $$$|b_i - a_i|$$$. By solving simple backpack problem let's find all possible sums of weights of these items. And if the sum contains $$$i$$$-th item, then we will assume that $$$a_i \ge b_i$$$, otherwise $$$a_i \le b_i$$$. So if the sum of weights of some items equals to $$$s$$$, then the sum of the array $$$a$$$ equals to $$$\sum_{i=1}^n min(a_i, b_i) + s$$$. So all possible sums of the array $$$a$$$ can be received from all possible sums of weights of these items.
Both solutions works in $$$O(n^2 \cdot maxA)$$$ where $$$maxA = 100$$$, and both solutions can be optimized with std::bitest, speeding up them by $$$64$$$ times, but it wasn't necessary.