AtCoder Regular Contest 182
D
Increment Decrement Again
AtCoder
⌀ 2992
原题
← CE →
翻译中…

Problem Statement

翻译中…

An integer sequence where no two adjacent elements are the same is called a good sequence.

翻译中…

You are given two good sequences of length $N$: $A=(A_1,A_2,\dots,A_N)$ and $B=(B_1,B_2,\dots,B_N)$. Each element of $A$ and $B$ is between $0$ and $M-1$, inclusive.

翻译中…

You can perform the following operations on $A$ any number of times, possibly zero:

翻译中…
  • Choose an integer $i$ between $1$ and $N$, inclusive, and perform one of the following:
  • Set $A_i \leftarrow (A_i + 1) \bmod M$.
    • Set $A_i \leftarrow (A_i - 1) \bmod M$. Here, $(-1) \bmod M = M - 1$.
翻译中…

However, you cannot perform an operation that makes $A$ no longer a good sequence.

翻译中…

Determine if it is possible to make $A$ equal to $B$, and if it is possible, find the minimum number of operations required to do so.

翻译中…

Constraints

翻译中…
  • $2 \leq N \leq 2 \times 10^5$
  • $2 \leq M \leq 10^6$
  • $0\leq A_i,B_i< M(1\leq i\leq N)$
  • $A_i\ne A_{i+1}(1\leq i\leq N-1)$
  • $B_i\ne B_{i+1}(1\leq i\leq N-1)$
  • All input values are integers.
翻译中…

Input

翻译中…

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

翻译中…

N M
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N

翻译中…

Output

翻译中…

If the goal is unachievable, print -1.

翻译中…

Otherwise, print the minimum number of operations required as an integer.

翻译中…

样例

Input #1
3 9
2 0 1
4 8 1
Output #1
3
You can achieve the goal in three operations as follows: - Set $A_1 \leftarrow (A_1 + 1) \bmod M$. Now $A = (3, 0, 1)$. - Set $A_2 \leftarrow (A_2 - 1) \bmod M$. Now $A = (3, 8, 1)$. - Set $A_1 \leftarrow (A_1 + 1) \bmod M$. Now $A = (4, 8, 1)$. It is impossible to achieve the goal in two or fewer operations, so the answer is 3. For example, you cannot set $A_2 \leftarrow (A_2 + 1) \bmod M$ in the first operation, because it would make $A = (2, 1, 1)$, which is not a good sequence.
Input #2
3 9
1 8 2
1 8 2
Output #2
0
$A$ and $B$ might be equal from the beginning.
Input #3
24 182
128 115 133 52 166 92 164 119 143 99 54 162 86 2 59 166 24 78 81 5 109 67 172 99
136 103 136 28 16 52 2 85 134 64 123 74 64 28 85 161 19 74 14 110 125 104 180 75
Output #3
811
题目信息
题号
arc182_d
时间限制
2 s
内存限制
1024 MB

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