#agc022c. [agc022_c]Remainder Game
[agc022_c]Remainder Game
Problem Statement
Aoki is playing with a sequence of numbers . Every second, he performs the following operation :
- Choose a positive integer . For each element of the sequence , Aoki may choose to replace with its remainder when divided by , or do nothing with . The cost of this operation is (regardless of how many elements he changes).
Aoki wants to turn the sequence into (the order of the elements is important). Determine if it is possible for Aoki to perform this task and if yes, find the minimum cost required.
Constraints
- All values in the input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the minimum cost required to turn the original sequence into . If the task is impossible, output instead.
Sample Input 1
3
19 10 14
0 3 4
Sample Output 1
160
Here's a possible sequence of operations :
-
Choose . Replace with , with and do nothing to . The sequence is now .
-
Choose . Replace with , do nothing to and replace with . The sequence is now .
The total cost is .
Sample Input 2
3
19 15 14
0 0 0
Sample Output 2
2
Aoki can just choose and turn everything into . The cost is .
Sample Input 3
2
8 13
5 13
Sample Output 3
-1
The task is impossible because we can never turn into using the given operation.
Sample Input 4
4
2 0 1 8
2 0 1 8
Sample Output 4
0
Aoki doesn't need to do anything here. The cost is .
Sample Input 5
1
50
13
Sample Output 5
137438953472
Beware of overflow issues.