Genius
Contest: Codeforces Round 708 (Div. 2)
Difficulty rating: 2500
Tags: bitmasks, dp, graphs, number theory

Please note the non-standard memory limit.

There are $$$n$$$ problems numbered with integers from $$$1$$$ to $$$n$$$. $$$i$$$-th problem has the complexity $$$c_i = 2^i$$$, tag $$$tag_i$$$ and score $$$s_i$$$.

After solving the problem $$$i$$$ it's allowed to solve problem $$$j$$$ if and only if $$$\text{IQ} < |c_i - c_j|$$$ and $$$tag_i \neq tag_j$$$. After solving it your $$$\text{IQ}$$$ changes and becomes $$$\text{IQ} = |c_i - c_j|$$$ and you gain $$$|s_i - s_j|$$$ points.

Any problem can be the first. You can solve problems in any order and as many times as you want.

Initially your $$$\text{IQ} = 0$$$. Find the maximum number of points that can be earned.

Input format:
The first line contains a single integer $$$t$$$ $$$(1 \le t \le 100)$$$  — the number of test cases.

The first line of each test case contains an integer $$$n$$$ $$$(1 \le n \le 5000)$$$  — the number of problems.

The second line of each test case contains $$$n$$$ integers $$$tag_1, tag_2, \ldots, tag_n$$$ $$$(1 \le tag_i \le n)$$$  — tags of the problems.

The third line of each test case contains $$$n$$$ integers $$$s_1, s_2, \ldots, s_n$$$ $$$(1 \le s_i \le 10^9)$$$  — scores of the problems.

It's guaranteed that sum of $$$n$$$ over all test cases does not exceed $$$5000$$$.

Output format:
For each test case print a single integer  — the maximum number of points that can be earned.

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

Note:
In the first test case optimal sequence of solving problems is as follows:

1. $$$1 \rightarrow 2$$$, after that total score is $$$5$$$ and $$$\text{IQ} = 2$$$
2. $$$2 \rightarrow 3$$$, after that total score is $$$10$$$ and $$$\text{IQ} = 4$$$
3. $$$3 \rightarrow 1$$$, after that total score is $$$20$$$ and $$$\text{IQ} = 6$$$
4. $$$1 \rightarrow 4$$$, after that total score is $$$35$$$ and $$$\text{IQ} = 14$$$

In the second test case optimal sequence of solving problems is as follows:

1. $$$1 \rightarrow 2$$$, after that total score is $$$5$$$ and $$$\text{IQ} = 2$$$
2. $$$2 \rightarrow 3$$$, after that total score is $$$10$$$ and $$$\text{IQ} = 4$$$
3. $$$3 \rightarrow 4$$$, after that total score is $$$15$$$ and $$$\text{IQ} = 8$$$
4. $$$4 \rightarrow 1$$$, after that total score is $$$35$$$ and $$$\text{IQ} = 14$$$

In the third test case optimal sequence of solving problems is as follows:

1. $$$1 \rightarrow 3$$$, after that total score is $$$17$$$ and $$$\text{IQ} = 6$$$
2. $$$3 \rightarrow 4$$$, after that total score is $$$35$$$ and $$$\text{IQ} = 8$$$
3. $$$4 \rightarrow 2$$$, after that total score is $$$42$$$ and $$$\text{IQ} = 12$$$

Example input:
5
4
1 2 3 4
5 10 15 20
4
1 2 1 2
5 10 15 20
4
2 2 4 1
2 8 19 1
2
1 1
6 9
1
1
666

Example output:
35
30
42
0
0

Editorial:
Let's consider a graph where vertexes are problems and there is an edge $$$\{i, j\}$$$ between vertexes $$$i$$$ and $$$j$$$ with weight $$$|c_i - c_j|$$$. Each edge has a unique weight. Let's prove that. Let's assume that $$$weight = |2^i - 2^j|$$$ and $$$i > j$$$. Then in binary form $$$weight$$$ has its $$$k$$$-th bit set true if and only if $$$j \le k < i$$$. Then for each unique pair $$$\{i, j\}$$$ $$$weight$$$ is unique too since the corners of a subsegment with true bits are unique.
Let $$$dp_i$$$ be the maximal amount of points that may be earned ending with problem $$$i$$$. Initially $$$dp_i = 0$$$ for each $$$1 \le i \le n$$$. Let's consider all edges in increasing order (because $$$\text{IQ}$$$ should increase). To do that we can consider $$$j$$$ in increasing order from $$$2$$$ to $$$n$$$ and then $$$i$$$ in decreasing order from $$$j - 1$$$ to $$$1$$$. It is also explained by the binary form of the $$$weight$$$.
Now let's relax $$$dp$$$ values. When we consider an edge $$$\{i, j\}, tag_i \neq tag_j$$$ we try to solve problem $$$i$$$ after solving $$$dp_j$$$ problems ending with $$$j$$$, and problem $$$i$$$ after solving $$$dp_i$$$ problems ending with $$$i$$$. It means that $$$dp_i = max(dp_i, dp_j + p), dp_j = max(dp_j, dp_i + p)$$$ at the same time, where $$$p = |s_i - s_j|$$$. After considering all edges the answer is the maximal value among all $$$dp$$$ values.