Problem E

Statement
Copy Copied
E. And Constrainttime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputwowaka & Hatsune Miku - Tosenbo 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.InputEach 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 < 2^{29}$$$).The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$0 \le b_i < 2^{29}$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.OutputFor 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$$$.ExampleInput741 4 41 2 3 444 0 41 1 1 1210 031 10 1 261 2 3 4 51 1 4 5 1 4200 040 1 0536870911 536870911 536870911 536870911Output4
-1
2
2
-1
0
536870916
NoteIn 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.

Submissions

IDLanguageExit CodeTimestampCodeStdoutStderrRetry
5004 cpp 0 2026-03-07T12:40:11.233991Z View View View Retry
5001 cpp 1 2026-03-07T12:28:25.13679Z View View View Retry

Evaluations

Eval IDRun IDProviderModelLangSuccessTimestampPromptResponseStdoutStderrRetry
3236 20260320-220006 openai gpt-5.4 go true 2026-03-21T00:01:03.186533Z View View View View Retry
1730 20260307-080843 openai gpt-5 go false 2026-03-07T12:21:55.62377Z View View View View Retry