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

The hero has $n$ skills. The $i$-th skill is of type $a_i$ (either fire or frost) and has initial damage $b_i$.

The hero can perform all of the $n$ 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,

If a skill of type fire and with initial damage $c$ is performed immediately after a skill of type fire, then it will deal $c$ damage;

If a skill of type fire and with initial damage $c$ is performed immediately after a skill of type frost, then it will deal $2c$ damage;

If a skill of type frost and with initial damage $c$ is performed immediately after a skill of type fire, then it will deal $2c$ damage;

If a skill of type frost and with initial damage $c$ is performed immediately after a skill of type frost , then it will deal $c$ 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 $t$ ($1 \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 $n$ ($1 \leq n \leq 10^5$), indicating the number of skills.

The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \leq a_i \leq 1$), where $a_i$ indicates the type of the $i$-th skill. Specifically, the $i$-th skill is of type fire if $a_i = 0$, and of type frost if $a_i = 1$.

The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \leq b_i \leq 10^9$), where $b_i$ indicates the initial damage of the $i$-th skill.

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


-----Output-----

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


-----Examples-----

Input
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
Output
2112
63
3000000000
1


-----Note-----

In the first test case, we can order the skills by $[3, 1, 4, 2]$, and the total damage is $100 + 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]$, 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]$, and the total damage is $1000000000 + 1000000000 + 1000000000 = 3000000000$.

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

Difficulty: EASY
Tags: Sorting

Implementation

Greedy algorithms
Skill types: Sorting

Greedy algorithms

Starter code:

Solutions:
import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	if 1 not in a:
		print(sum(b))
	elif 0 not in a:
		print(sum(b))
	else:
		p = []
		q = []
		for i in range(n):
			if a[i] == 1:
				q.append(b[i])
			else:
				p.append(b[i])
		p = sorted(p, reverse=True)
		q = sorted(q, reverse=True)
		summ = 0
		if len(p) > len(q):
			summ += p[-1] + sum(q) * 2 + sum(p[:len(q)]) * 2 + sum(p[len(q):-1])
		elif len(p) < len(q):
			summ += q[-1] + sum(p) * 2 + sum(q[:len(p)]) * 2 + sum(q[len(p):-1])
		else:
			b.sort()
			summ += b[0] + sum(b[1:]) * 2
		print(summ)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().strip().split()))
	ls = list(map(int, input().strip().split()))
	a = l.count(0)
	b = n - a
	co = 0
	if a == n or b == n:
		co = sum(ls)
	elif a == b:
		co = sum(ls) * 2 - min(ls)
	elif a < b:
		(l0, l1) = ([], [])
		for i in range(n):
			if l[i] == 0:
				l0.append(ls[i])
			else:
				l1.append(ls[i])
		l0.sort(reverse=True)
		l1.sort(reverse=True)
		co += sum(ls)
		for i in range(a):
			co += l1[i] + l0[i]
	else:
		(l0, l1) = ([], [])
		for i in range(n):
			if l[i] == 0:
				l0.append(ls[i])
			else:
				l1.append(ls[i])
		l0.sort(reverse=True)
		l1.sort(reverse=True)
		co += sum(ls)
		for i in range(b):
			co += l1[i] + l0[i]
	print(co)


import sys
input = sys.stdin.readline

def solve():
	n = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	(t1, t2) = ([], [])
	for i in range(n):
		if A[i]:
			t1.append(B[i])
		else:
			t2.append(B[i])
	if not t1 or not t2:
		return sum(B)
	t1.sort(reverse=True)
	t2.sort(reverse=True)
	(u, v) = (len(t1), len(t2))
	m = min(u, v)
	if u != v:
		ans = sum(B) + sum(t1[:m]) + sum(t2[:m])
	else:
		ans = 2 * sum(B) - min(B)
	return ans
for _ in range(int(input())):
	print(solve())


import sys
input = sys.stdin.readline

def solve():
	n = int(input())
	arr = list(map(int, input().split()))
	power = list(map(int, input().split()))
	ans = 0
	p = list(zip(power, arr))
	p.sort(reverse=True)
	fire = arr.count(1)
	frost = arr.count(0)
	if fire == frost:
		return 2 * sum(power) - min(power)
	i = 0
	j = 0
	while i < n and j < n and fire and frost:
		while p[i][1] == 0:
			i += 1
		while 1 == p[j][1]:
			j += 1
		ans += 2 * (p[i][0] + p[j][0])
		fire -= 1
		frost -= 1
		i += 1
		j += 1
	k = 1
	if frost:
		i = j
		k = 0
	while i < n:
		if p[i][1] == k:
			ans += p[i][0]
		i += 1
	return ans
for _ in range(int(input())):
	print(solve())


import sys
import math
from collections import Counter
from collections import defaultdict

def get_ints():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_string():
	return sys.stdin.readline().strip()

def solve(a, b):
	if a.count(1) == len(a) or a.count(1) == 0:
		return sum(b)
	d = defaultdict(list)
	for i in range(len(a)):
		d[a[i]].append(b[i])
	for i in d:
		d[i].sort()
	zero = a.count(0)
	one = a.count(1)
	if zero == one:
		return 2 * sum(b) - min(b)
	ans = 0
	if one < zero:
		t = zero - one
		ind = 0
		anss = 0
		for i in range(t):
			anss += d[0][ind]
			ind += 1
		return 2 * sum(b) - anss
	else:
		t = one - zero
		ind = 0
		anss = 0
		for i in range(t):
			anss += d[1][ind]
			ind += 1
		return 2 * sum(b) - anss
t = int(input())
for _ in range(t):
	n = get_ints()
	arr = get_ints()
	arr2 = get_ints()
	print(solve(arr, arr2))


import sys

def solve():
	inp = sys.stdin.readline
	n = int(inp())
	a = list(map(int, inp().split()))
	b = list(map(int, inp().split()))
	aa = []
	bb = []
	for i in range(n):
		if a[i] == 0:
			aa.append(b[i])
		else:
			bb.append(b[i])
	aa.sort()
	bb.sort()
	if len(aa) == len(bb):
		if aa[0] <= bb[0]:
			print((sum(aa) + sum(bb)) * 2 - aa[0])
		else:
			print((sum(aa) + sum(bb)) * 2 - bb[0])
	else:
		v = min(len(aa), len(bb))
		print((sum(aa[len(aa) - v:]) + sum(bb[len(bb) - v:])) * 2 + (sum(aa[:len(aa) - v]) + sum(bb[:len(bb) - v])))

def main():
	for i in range(int(sys.stdin.readline())):
		solve()
main()


import sys, math
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	x = a.count(1)
	y = n - x
	if x == y:
		print(2 * sum(b) - min(b))
	else:
		u1 = []
		u2 = []
		for i in range(n):
			if a[i] == 1:
				u1.append(b[i])
			else:
				u2.append(b[i])
		u1.sort(reverse=True)
		u2.sort(reverse=True)
		if x > y:
			s = 2 * sum(u1[:y])
			s2 = sum(u1[y:])
			print(s + s2 + 2 * sum(u2))
		else:
			s = 2 * sum(u2[:x])
			s2 = sum(u2[x:])
			print(s + s2 + 2 * sum(u1))


import sys
import heapq
import math
input = sys.stdin.readline
sys.setrecursionlimit(100000)
from collections import Counter, defaultdict

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def main():
	length = inp()
	array = inlt()
	strength = inlt()
	powers = defaultdict(list)
	for i in range(length):
		powers[array[i]].append(strength[i])
	fire = powers[0]
	frost = powers[1]
	fire.sort()
	frost.sort()
	(N, M) = (len(fire), len(frost))
	if N == 0:
		print(sum(frost))
	elif M == 0:
		print(sum(fire))
	elif N == M:
		print(2 * sum(fire) + 2 * sum(frost) - min(fire[0], frost[0]))
	elif N > M:
		print(2 * sum(frost) + 2 * sum(fire[-M:]) + sum(fire[:-M]))
	else:
		print(2 * sum(fire) + 2 * sum(frost[-N:]) + sum(frost[:-N]))
t = inp()
for _ in range(t):
	main()


import sys
input = sys.stdin.readline

def print(*args, end='\n', sep=' ') -> None:
	sys.stdout.write(sep.join(map(str, args)) + end)

def Solve():
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	frost = []
	fire = []
	for i in range(n):
		if a[i] == 0:
			frost.append(b[i])
		else:
			fire.append(b[i])
	fil = len(fire)
	frl = len(frost)
	ans = 0
	frost.sort(reverse=True)
	fire.sort(reverse=True)
	if fil < frl:
		ans += sum(frost[:-1][:fil]) + sum(frost)
		ans += sum(fire) + sum(fire[:frl])
	elif fil > frl:
		ans += sum(fire[:-1][:frl]) + sum(fire)
		ans += sum(frost) + sum(frost[:fil])
	elif fire[-1] < frost[-1]:
		ans += sum(fire[:-1]) + sum(fire) + sum(frost) * 2
	else:
		ans += sum(frost[:-1]) + sum(frost) + sum(fire) * 2
	print(ans)
for t in range(int(input())):
	Solve()


import sys

def read_int():
	return int(sys.stdin.readline())

def read_iarray():
	return [int(i) for i in sys.stdin.readline().strip().split()]

def solve(p1, p2):
	(sr, lr) = (p2, p1)
	if len(p1) < len(p2):
		(sr, lr) = (p1, p2)
	tot = 0
	i = 0
	while len(lr) - i > len(sr):
		tot += lr[i]
		i += 1
	for j in range(len(sr)):
		tot += 2 * (sr[j] + lr[i + j])
	if len(sr) == len(lr):
		tot -= min(lr[0], sr[0])
	return tot
t = read_int()
for _ in range(t):
	n = read_int()
	skills = read_iarray()
	power = read_iarray()
	(fire, frost) = ([], [])
	for (i, v) in enumerate(power):
		if skills[i]:
			fire.append(v)
		else:
			frost.append(v)
	print(solve(sorted(fire), sorted(frost)))


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	a = list(map(int, sys.stdin.readline().split()))
	b = list(map(int, sys.stdin.readline().split()))
	aa = []
	bb = []
	for i in range(n):
		if a[i]:
			aa.append(b[i])
		else:
			bb.append(b[i])
	bb.sort(reverse=True)
	aa.sort(reverse=True)
	k = min(len(aa), len(bb))
	ans = 0
	if n % 2 == 0 and n // 2 == k:
		for i in range(k - 1):
			ans += 2 * (aa[i] + bb[i])
		ans += 2 * max(aa[-1], bb[-1])
		ans += min(aa[-1], bb[-1])
	else:
		for i in range(k):
			ans += 2 * (aa[i] + bb[i])
		for i in range(k, len(aa)):
			ans += aa[i]
		for i in range(k, len(bb)):
			ans += bb[i]
	print(ans)


import sys
input = sys.stdin.readline
t = int(input())
for tc in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	c = list(map(int, input().split()))
	total_damage = 0
	fire = [c[i] for i in range(n) if a[i] == 1]
	fire.sort(reverse=True)
	frost = [c[i] for i in range(n) if a[i] == 0]
	frost.sort(reverse=True)
	min_count = min(len(fire), len(frost))
	total_damage = 2 * (sum(fire[:min_count]) + sum(frost[:min_count]))
	if len(fire) < len(frost):
		total_damage += sum(frost[min_count:])
	elif len(fire) > len(frost):
		total_damage += sum(fire[min_count:])
	else:
		min_skill = min(min(fire), min(frost))
		total_damage -= min_skill
	print(total_damage)


import sys
readline = sys.stdin.readline
T = int(readline())
for _ in range(T):
	N = int(readline())
	(A, B) = [list(map(int, readline().split())) for _ in range(2)]
	(fire, flost) = [list() for _ in range(2)]
	for i in range(N):
		if A[i] == 0:
			fire.append(B[i])
		else:
			flost.append(B[i])
	fire.sort(reverse=True)
	flost.sort(reverse=True)
	if len(fire) > len(flost):
		ans = sum(fire[:len(flost)]) * 2 + sum(flost) * 2 + sum(fire[len(flost):])
	elif len(fire) < len(flost):
		ans = sum(fire) * 2 + sum(flost[:len(fire)]) * 2 + sum(flost[len(fire):])
	else:
		ans = sum(fire) * 2 + sum(flost) * 2 - min(fire[-1], flost[-1])
	print(ans)


t = int(input())
a = []
b = []
n = []
for i in range(t):
	n.append(int(input()))
	a.append(list(map(int, input().split())))
	b.append(list(map(int, input().split())))
for k in range(t):
	zero = []
	one = []
	cn = n[k]
	ca = a[k]
	cb = b[k]
	s = 0
	for j in range(cn):
		if ca[j] == 1:
			one.append(cb[j])
		else:
			zero.append(cb[j])
	if len(one) == 0 or len(zero) == 0:
		print(sum(cb))
	elif len(one) == len(zero):
		mb = min(cb)
		s += mb
		cb.remove(mb)
		s += sum(cb) * 2
		print(s)
	else:
		nn = min(len(zero), len(one))
		m1 = len(zero)
		m2 = len(one)
		if m1 > m2:
			c = 0
		else:
			c = 1
		zero.sort()
		one.sort()
		for item in range(nn * 2):
			if c % 2 == 0:
				mm = zero.pop()
				s += mm * 2
			else:
				mm = one.pop()
				s += mm * 2
			c += 1
		if c % 2 == 1:
			for ii in one:
				s += ii
		else:
			for jj in zero:
				s += jj
		print(s)


t = int(input())
n = []
a = []
b = []
for i in range(t):
	n.append(int(input()))
	row1 = input().split()
	row2 = input().split()
	for j in range(n[i]):
		row1[j] = int(row1[j])
		row2[j] = int(row2[j])
	a.append(row1)
	b.append(row2)
for i in range(t):
	f = []
	l = []
	for j in range(n[i]):
		if a[i][j] == 0:
			f.append(b[i][j])
		else:
			l.append(b[i][j])
	f = sorted(f, reverse=True)
	l = sorted(l, reverse=True)
	k = min(len(f), len(l))
	x = sum(2 * l[:k] + l[k:] + 2 * f[:k] + f[k:])
	if len(f) == len(l):
		x -= min(b[i])
	print(x)


from math import isqrt

def cal(a):
	if a == 0:
		return 0
	if a < 4:
		return a
	t = isqrt(a)
	ans = 3 * (t - 1)
	i = 0
	while t ** 2 + t * i <= a:
		i += 1
	return ans + i
for jj in range(int(input())):
	n = int(input())
	ty = input().split()
	dam = [int(i) for i in input().split()]
	a = ty.count('0')
	b = ty.count('1')
	if a == 0 or b == 0:
		print(sum(dam))
	elif a == b:
		dam.sort()
		print(dam[0] + 2 * sum(dam[1:]))
	else:
		ar = []
		br = []
		for i in range(n):
			if ty[i] == '0':
				ar.append(dam[i])
			else:
				br.append(dam[i])
		ar.sort()
		br.sort()
		if a > b:
			print(sum(ar[:a - b]) + 2 * (sum(ar[a - b:]) + sum(br)))
		else:
			print(sum(br[:b - a]) + 2 * (sum(ar) + sum(br[b - a:])))


def glory_addict(n, a, b):
	if n == 1:
		return b[0]
	(fire, frost) = ([], [])
	for i in range(n):
		if a[i] == 0:
			fire.append(b[i])
		else:
			frost.append(b[i])
	(sum_fire, sum_frost, sum_b) = (sum(fire), sum(frost), sum(b))
	if len(fire) == 0:
		return sum_frost
	if len(frost) == 0:
		return sum_fire
	fire.sort()
	frost.sort()
	result = 0
	common = min(len(fire), len(frost))
	if len(fire) < len(frost):
		result += sum_fire
		for j in range(common):
			result += frost[len(frost) - j - 1]
		result += sum_b
		return result
	if len(fire) == len(frost):
		result += 2 * sum_b - min(fire[0], frost[0])
		return result
	if len(fire) > len(frost):
		result += sum_frost
		for j in range(common):
			result += fire[len(fire) - j - 1]
		result += sum_b
		return result
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	print(glory_addict(n, a, b))


for i in range(int(input())):
	a = int(input())
	b = list(map(int, input().split()))
	c = list(map(int, input().split()))
	su = sum(c)
	if 0 not in b or 1 not in b:
		print(su)
	else:
		d = []
		e = []
		for i in range(a):
			if b[i] == 0:
				d.append(c[i])
			else:
				e.append(c[i])
		d.sort()
		e.sort()
		f = len(d)
		g = len(e)
		if f > g:
			print(2 * su - sum(d[:f - g]))
		elif f < g:
			print(2 * su - sum(e[:g - f]))
		else:
			print(2 * su - min(c))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	if n == 1:
		print(b[0])
	else:
		one = []
		zeros = []
		for i in range(n):
			if a[i] == 0:
				zeros.append(b[i])
			else:
				one.append(b[i])
		if not zeros:
			print(sum(b))
		elif not one:
			print(sum(b))
		else:
			one.sort()
			one = one[::-1]
			zeros.sort()
			zeros = zeros[::-1]
			if len(one) == len(zeros):
				print(2 * sum(one) + 2 * sum(zeros) - min(min(one), min(zeros)))
			elif not one:
				print(sum(zeros))
			elif not zeros:
				print(sum(one))
			elif len(one) < len(zeros):
				print(2 * sum(one) + 2 * sum(zeros[:len(one)]) + sum(zeros[len(one):]))
			else:
				print(2 * sum(zeros) + 2 * sum(one[:len(zeros)]) + sum(one[len(zeros):]))


times = int(input())
for i in range(times):
	length = int(input())
	skills = list(map(int, input().split()))
	damage = list(map(int, input().split()))
	if 1 in skills and 0 in skills:
		fire = skills.count(0)
		frost = skills.count(1)
		fireDamage = [damage[j] for j in range(len(damage)) if skills[j] == 0]
		frostDamage = [damage[j] for j in range(len(damage)) if skills[j] == 1]
		frostDamage.sort()
		fireDamage.sort()
		t = min(frost, fire)
		s = 0
		if fire == frost:
			s = s - min(damage) + 2 * sum(damage)
		else:
			s = s + 2 * sum(fireDamage[fire - t:]) + 2 * sum(frostDamage[frost - t:]) + sum(fireDamage[:fire - t]) + sum(frostDamage[:frost - t])
		print(s)
	else:
		print(sum(damage))


def help(A, B):
	FI = []
	FR = []
	for i in range(len(A)):
		if A[i] == 0:
			FI.append(B[i])
		else:
			FR.append(B[i])
	return (sorted(FI, reverse=True), sorted(FR, reverse=True))

def solve(FI, FR):
	ans = 0
	i = min(len(FI), len(FR))
	ans += 2 * (sum(FI[:i]) + sum(FR[:i]))
	if len(FI) > len(FR):
		ans += 2 * FI[i]
		ans += sum(FI[i + 1:])
	else:
		ans += sum(FR[i:])
	return ans
N = int(input())
for _ in range(N):
	n = int(input())
	A = [int(x) for x in input().split(' ')]
	B = [int(x) for x in input().split(' ')]
	if sum(A) == 0 or sum(A) == len(A):
		print(sum(B))
		continue
	(FI, FR) = help(A, B)
	v1 = solve(FI, FR[:-1]) + FR[-1] if len(FR) > 0 else 0
	v2 = solve(FR, FI[:-1]) + FI[-1] if len(FI) > 0 else 0
	print(f'{max(v1, v2)}')


t = int(input())
for q in range(t):
	n = int(input())
	(lfr, lfi) = ([], [])
	l1 = input().split()
	l2 = input().split()
	dmg = 0
	for j in range(n):
		if l1[j] == '1':
			lfr.append(int(l2[j]))
		else:
			lfi.append(int(l2[j]))
	if len(lfi) == 0:
		for k in lfr:
			dmg += k
	elif len(lfr) == 0:
		for k in lfi:
			dmg += k
	else:
		lfi.sort()
		lfr.sort()
		if len(lfi) == len(lfr):
			ld1 = min(lfi)
			ld2 = min(lfr)
			if ld1 > ld2:
				dmg -= ld2
				for k in range(int(n / 2)):
					dmg += 2 * (lfi[k] + lfr[k])
			else:
				dmg -= ld1
				for k in range(int(n / 2)):
					dmg += 2 * (lfi[k] + lfr[k])
		else:
			a = len(lfr)
			b = len(lfi)
			if b > a:
				for k1 in range(b - a):
					dmg += lfi[k1]
				for k2 in range(a):
					dmg += 2 * (lfr[k2] + lfi[b - a + k2])
			else:
				for k1 in range(a - b):
					dmg += lfr[k1]
				for k2 in range(b):
					dmg += 2 * (lfi[k2] + lfr[a - b + k2])
	print(dmg)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	o = a.count(1)
	z = a.count(0)
	if o == z:
		print(2 * sum(b) - min(b))
		continue
	if not o or not z:
		print(sum(b))
		continue
	mx = sorted(zip(a, b), key=lambda x: x[1])
	ans = 0
	sign = 1 if o > z else 0
	_min = max(o, z) - min(o, z)
	for i in range(n):
		if _min and mx[i][0] == sign:
			ans += mx[i][1]
			_min -= 1
		else:
			ans += 2 * mx[i][1]
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	if 1 not in a or 0 not in a:
		print(sum(b))
	else:
		z = []
		o = []
		for i in range(n):
			if a[i] == 0:
				z.append(b[i])
			else:
				o.append(b[i])
		z.sort()
		o.sort()
		s = 0
		if len(z) == len(o):
			s = min(z[0], o[0]) + 2 * max(z[0], o[0])
			for i in range(1, len(z)):
				s = s + 2 * z[i] + 2 * o[i]
			print(s)
		elif len(z) < len(o):
			while len(z) != 0:
				s = s + 2 * o.pop()
				s = s + 2 * z.pop()
			s = s + sum(o)
			print(s)
		else:
			while len(o) != 0:
				s = s + 2 * z.pop()
				s = s + 2 * o.pop()
			s = s + sum(z)
			print(s)


import collections

def max_uron(a, b, n):
	if len(a) == 1:
		print(b[0])
	elif len(set(a)) == 1:
		print(sum(b))
	else:
		c = collections.Counter(a)
		res = 0
		b_maxc = [0 for _ in range(n)]
		mincount = 0 if c[1] > c[0] else 1
		if c[0] == c[1]:
			m = min(b)
			res = 2 * sum(b) - m
		elif c[1] > c[0] or c[1] < c[0]:
			for i in range(n):
				if a[i] == mincount:
					res += 2 * b[i]
				else:
					b_maxc[i] = b[i]
			b_maxc.sort(reverse=True)
			for i in range(c[mincount]):
				res += 2 * b_maxc[i]
			res += sum(b_maxc[c[mincount]:])
		print(res)
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	max_uron(a, b, n)


def A():
	inp = int(input())
	for _ in range(inp):
		n = int(input())
		a = [int(x) for x in input().split(' ')]
		b = [int(y) for y in input().split(' ')]
		fire = []
		frost = []
		for i in range(len(a)):
			if a[i] == 0:
				fire.append(b[i])
			else:
				frost.append(b[i])
		fire.sort()
		frost.sort()
		if len(fire) == 0:
			print(sum(frost))
			continue
		if len(frost) == 0:
			print(sum(fire))
			continue
		ans = 0
		if len(fire) > len(frost):
			ans += 2 * sum(frost)
			ans += 2 * sum(fire[-len(frost):])
			ans += sum(fire[:-len(frost)])
		elif len(fire) < len(frost):
			ans += 2 * sum(fire)
			ans += 2 * sum(frost[-len(fire):])
			ans += sum(frost[:-len(fire)])
		elif len(fire) == len(frost):
			ans += 2 * sum(fire)
			ans += 2 * sum(frost)
			ans -= fire[0] if fire[0] <= frost[0] else frost[0]
		print(ans)
A()


I = input
for _ in [0] * int(I()):
	I()
	c = [[], []]
	for (x, y) in zip(I()[::2], I().split()):
		c[x > '0'] += (int(y),)
	(a, b) = sorted(c, key=len)
	l = len(a)
	c = sorted(b)[::-1][:l]
	print(sum(a * 2 + b + c) - (l == len(b)) * min(a + b))


def func(skill, demage):
	fire = []
	frost = []
	for i in range(len(demage)):
		if skill[i] == 0:
			fire.append(demage[i])
		else:
			frost.append(demage[i])
	demage.sort()
	fire.sort(reverse=True)
	frost.sort(reverse=True)
	if len(fire) == 0 or len(frost) == 0:
		return sum(demage)
	elif len(fire) == len(frost):
		return 2 * sum(demage) - demage[0]
	elif len(fire) > len(frost):
		return 2 * sum(frost) + 2 * sum(fire[:len(frost)]) + sum(fire[len(frost):])
	elif len(fire) < len(frost):
		return 2 * sum(fire) + 2 * sum(frost[:len(fire)]) + sum(frost[len(fire):])
num_test = int(input())
for test in range(num_test):
	n = input()
	skill = [int(x) for x in input().split(' ')]
	demage = [int(x) for x in input().split(' ')]
	print(func(skill, demage))


t = int(input())
for _ in range(0, t):
	n = int(input())
	list1 = [int(num) for num in input().split()]
	list2 = [int(num) for num in input().split()]
	zeros = []
	ones = []
	zl = 0
	ol = 0
	c = 0
	for i in range(0, n):
		if list1[i] == 0:
			zeros.append(list2[i])
			zl += 1
		else:
			ones.append(list2[i])
			ol += 1
	zeros.sort(reverse=True)
	ones.sort(reverse=True)
	if zl == 0:
		print(sum(ones))
	elif ol == 0:
		print(sum(zeros))
	else:
		if zl == ol:
			if ones[ol - 1] <= zeros[ol - 1]:
				c += ones[ol - 1]
				c += zeros[0] * 2
				for i in range(0, ol - 1):
					c += ones[i] * 2
					c += zeros[i + 1] * 2
			else:
				c += zeros[ol - 1]
				c += ones[0] * 2
				for i in range(0, ol - 1):
					c += zeros[i] * 2
					c += ones[i + 1] * 2
		else:
			if zl < ol:
				minl = zl
				maxl = ol
				mina = zeros
				maxa = ones
			else:
				minl = ol
				maxl = zl
				mina = ones
				maxa = zeros
			c += maxa[maxl - 1]
			j = 0
			for i in range(0, minl):
				c += mina[i] * 2
				c += maxa[i] * 2
				j += 1
			i = j
			for i in range(j, maxl - 1):
				c += maxa[i]
		print(c)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	zr = []
	on = []
	cz = 0
	co = 0
	for i in range(n):
		if a[i] == 0:
			cz += 1
			zr.append(b[i])
		else:
			co += 1
			on.append(b[i])
	if cz == 0:
		print(sum(on))
	elif co == 0:
		print(sum(zr))
	else:
		zr.sort()
		on.sort()
		if cz == co:
			if zr[0] < on[0]:
				sm = 0
				sm += zr[0]
				sm += 2 * (sum(zr[1:]) + sum(on))
				print(sm)
			else:
				sm = 0
				sm += on[0]
				sm += 2 * (sum(zr) + sum(on[1:]))
				print(sm)
		else:
			sm = 0
			while len(zr) and len(on):
				sm += zr.pop()
				sm += on.pop()
			sm *= 2
			while len(zr):
				sm += zr.pop()
			while len(on):
				sm += on.pop()
			print(sm)


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(ii) for ii in input().split()]
	b = [int(ii) for ii in input().split()]
	(x, y) = ([], [])
	for j in range(n):
		if a[j] == 0:
			x.append(b[j])
		else:
			y.append(b[j])
	(lx, ly) = (len(x), len(y))
	if lx == 0 or ly == 0:
		print(sum(b))
		continue
	x.sort(reverse=True)
	y.sort(reverse=True)
	if lx == ly:
		m = min(x[-1], y[-1])
		print(2 * sum(b) - m)
	elif lx < ly:
		print(2 * sum(x[:lx]) + 2 * sum(y[:lx]) + sum(y[lx:]))
	else:
		print(2 * sum(x[:ly]) + 2 * sum(y[:ly]) + sum(x[ly:]))


from collections import deque

def foo(n, a, b):
	l1 = []
	l2 = []
	for i in range(n):
		if a[i] == 0:
			l1.append(b[i])
		else:
			l2.append(b[i])
	l1.sort()
	l2.sort()
	ans = 0
	while l1 and l2:
		ans += 2 * (l1.pop() + l2.pop())
	if l1:
		ans += sum(l1)
	elif l2:
		ans += sum(l2)
	else:
		ans -= min(b)
	return ans
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	b = [int(i) for i in input().split()]
	print(foo(n, a, b))


a = int(input())
for y in range(a):
	b = int(input())
	c = list(map(int, input().split()))
	d = list(map(int, input().split()))
	j = []
	h = []
	for z in range(b):
		if c[z] == 0:
			j.append(d[z])
		else:
			h.append(d[z])
	if h == [] or j == []:
		if h == []:
			print(sum(j))
		else:
			print(sum(h))
	else:
		h.sort()
		j.sort()
		k = len(h)
		l = len(j)
		if k == l:
			s = sum(h) + sum(j)
			print(max(2 * s - h[0], 2 * s - j[0]))
		elif k < l:
			print(2 * (sum(h) + sum(j[l - k:])) + sum(j[:l - k]))
		else:
			print(2 * (sum(j) + sum(h[k - l:])) + sum(h[:k - l]))


t = int(input())
while t > 0:
	n = int(input())
	a = list(map(int, input().split(' ')))
	b = list(map(int, input().split(' ')))
	fire = []
	frost = []
	for (x, y) in zip(a, b):
		if x == 0:
			fire.append(y)
		else:
			frost.append(y)
	fire.sort()
	frost.sort()
	fire_len = len(fire)
	frost_len = len(frost)
	ans = 0
	if fire_len == frost_len:
		ans -= min(fire[0], frost[0])
	while fire and frost:
		ans += (fire.pop() + frost.pop()) * 2
	for x in fire:
		ans += x
	for x in frost:
		ans += x
	print(ans)
	t -= 1


for i in range(int(input())):
	n = int(input())
	f_s = list(map(int, input().split()))
	value_list = list(map(int, input().split()))
	fire = []
	frost = []
	for i in range(n):
		if f_s[i] == 1:
			fire.append(value_list[i])
		else:
			frost.append(value_list[i])
	fire_len = len(fire)
	frost_len = len(frost)
	if fire_len < 1 or frost_len < 1:
		print(sum(fire) + sum(frost))
	else:
		dif = abs(fire_len - frost_len)
		if fire_len > frost_len:
			fire = sorted(fire)
			ans = 2 * sum(frost) + 2 * sum(fire[-frost_len:]) + sum(fire[:dif])
		elif fire_len < frost_len:
			frost = sorted(frost)
			ans = 2 * sum(fire) + 2 * sum(frost[-fire_len:]) + sum(frost[:dif])
		else:
			ans = 2 * sum(fire) + 2 * sum(frost) - min(value_list)
		print(ans)


t = int(input())

def make_list(first, c) -> int:
	ans = 0
	c = c[:]
	for i in range(len(c) - 1, -1, -1):
		if c[i][1] == first:
			ans += c[i][0]
			c.pop(i)
			break
	cnt = [0, 0]
	cnt[first] = 1
	for (y, x) in c:
		cnt[x] += 1
	for (y, x) in c:
		if cnt[1 - x]:
			ans += 2 * y
			cnt[1 - x] -= 1
		else:
			ans += y
	return ans
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	d = [(b[i], a[i]) for i in range(n)]
	d.sort(reverse=True)
	if sum(a) == n or sum(a) == 0:
		print(sum(b))
	else:
		print(max(make_list(0, d), make_list(1, d)))


t = int(input())
for _ in range(t):
	r = int(input())
	c = []
	d = []
	e = []
	f = []
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	for i in range(r):
		if a[i] == 0:
			c.append(b[i])
			e.append(b[i])
		else:
			d.append(b[i])
			f.append(b[i])
	c.sort()
	d.sort()
	e.sort()
	f.sort()
	if len(d) == 0:
		x = sum(c)
		print(x)
		continue
	if len(c) == 0:
		x = sum(d)
		print(x)
		continue
	x = c[0] + 2 * d[-1]
	y = f[0] + 2 * e[-1]
	c.pop(0)
	d.pop(-1)
	f.pop(0)
	e.pop(-1)
	c.reverse()
	d.reverse()
	e.reverse()
	f.reverse()
	while c != [] and d != []:
		x += 2 * d[0] + 2 * c[0]
		c.pop(0)
		d.pop(0)
	if c == []:
		if len(d) > 0:
			x += sum(d)
	elif len(c) > 0:
		x += sum(c) + c[0]
	while e != [] and f != []:
		y += 2 * e[0] + 2 * f[0]
		e.pop(0)
		f.pop(0)
	if e == []:
		if len(f) > 0:
			y += sum(f) + f[0]
	elif len(e) > 0:
		y += sum(e)
	print(max(x, y))


def solve():
	input()
	skills = [int(x) for x in input().split()]
	damages = [int(x) for x in input().split()]
	fire = []
	frost = []
	for (skill, damage) in zip(skills, damages):
		if skill == 0:
			fire.append(damage)
		else:
			frost.append(damage)
	fire.sort()
	frost.sort()
	full_damages = fire + frost
	lfi = len(fire)
	lfr = len(frost)
	if lfi == lfr:
		print(sum(full_damages) * 2 - min(full_damages))
	else:
		s = 0
		for _ in range(min(lfi, lfr)):
			s += (fire.pop() + frost.pop()) * 2
		s += sum(fire) + sum(frost)
		print(s)
for _ in range(int(input())):
	solve()


test = int(input())
for t in range(test):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	fire = list()
	frost = list()
	fire_num = 0
	frost_num = 0
	for i in range(n):
		if a[i] == 0:
			fire.append(b[i])
			fire_num += 1
		else:
			frost.append(b[i])
			frost_num += 1
	fire.sort()
	frost.sort()
	if fire_num == 0:
		score = sum(frost)
	elif frost_num == 0:
		score = sum(fire)
	elif frost_num > fire_num:
		score = frost.pop(0)
		frost_num -= 1
		score += sum(fire) * 2 + sum(frost[-fire_num:]) * 2 + sum(frost[:frost_num - fire_num])
	elif frost_num < fire_num:
		score = fire.pop(0)
		fire_num -= 1
		score += sum(frost) * 2 + sum(fire[-frost_num:]) * 2 + sum(fire[:fire_num - frost_num])
	else:
		if fire[0] < frost[0]:
			score = fire.pop(0)
		else:
			score = frost.pop(0)
		score += sum(frost) * 2 + sum(fire) * 2
	print(score)


k = int(input())
for i in range(k):
	m = int(input())
	x = list(map(int, input().split()))
	y = list(map(int, input().split()))
	if min(x) == max(x):
		print(sum(y))
		continue
	zeros = []
	ones = []
	for (m, n) in zip(x, y):
		if m == 0:
			zeros.append(n)
		else:
			ones.append(n)
	res = 0
	if len(ones) == len(zeros):
		res += 2 * sum(ones)
		res += 2 * sum(zeros)
		res -= min(min(zeros), min(ones))
		print(res)
		continue
	zeros.sort(reverse=True)
	ones.sort(reverse=True)
	if len(zeros) > len(ones):
		(ones, zeros) = (zeros, ones)
	if len(ones) > len(zeros):
		for i in range(len(zeros)):
			res += 2 * zeros[0]
			zeros.pop(0)
			res += 2 * ones[0]
			ones.pop(0)
		res += sum(ones)
	print(res)


for asdasdas in range(int(input())):
	n = int(input())
	items_a = [int(i) for i in input().split()]
	items_dict = {0: [], 1: []}
	for (index, elem) in enumerate(input().split()):
		items_dict[items_a[index]].append(int(elem))
	items_dict[0].sort()
	items_dict[1].sort()
	c = 0
	if len(items_dict[1]) == 0:
		print(sum(items_dict[0]))
	elif len(items_dict[0]) == 0:
		print(sum(items_dict[1]))
	elif len(items_dict[1]) == len(items_dict[0]):
		min_0 = items_dict[0][0]
		min_1 = items_dict[1][0]
		if min_0 < min_1:
			c = items_dict[0].pop(0)
		else:
			c = items_dict[1].pop(0)
		print(c + 2 * sum(items_dict[0] + items_dict[1]))
	elif len(items_dict[1]) > len(items_dict[0]):
		c = sum(items_dict[0]) * 2 + sum(items_dict[1][-len(items_dict[0]):]) * 2 + sum(items_dict[1][:-len(items_dict[0])])
		print(c)
	else:
		c = sum(items_dict[1]) * 2 + sum(items_dict[0][-len(items_dict[1]):]) * 2 + sum(items_dict[0][:-len(items_dict[1])])
		print(c)


import math
import sys

def solve():
	n = int(input())
	ind = [int(x) for x in input().split()]
	val = [int(x) for x in input().split()]
	zeros = []
	ones = []
	for i in range(n):
		if ind[i] == 0:
			zeros.append(val[i])
		else:
			ones.append(val[i])
	zeros.sort()
	ones.sort()
	re = 0
	if len(zeros) == len(ones):
		if zeros[0] >= ones[0]:
			re = ones[0]
			ones[0] = 0
		else:
			re = zeros[0]
			zeros[0] = 0
		ans = sum(zeros) * 2 + sum(ones) * 2 + re
		print(ans)
	elif len(zeros) > len(ones):
		print(cal(ones, zeros))
	else:
		print(cal(zeros, ones))

def cal(arr1, arr2):
	ans = sum(arr1) * 2 + sum(arr2[len(arr2) - len(arr1):]) * 2 + sum(arr2[:len(arr2) - len(arr1)])
	return ans

def main():
	t = int(input())
	for _ in range(t):
		solve()
main()


t = int(input())
while t != 0:
	n = int(input())
	arrA = list(map(int, input().split()))
	arrB = list(map(int, input().split()))
	frost = []
	fire = []
	for i in range(n):
		if arrA[i] == 0:
			fire.append(arrB[i])
		else:
			frost.append(arrB[i])
	fire.sort()
	frost.sort()
	res = 0
	if len(fire) == 0:
		res = sum(frost)
	elif len(frost) == 0:
		res = sum(fire)
	else:
		rr = len(fire) - 1
		rf = len(frost) - 1
		if rf == rr:
			while rr >= 1 and rf >= 1:
				res += 2 * fire[rr] + 2 * frost[rf]
				rr -= 1
				rf -= 1
			if fire[rr] > frost[rf]:
				res += 2 * fire[rr] + frost[rf]
			else:
				res += 2 * frost[rf] + fire[rr]
		else:
			while rr >= 0 and rf >= 0:
				res += 2 * fire[rr] + 2 * frost[rf]
				rr -= 1
				rf -= 1
			if rr == -1:
				for i in range(0, rf + 1):
					res += frost[i]
			else:
				for i in range(0, rr + 1):
					res += fire[i]
	print(res)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	x = []
	y = []
	for i in range(n):
		if a[i] == 0:
			x.append(b[i])
		else:
			y.append(b[i])
	x.sort(reverse=True)
	y.sort(reverse=True)
	ff = 0
	b.sort()
	if len(x) == 0:
		print(sum(y))
		continue
	elif len(y) == 0:
		print(sum(x))
		continue
	if len(x) < len(y):
		for i in range(len(x)):
			ff += 2 * x[i]
			ff += 2 * y[i]
		ff += sum(y[len(x):])
		print(ff)
	elif len(y) < len(x):
		for i in range(len(y)):
			ff += 2 * x[i]
			ff += 2 * y[i]
		ff += sum(x[len(y):])
		print(ff)
	else:
		print(2 * sum(b) - b[0])


H = input
for _ in [0] * int(H()):
	H()
	r = [[], []]
	for (x, y) in zip(H()[::2], H().split()):
		r[x > '0'] += (int(y),)
	(p, q) = sorted(r, key=len)
	l = len(p)
	r = sorted(q)[::-1][:l]
	print(sum(p * 2 + q + r) - (l == len(q)) * min(p + q))


n = int(input())
for y in range(n):
	number = int(input())
	types = input().split()
	array = input().split()
	fire = []
	frost = []
	for x in range(number):
		if types[x] == '0':
			fire.append(int(array[x]))
		else:
			frost.append(int(array[x]))
	fire.sort(reverse=True)
	frost.sort(reverse=True)
	total = 0
	if len(fire) < len(frost):
		total += 2 * sum(fire)
		total += 2 * sum(frost[:len(fire)])
		total += sum(frost[len(fire):])
		print(total)
	elif len(fire) == len(frost):
		if min(fire) < min(frost):
			total += min(fire)
			total += 2 * sum(fire[:len(fire) - 1]) + 2 * sum(frost)
		else:
			total += min(frost)
			total += 2 * sum(frost[:len(frost) - 1]) + 2 * sum(fire)
		print(total)
	else:
		total += 2 * sum(frost)
		total += 2 * sum(fire[:len(frost)])
		total += sum(fire[len(frost):])
		print(total)


n = int(input())
for i in range(n):
	m = int(input())
	v1 = list(map(int, input().split()))
	v2 = list(map(int, input().split()))
	v3 = []
	v4 = []
	p = 0
	q = 0
	for j in range(m):
		if v1[j] == 1:
			v3.append(v2[j])
			p += 1
			q += v2[j]
		else:
			v4.append(v2[j])
			q += v2[j]
	if p == 0 or p == m:
		print(q)
	elif p == m - p:
		v3.sort()
		v4.sort()
		q *= 2
		if v3[0] > v4[0]:
			print(q - v4[0])
		else:
			print(q - v3[0])
	elif p > m - p:
		v3.sort()
		b = 0
		for j in range(2 * p - m):
			b += v3[j]
		for j in range(2 * p - m, p):
			b += 2 * v3[j]
		for j in range(m - p):
			b += 2 * v4[j]
		print(b)
	else:
		v4.sort()
		b = 0
		for j in range(m - 2 * p):
			b += v4[j]
		for j in range(m - 2 * p, m - p):
			b += 2 * v4[j]
		for j in range(p):
			b += 2 * v3[j]
		print(b)


test_cases = int(input())
for t in range(test_cases):
	n = int(input())
	damage_fires = []
	damage_frost = []
	skills = [int(x) for x in input().split(' ')]
	counter = 0
	for x in input().split(' '):
		if skills[counter] == 0:
			damage_frost.append(int(x))
		else:
			damage_fires.append(int(x))
		counter += 1
	damage_frost.sort()
	damage_fires.sort()
	max_dmg = sum(damage_fires) + sum(damage_frost)
	sum_ = sum(skills)
	if sum_ == 0 or sum_ == n:
		print(max_dmg)
	elif sum_ == n / 2:
		max_dmg += max_dmg - min(min(damage_fires), min(damage_frost))
		print(max_dmg)
	elif sum_ > n / 2:
		max_dmg += sum(damage_frost)
		max_dmg += sum(damage_fires[-len(damage_frost):])
		print(max_dmg)
	else:
		max_dmg += sum(damage_fires)
		max_dmg += sum(damage_frost[-len(damage_fires):])
		print(max_dmg)


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d = list(map(int, input().split()))
	a0 = []
	a1 = []
	dmg = 0
	for j in range(n):
		if a[j] == 0:
			a0.append(d[j])
		if a[j] == 1:
			a1.append(d[j])
	a0.sort()
	a1.sort()
	if len(a0) == 0 or len(a1) == 0:
		if len(d) != 0:
			dmg = sum(d)
	elif len(a0) != len(a1):
		if len(a1) > len(a0):
			for x in range(len(a1)):
				if x < len(a1) - len(a0):
					dmg += a1[x]
				else:
					dmg += 2 * a1[x]
			dmg += 2 * sum(a0)
		else:
			for x in range(len(a0)):
				if x < len(a0) - len(a1):
					dmg += a0[x]
				else:
					dmg += 2 * a0[x]
			dmg += 2 * sum(a1)
	elif len(a0) == len(a1):
		if min(a1) < min(a0):
			dmg += a1[0]
			dmg += 2 * sum(a0)
			dmg += 2 * sum(a1[1:])
		else:
			dmg += a0[0]
			dmg += 2 * sum(a1)
			dmg += 2 * sum(a0[1:])
	print(dmg)


G = input
srg = sorted
for _ in [0] * int(G()):
	G()
	f = [[], []]
	for (g, h) in zip(G()[::2], G().split()):
		f[g > '0'] += (int(h),)
	(f, e) = srg(f, key=len)
	l = len(f)
	print(sum(f * 2 + e + srg(e)[::-1][:l]) - (l == len(e)) * min(f + e))


for i in range(int(input())):
	n = int(input())
	values1 = list(map(int, input().split()))
	values2 = list(map(int, input().split()))
	z = []
	o = []
	zc = 0
	oc = 0
	j = 0
	while j < n:
		if values1[j] == 0:
			z.append(values2[j])
			zc += 1
		else:
			o.append(values2[j])
			oc += 1
		j += 1
	z.sort()
	o.sort()
	if zc == oc:
		if z[0] < o[0]:
			print(2 * (sum(o) + sum(z)) - z[0])
		else:
			print(2 * (sum(o) + sum(z)) - o[0])
	elif zc > oc:
		print(sum(z[0:zc - oc]) + 2 * (sum(o) + sum(z[zc - oc:])))
	else:
		print(sum(o[0:oc - zc]) + 2 * (sum(z) + sum(o[oc - zc:])))


def main():
	t = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	fire = []
	frost = []
	for i in range(t):
		if a[i]:
			frost.append(b[i])
		else:
			fire.append(b[i])
	fire.sort()
	frost.sort()
	n = len(fire)
	m = t - n
	ans = 0
	sn = sum(fire)
	sm = sum(frost)
	if n == 0 or m == 0:
		return sn + sm
	if n > m:
		ans += 2 * sm
		ans += sn
		ans += sum(fire[-m:])
		return ans
	elif m > n:
		ans += 2 * sn
		ans += sm
		ans += sum(frost[-n:])
		return ans
	ans += 2 * sm
	ans += 2 * sn
	ans -= min(fire[0], frost[0])
	return ans
for _ in range(int(input())):
	print(main())


for _ in range(int(input())):
	n = int(input())
	L = list(map(int, input().split()))
	M = list(map(int, input().split()))
	A = []
	B = []
	for i in range(n):
		if L[i] == 0:
			A.append(M[i])
		else:
			B.append(M[i])
	A.sort(reverse=True)
	B.sort(reverse=True)
	a = len(A)
	b = len(B)
	if a == 0:
		print(sum(B))
		continue
	if b == 0:
		print(sum(A))
		continue
	if a < b:
		ans = B[-1]
		B.pop()
		for i in range(len(A)):
			ans += 2 * (A[i] + B[i])
		for i in range(len(A), len(B)):
			ans += B[i]
	elif b < a:
		ans = A[-1]
		A.pop()
		for i in range(len(B)):
			ans += 2 * (B[i] + A[i])
		for i in range(len(B), len(A)):
			ans += A[i]
	else:
		ans = 2 * sum(M)
		ans -= min(M)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	type = list(map(int, input().split()))
	skill = list(map(int, input().split()))
	ones = []
	zeros = []
	for i in range(n):
		if type[i] == 0:
			zeros.append(skill[i])
		else:
			ones.append(skill[i])
	ones.sort(reverse=True)
	zeros.sort(reverse=True)
	if len(ones) == len(zeros):
		t = sum(ones) * 2 + sum(zeros) * 2 - min(ones[-1], zeros[-1])
		print(t)
	elif len(ones) < len(zeros):
		k = len(ones)
		if k == 0:
			t = sum(zeros)
		else:
			t = sum(ones) * 2 + sum(zeros[:k]) * 2 + sum(zeros[k:])
		print(t)
	else:
		k = len(zeros)
		if k == 0:
			t = sum(ones)
		else:
			t = sum(zeros) * 2 + sum(ones[:k]) * 2 + sum(ones[k:])
		print(t)


arr = input
s = sorted
for _ in [0] * int(arr()):
	arr()
	a = [[], []]
	for (x, y) in zip(arr()[::2], arr().split()):
		a[x > '0'] += (int(y),)
	(a, b) = s(a, key=len)
	l = len(a)
	print(sum(a * 2 + b + s(b)[::-1][:l]) - (l == len(b)) * min(a + b))


I = lambda : int(input())
Is = lambda : map(int, input().split())
Li = lambda : list(map(int, input().split()))

def solve():
	n = I()
	a = Li()
	b = Li()
	hashMap0 = []
	sum1 = 0
	hashMap1 = []
	for i in range(n):
		if a[i] == 1:
			hashMap1.append(b[i])
		else:
			hashMap0.append(b[i])
		sum1 += b[i] * 2
	hashMap1.sort(reverse=True)
	hashMap0.sort(reverse=True)
	n0 = len(hashMap0)
	n1 = n - n0
	if n1 == n0:
		min_val = min(hashMap0[-1], hashMap1[-1])
		ans = sum1 - min_val
	elif n1 > n0:
		ans = sum1 - sum(hashMap1[n0:])
	else:
		ans = sum1 - sum(hashMap0[n1:])
	print(ans)
	return 1
t = I()
while t:
	solve()
	t -= 1