CF1433B.Yet Another Bookshelf

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

Yet Another Bookshelf

There is a bookshelf which can fit nn books. The ii-th position of bookshelf is ai=1a_i = 1 if there is a book on this position and ai=0a_i = 0 otherwise. It is guaranteed that there is at least one book on the bookshelf.

In one move, you can choose some contiguous segment [l;r][l; r] consisting of books (i.e. for each ii from ll to rr the condition ai=1a_i = 1 holds) and:

  • Shift it to the right by 11: move the book at index ii to i+1i + 1 for all lirl \le i \le r. This move can be done only if r+1nr+1 \le n and there is no book at the position r+1r+1.
  • Shift it to the left by 11: move the book at index ii to i1i-1 for all lirl \le i \le r. This move can be done only if l11l-1 \ge 1 and there is no book at the position l1l-1.

Your task is to find the minimum number of moves required to collect all the books on the shelf as a contiguous (consecutive) segment (i.e. the segment without any gaps).

For example, for a=[0,0,1,0,1]a = [0, 0, 1, 0, 1] there is a gap between books (a4=0a_4 = 0 when a3=1a_3 = 1 and a5=1a_5 = 1), for a=[1,1,0]a = [1, 1, 0] there are no gaps between books and for a=[0,0,0]a = [0, 0,0] there are also no gaps between books.

You have to answer tt independent test cases.

Input

The first line of the input contains one integer tt (1t2001 \le t \le 200) — the number of test cases. Then tt test cases follow.

The first line of the test case contains one integer nn (1n501 \le n \le 50) — the number of places on a bookshelf. The second line of the test case contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (0ai10 \le a_i \le 1), where aia_i is 11 if there is a book at this position and 00 otherwise. It is guaranteed that there is at least one book on the bookshelf.

Output

For each test case, print one integer: the minimum number of moves required to collect all the books on the shelf as a contiguous (consecutive) segment (i.e. the segment without gaps).

Note

In the first test case of the example, you can shift the segment [3;3][3; 3] to the right and the segment [4;5][4; 5] to the right. After all moves, the books form the contiguous segment [5;7][5; 7]. So the answer is 22.

In the second test case of the example, you have nothing to do, all the books on the bookshelf form the contiguous segment already.

In the third test case of the example, you can shift the segment [5;5][5; 5] to the left and then the segment [4;4][4; 4] to the left again. After all moves, the books form the contiguous segment [1;3][1; 3]. So the answer is 22.

In the fourth test case of the example, you can shift the segment [1;1][1; 1] to the right, the segment [2;2][2; 2] to the right, the segment [6;6][6; 6] to the left and then the segment [5;5][5; 5] to the left. After all moves, the books form the contiguous segment [3;4][3; 4]. So the answer is 44.

In the fifth test case of the example, you can shift the segment [1;2][1; 2] to the right. After all moves, the books form the contiguous segment [2;5][2; 5]. So the answer is 11.

Samples

5
7
0 0 1 0 1 0 1
3
1 0 0
5
1 1 0 0 1
6
1 0 0 0 0 1
5
1 1 0 1 1
2
0
2
4
1

在线编程 IDE

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