CF1746B.Rebellion

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

Rebellion

You have an array aa of size nn consisting only of zeroes and ones. You can do the following operation:

  • choose two indices 1i,jn1 \le i , j \le n, iji \ne j,
  • add aia_{i} to aja_{j},
  • remove aia_{i} from aa.

Note that elements of aa can become bigger than 11 after performing some operations. Also note that nn becomes 11 less after the operation.

What is the minimum number of operations needed to make aa non-decreasing, i. e. that each element is not less than the previous element?

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1t1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains an integer nn (1n1051 \le n \le 10^5), the size of array aa.

Next line contains nn integers a1,a2,ana_{1}, a_{2}, \ldots a_{n} (aia_i is 00 or 11), elements of array aa.

It's guaranteed that sum of nn over all test cases doesn't exceed 21052 \cdot 10^5.

Output

For each test case print a single integer, minimum number of operations needed to make aa non-decreasing.

Note

In the first test case, aa is already non-decreasing, so you don't need to do any operations and the answer is 00.

In the second test case, you can perform an operation for i=1i = 1 and j=5j = 5, so aa will be equal to [0,0,1,2][0, 0, 1, 2] and it becomes non-decreasing.

In the third test case, you can perform an operation for i=2i = 2 and j=1j = 1, so aa will be equal to [1][1] and it becomes non-decreasing.

Samples

4
8
0 0 1 1 1 1 1 1
5
1 0 0 1 1
2
1 0
11
1 1 0 0 1 0 0 1 1 1 0
0
1
1
3

在线编程 IDE

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