Codeforces Round 1035 (Div. 2)
E
And Constraint
Codeforces
2600
原题
← DF →
翻译中…

You are given a sequence $a$ of length $n-1$ and a sequence $b$ of length $n$.

翻译中…

You can perform the following operation any number of times (possibly zero):

翻译中…
  • choose an index $1 \le i \le n$ and increment $b_i$ by $1$ (i.e., set $b_i \leftarrow b_i + 1$).
翻译中…

Your goal is to perform the minimum number of operations such that for every $1 \le i \le n-1$, the condition $b_i \, \& \, b_{i+1} = a_i$ holds, where $\&$ denotes the bitwise AND operation. If it is impossible to satisfy the condition, report it as well.

翻译中…

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 contains a single integer $n$ ($2 \le n \le 10^5$).

翻译中…

The second line contains $n-1$ integers $a_1, a_2, \ldots, a_{n-1}$ ($0 \le a_i \lt 2^{29}$).

翻译中…

The third line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($0 \le b_i \lt 2^{29}$).

翻译中…

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

翻译中…

Output

翻译中…

For each test case, you need to output only one integer.

翻译中…

If the goal can be achieved, output one integer — the minimum number of operations required. Otherwise, output $-1$.

翻译中…

Note

翻译中…

In the first test case, one of the optimal strategies is to use $4$ operations to make $b = [1,5,4,4]$, which satisfies all the conditions. We can prove that it is impossible to use fewer than $4$ operations.

翻译中…

In the second test case, since $b_1 \, \& \, b_2 = 4$ and $b_2 \, \& \, b_3 =0$, then $b_3 \, \& \, 4 =0$. However, we require that $b_3 \, \& \, b_4 =4$, which means it is impossible to satisfy all the conditions.

翻译中…

样例

Input #1
7
4
1 4 4
1 2 3 4
4
4 0 4
1 1 1 1
2
1
0 0
3
1 1
0 1 2
6
1 2 3 4 5
1 1 4 5 1 4
2
0
0 0
4
0 1 0
536870911 536870911 536870911 536870911
Output #1
4
-1
2
2
-1
0
536870916
InputCopy OutputCopy
题目信息
题号
2119E
时间限制
2 s
内存限制
256 MB
标签
位运算
动态规划
贪心

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