cswork / OA打开练习工作台

AmazonOA #221

Equalize The Arrays

估计中等可在线判题

考点(AI 标注)前缀和区间贪心调度

Each step adds 1 to every element of any non-empty prefix, or to every element of any non-empty suffix. Turn source into target and output the minimum number of steps, or −1 if it is impossible.

输入格式

The first line contains n, the second line source, and the third line target. The original text had no numeric bounds; this site uses 1≤n≤200000, with elements of both arrays in −10^9..10^9.

输出格式

Output a single integer, the answer.

样例 1

输入:

5
1 2 3 -1 0
3 4 3 0 4

输出:

6

样例 2

输入:

3
0 0 0
0 1 0

输出:

-1

样例 3

输入:

3
1 2 2
2 2 3

输出:

2

样例说明

The answers are 6, −1 and 2. Raising only the middle cannot be done with the allowed operations.