Strangeoperation (It)
Strangeoperation (It)
• Choose an index 1 ≤ i ≤ N − 2 and let Ai := −(Ai−1 + Ai + Ai+1 ). That is, assign the value
−(Ai−1 + Ai + Ai+1 ) to Ai .
Help the Doctor determine whether it is possible to make the array A equal to B and if so, then find the
minimum number of operations required to achieve this.
+ Among the attachments of this task you may find a template file strangeoperation.* with
a sample incomplete implementation.
Input
The first line contains the only integer N . The second line contains N integers, the elements of array A.
The third line contains N integers, the elements of array B.
Output
You need to write a single line with an integer: the minimum number of operations required to make A
equal to B, or −1 if it is not possible to do so.
Constraints
• 2 ≤ N ≤ 200 000.
• −109 ≤ Ai , Bi ≤ 109 for each i = 0 . . . N − 1.
strangeoperation Page 1 of 2
Scoring
Your program will be tested against several test cases grouped in subtasks. In order to obtain the score
of a subtask, your program needs to correctly solve all of its test cases.
In this task, you can get partial scores: you will get 50% of the points for a subtask if you successfully
determine whether it is possible to make A equal to B (but do not correctly solve all of its test cases).
For this, the following condition must be satisfied for all test cases in a subtask: you should output −1
whenever it is impossible to make the two arrays equal, and otherwise, you should output a non-negative
integer between 0 and 263 − 1.
– Subtask 1 (0 points) Examples.
– Subtask 2 (15 points) There is at most one non-zero number in A.
– Subtask 3 (18 points) N ≤ 7.
– Subtask 4 (50 points) N ≤ 1000.
– Subtask 5 (17 points) No additional limitations.
Examples
input output
6 3
2 7 1 8 2 8
2 -10 1 -11 1 8
4 -1
3 1 4 1
-4 1 -6 1
Explanation
In the first sample case, consider the following steps for the array A = [2, 7, 1, 8, 2, 8]:
• Perform the operation on index 3. The array becomes: [2, 7, 1, −11, 2, 8].
• Perform the operation on index 1. The array becomes: [2, −10, 1, −11, 2, 8].
• Perform the operation on index 4. The array becomes: [2, −10, 1, −11, 1, 8].
It is not possible to make the two arrays equal in less than 3 moves.
In the second sample case, it can be proven that there is no way to make A equal to B using the
described operation.
strangeoperation Page 2 of 2