AtCoder Regular Contest 120
C
Swaps 2
AtCoder
⌀ 1478
原题
← BD →
翻译中…

Problem Statement

翻译中…

Given are two sequences of length $N$ each: $A = (A_1, A_2, A_3, \dots, A_N)$ and $B = (B_1, B_2, B_3, \dots, B_N)$. Determine whether it is possible to make $A$ equal $B$ by repeatedly doing the operation below (possibly zero times). If it is possible, find the minimum number of operations required to do so.

翻译中…
  • Choose an integer $i$ such that $1 \le i \lt N$, and do the following in order:
  • swap $A_i$ and $A_{i + 1}$;
    • add $1$ to $A_i$;
    • subtract $1$ from $A_{i + 1}$.
翻译中…

Constraints

翻译中…
  • $2 \le N \le 2 \times 10^5$
  • $0 \le A_i \le 10^9$
  • $0 \le B_i \le 10^9$
  • All values in input are integers.
翻译中…

Input

翻译中…

Input is given from Standard Input in the following format:

翻译中…

N
A_1 A_2 A_3 \dots A_N
B_1 B_2 B_3 \dots B_N

翻译中…

Output

翻译中…

If it is impossible to make $A$ equal $B$, print -1. Otherwise, print the minimum number of operations required to do so.

翻译中…

样例

Input #1
3
3 1 4
6 2 0
Output #1
2
We can match $A$ with $B$ in two operations, as follows: - First, do the operation with $i = 2$, making $A = (3, 5, 0)$. - Next, do the operation with $i = 1$, making $A = (6, 2, 0)$. We cannot meet our objective in one or fewer operations.
Input #2
3
1 1 1
1 1 2
Output #2
-1
In this case, it is impossible to match $A$ with $B$.
Input #3
5
5 4 1 3 2
5 4 1 3 2
Output #3
0
$A$ may equal $B$ before doing any operation.
Input #4
6
8 5 4 7 4 5
10 5 6 7 4 1
Output #4
7
题目信息
题号
arc120_c
时间限制
2 s
内存限制
1024 MB

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