atcoder#AGC022C. [AGC022C] Remainder Game
[AGC022C] Remainder Game
Score : points
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.
3
19 10 14
0 3 4
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 .
3
19 15 14
0 0 0
2
Aoki can just choose and turn everything into . The cost is .
2
8 13
5 13
-1
The task is impossible because we can never turn into using the given operation.
4
2 0 1 8
2 0 1 8
0
Aoki doesn't need to do anything here. The cost is .
1
50
13
137438953472
Beware of overflow issues.