cswork / OAOpen practice workspace

MathWorksOA #15

Minimum Operations to Make Arrays Equal

Estimated MediumJudge-ready

Topics (AI-labelled)Prefix & RangesGreedy

Given integer arrays source and target of equal length. One operation chooses a prefix or a suffix and adds 1 to every element in it. Find the minimum number of operations; output −1 if source cannot be turned into target.

This site uses a standard input/output protocol.

Input

The first line contains n; the second line contains source; the third line contains target. 1≤n≤100000, and every element's absolute value is ≤10^13.

Output

Output the minimum number of operations, or −1.

Example 1

Input:

3
1 2 2
2 2 3

Output:

2

Example 2

Input:

2
0 0
2 3

Output:

3

Example 3

Input:

3
0 0 0
1 2 1

Output:

2

Example 4

Input:

1
5
8

Output:

3

Editorial

Loading solution…