AmazonOA #221
Equalize The Arrays
Estimated MediumJudge-ready
Topics (AI-labelled)Prefix & RangesGreedy
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.
Input
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
Output a single integer, the answer.
Example 1
Input:
5
1 2 3 -1 0
3 4 3 0 4
Output:
6
Example 2
Input:
3
0 0 0
0 1 0
Output:
-1
Example 3
Input:
3
1 2 2
2 2 3
Output:
2
Example notes
The answers are 6, −1 and 2. Raising only the middle cannot be done with the allowed operations.
Editorial
Loading solution…