Codeforces Round 708 (Div. 2)
D
Genius
Codeforces
2500
原题
← C2E1 →
翻译中…

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} \lt |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

翻译中…

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

翻译中…

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

翻译中…

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$
翻译中…

样例

Input #1
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
Output #1
35
30
42
0
0
InputCopy OutputCopy
题目信息
题号
1497D
时间限制
2 s
内存限制
32 MB
标签
位运算
动态规划
图论
数论

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。