CF1738A.Glory Addicts

传统题 时间 2000 ms 内存 256 MiB 3 尝试 1 已通过 1 标签

Glory Addicts

The hero is addicted to glory, and is fighting against a monster.

The hero has nn skills. The ii-th skill is of type aia_i (either fire or frost) and has initial damage bib_i.

The hero can perform all of the nn skills in any order (with each skill performed exactly once). When performing each skill, the hero can play a magic as follows:

  • If the current skill immediately follows another skill of a different type, then its damage is doubled.

In other words,

  1. If a skill of type fire and with initial damage cc is performed immediately after a skill of type fire, then it will deal cc damage;
  2. If a skill of type fire and with initial damage cc is performed immediately after a skill of type frost, then it will deal 2c2c damage;
  3. If a skill of type frost and with initial damage cc is performed immediately after a skill of type fire, then it will deal 2c2c damage;
  4. If a skill of type frost and with initial damage cc is performed immediately after a skill of type frost , then it will deal cc damage.

Your task is to find the maximum damage the hero can deal.

Input

Each test contains multiple test cases. The first line contains an integer tt (1t1051 \leq t \leq 10^5) — the number of test cases. The following lines contain the description of each test case.

The first line of each test case contains an integer nn (1n1051 \leq n \leq 10^5), indicating the number of skills.

The second line of each test case contains nn integers a1,a2,,ana_1, a_2, \dots, a_n (0ai10 \leq a_i \leq 1), where aia_i indicates the type of the ii-th skill. Specifically, the ii-th skill is of type fire if ai=0a_i = 0, and of type frost if ai=1a_i = 1.

The third line of each test case contains nn integers b1,b2,,bnb_1, b_2, \dots, b_n (1bi1091 \leq b_i \leq 10^9), where bib_i indicates the initial damage of the ii-th skill.

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

Output

For each test case, output the maximum damage the hero can deal.

Note

In the first test case, we can order the skills by [3,1,4,2][3, 1, 4, 2], and the total damage is 100+2×1+2×1000+10=2112100 + 2 \times 1 + 2 \times 1000 + 10 = 2112.

In the second test case, we can order the skills by [1,4,2,5,3,6][1, 4, 2, 5, 3, 6], and the total damage is $3 + 2 \times 6 + 2 \times 4 + 2 \times 7 + 2 \times 5 + 2 \times 8 = 63$.

In the third test case, we can order the skills by [1,2,3][1, 2, 3], and the total damage is 1000000000+1000000000+1000000000=30000000001000000000 + 1000000000 + 1000000000 = 3000000000.

In the fourth test case, there is only one skill with initial damage 11, so the total damage is 11.

Samples

4
4
0 1 1 1
1 10 100 1000
6
0 0 0 1 1 1
3 4 5 6 7 8
3
1 1 1
1000000000 1000000000 1000000000
1
1
1
2112
63
3000000000
1

在线编程 IDE

建议全屏模式获得最佳体验