Codeforces Round 802 (Div. 2)
F
Puzzle
Codeforces
2600
原题
← E→
翻译中…

Pupils Alice and Ibragim are best friends. It's Ibragim's birthday soon, so Alice decided to gift him a new puzzle. The puzzle can be represented as a matrix with $2$ rows and $n$ columns, every element of which is either $0$ or $1$. In one move you can swap two values in neighboring cells.

翻译中…

More formally, let's number rows $1$ to $2$ from top to bottom, and columns $1$ to $n$ from left to right. Also, let's denote a cell in row $x$ and column $y$ as $(x, y)$. We consider cells $(x_1, y_1)$ and $(x_2, y_2)$ neighboring if $|x_1 - x_2| + |y_1 - y_2| = 1$.

翻译中…

Alice doesn't like the way in which the cells are currently arranged, so she came up with her own arrangement, with which she wants to gift the puzzle to Ibragim. Since you are her smartest friend, she asked you to help her find the minimal possible number of operations in which she can get the desired arrangement. Find this number, or determine that it's not possible to get the new arrangement.

翻译中…

Input

翻译中…

The first line contains an integer $n$ ($1 \leq n \leq 200\,000$) — the number of columns in the puzzle.

翻译中…

Following two lines describe the current arrangement on the puzzle. Each line contains $n$ integers, every one of which is either $0$ or $1$.

翻译中…

The last two lines describe Alice's desired arrangement in the same format.

翻译中…

Output

翻译中…

If it is possible to get the desired arrangement, print the minimal possible number of steps, otherwise print $-1$.

翻译中…

Note

翻译中…

In the first example the following sequence of swaps will suffice:

翻译中…
  • $(2, 1), (1, 1)$,
  • $(1, 2), (1, 3)$,
  • $(2, 2), (2, 3)$,
  • $(1, 4), (1, 5)$,
  • $(2, 5), (2, 4)$.
翻译中…

It can be shown that $5$ is the minimal possible answer in this case.

翻译中…

In the second example no matter what swaps you do, you won't get the desired arrangement, so the answer is $-1$.

翻译中…

样例

Input #1
5
0 1 0 1 0
1 1 0 0 1
1 0 1 0 1
0 0 1 1 0
Output #1
5
InputCopy OutputCopy
Input #2
3
1 0 0
0 0 0
0 0 0
0 0 0
Output #2
-1
InputCopy OutputCopy
题目信息
题号
1700F
时间限制
1 s
内存限制
256 MB
标签
构造
动态规划
贪心

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