CF1993B.Parity and Sum

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

Parity and Sum

Given an array aa of nn positive integers.

In one operation, you can pick any pair of indexes (i,j)(i, j) such that aia_i and aja_j have distinct parity, then replace the smaller one with the sum of them. More formally:

  • If ai<aja_i \lt a_j, replace aia_i with ai+aja_i + a_j;
  • Otherwise, replace aja_j with ai+aja_i + a_j.

Find the minimum number of operations needed to make all elements of the array have the same parity.

Input

The first line contains a single integer tt (1t1041 \le t \le 10^4) — the number of test cases.

The first line of each test case contains a single integer nn (1n21051 \le n \le 2 \cdot 10^5).

The second line contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (1ai1091 \le a_i \le 10^9) — the elements of array aa.

It is guaranteed that the sum of nn over all test cases does not exceed 21052 \cdot 10^5.

Output

For each test case, output a single integer — the minimum number of operations required.

Note

In the first test case, all integers already have the same parity. Therefore, no operation is needed.

In the third test case, we can perform two operations (1,2)(1, 2) and (1,3)(1, 3). The array aa transforms as follows: $a = [\color{red}2, \color{red}3, 4] \longrightarrow [\color{red}5, 3, \color{red}4] \longrightarrow [5, 3, 9]$.

In the fourth test case, an example of an optimal sequence of operations is (1,2)(1, 2), (1,3)(1, 3), (1,4)(1, 4), and (1,4)(1, 4). The array aa transforms as follows: $a = [\color{red}3, \color{red}2, 2, 8] \longrightarrow [\color{red}3, 5, \color{red}2, 8] \longrightarrow [\color{red}3, 5, 5, \color{red}8] \longrightarrow [\color{red}{11}, 5, 5, \color{red}8] \longrightarrow [11, 5, 5, 19]$.

Samples

7
5
1 3 5 7 9
4
4 4 4 4
3
2 3 4
4
3 2 2 8
6
4 3 6 1 2 1
6
3 6 1 2 1 2
5
999999996 999999997 999999998 999999999 1000000000
0
0
2
4
3
3
3

在线编程 IDE

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