atcoder#ARC156A. [ARC156A] Non-Adjacent Flip

[ARC156A] Non-Adjacent Flip

Score : 400400 points

Problem Statement

We have NN coins numbered 11 to NN with two distinguishable sides. A string SS represents the current state of the coins. If the ii-th character of SS is 1, coin ii is showing heads; if that character is 0, coin ii is showing tails.

You can repeat the following operation zero or more times.

  • Choose a pair of integers (i,j)(i,j) such that 1i<jN1\leq i < j\leq N and ji2j-i\geq \bm{2}. Flip coin ii and coin jj.

Determine whether it is possible to make all the NN coins show tails. If it is possible, find the minimum number of operations needed.

You are given TT test cases to solve.

Constraints

  • 1T2×1051 \leq T \leq 2\times 10^5
  • 3N2×1053 \leq N \leq 2\times 10^5
  • SS is a string of length NN consisting of 0 and 1.
  • All numbers in the input are integers.
  • For each input file, the sum of NN over the test cases is at most 2×1052\times 10^5.

Input

The input is given from Standard Input in the following format:

TT

case1\mathrm{case}_1

\vdots

caseT\mathrm{case}_T

​ Each case is in the following format:

NN

SS

Output

Print TT lines. The ii-th line (1iT)(1\leq i \leq T) should contain the minimum number of operations needed to make all the coins show tails if it is possible, and -1 otherwise.

5
3
101
6
101101
5
11111
6
000000
30
111011100110101100101000000111
1
2
-1
0
8

For the first test case, you can perform the operation with (i,j)=(1,3)(i,j)=(1,3) to make all the coins show tails in one operation.

For the second test case, you can perform the operation with (i,j)=(1,3)(i,j)=(1,3) and then with (i,j)=(4,6)(i,j)=(4,6) to make all the coins show tails in two operations.

For the third test case, you can prove that there is no way to make all the coins show tails, so you should print -1.

For the fourth test case, the coins already show tails, so no operation is needed.