Codeforces Round 1075 (Div. 2)
E
Majority Wins?
Codeforces
2600
原题
← D2F →
翻译中…

You are given a binary string$^{\text{∗}}$ $s$ of length $n$.

翻译中…

In one operation with a binary string $g$ of length $k$, you can do the following:

翻译中…
  • choose some $l, r$ such that $1 \leq l \leq r \leq k$;
  • replace the substring$^{\text{†}}$ $g_l, \ldots, g_r$ of the string $g$ with one character that appears in this substring at least as many times as the other character.
翻译中…

The cost of such an operation will be equal to $r-l+1$.

翻译中…

For example, the string 010010 can be transformed into 010 in one operation with a cost of $4$ by replacing the substring 1001 with 1; the string 1111 can be transformed into 1 by performing an operation with a cost of $4$ on the entire string, and the string 0100 can be transformed, for example, into 00 by taking the substring 100.

翻译中…

You need to find the minimum cost to transform the entire string $s$ into the string 1, using several (possibly none) operations, or determine that it is impossible.

翻译中…

$^{\text{∗}}$A binary string is a string that only consists of characters $0$ and $1$.$^{\text{†}}$A string $t$ is a substring of a string $g$ if $t$ can be obtained from $g$ by the deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

翻译中…

Input

翻译中…

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

翻译中…

The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5 \cdot 10^5$) — the length of the binary string.

翻译中…

The second line of each test case contains a string of length $n$, consisting of the characters 0 and 1 — the string $s$.

翻译中…

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

翻译中…

Output

翻译中…

For each test case, if it is impossible to obtain the string 1, output $-1$. Otherwise, output the minimum cost.

翻译中…

Note

翻译中…

In the first test case, it is impossible to obtain 1, since the only possible operation ($l = r = 1$) does not change the string.

翻译中…

In the second test case, the string is already 1 initially, so the answer is 0.

翻译中…

In the fourth test case, it is possible to perform an operation on the entire string, replacing it with 1. The cost of this operation is $2$. It can be shown that it is impossible to obtain the string 1 for a lower cost.

翻译中…

样例

Input #1
7
1
0
1
1
2
00
2
10
3
010
8
00111000
6
100100
Output #1
-1
0
-1
2
4
9
7
InputCopy OutputCopy
题目信息
题号
2189E
时间限制
2 s
内存限制
256 MB
标签
构造
贪心
数学
字符串

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。