MathWorksOA #15
Minimum Operations to Make Arrays Equal
估计中等可在线判题
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.
输入格式
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 the minimum number of operations, or −1.
样例 1
输入:
3
1 2 2
2 2 3
输出:
2
样例 2
输入:
2
0 0
2 3
输出:
3
样例 3
输入:
3
0 0 0
1 2 1
输出:
2
样例 4
输入:
1
5
8
输出:
3