2337: CF632 - B. Array GCD
[Creator : ]
Description
You are given array $a_i$ of length $n$. You may consecutively apply two operations to this array:
- remove some subsegment (continuous subsequence) of length $m<n$ and pay for it $m·a$ coins;
- change some elements of the array by at most $1$, and pay $b$ coins for each change.
Your goal is to calculate the minimum number of coins that you need to spend in order to make the greatest common divisor of the elements of the resulting array be greater than $1$.
Input
The first line of the input contains integers $n, a, b\ (1≤n≤1000000,0≤a,b≤10^9)$− the length of the array, the cost of removing a single element in the first operation and the cost of changing an element, respectively.
The second line contains $n$ integers $a_i\ (2≤a_i≤10^9)$− elements of the array.
The second line contains $n$ integers $a_i\ (2≤a_i≤10^9)$− elements of the array.
Output
Print a single number− the minimum cost of changes needed to obtain an array, such that the greatest common divisor of all its elements is greater than 1.
Sample 1 Input
3 1 4
4 2 3
Sample 1 Output
1
The optimal way is to remove number 3 and pay 1 coin for it.
Sample 2 Input
5 3 2
5 17 13 5 6
Sample 2 Output
8
You need to remove a segment [17,13] and then decrease number 6. The cost of these changes is equal to 2·3+2=8 coins.
Sample 3 Input
8 3 4
3 7 5 4 3 12 9 4
Sample 3 Output
13