cswork / OAOpen practice workspace

The D. E. Shaw GroupOA #10

Minimum Operations to Make Array Equal

Estimated HardJudge-ready

Topics (AI-labelled)Prefix & RangesBits & MathGreedy

In each operation, choose any non-empty contiguous subarray of even length and apply +1, −1, +1, −1 to its elements in turn from left to right. The same range may be operated on repeatedly, and different ranges may also be chosen. Find the minimum number of operations to turn source into target; output −1 if it is impossible.

The standard input/output, the ranges added by this site, and the examples follow this problem's description.

Input

The first line contains n (original problem: 1≤n≤10^5); the next two lines contain the n source values and the n target values, respectively (original problem: −10^9≤element≤10^9).

Output

Output the minimum number of operations; output −1 if target is unreachable.

Example 1

Input:

2
0 0
1 -1

Output:

1

Example 2

Input:

2
0 0
-1 1

Output:

-1

Example 3

Input:

4
0 0 0 0
1 -1 1 -1

Output:

1

Editorial

Loading solution…