D. Array GCD

You are given array ai 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.

Please note that each of operations may be applied at most once (and may be not applied at all) so you can remove only one segment and each number may be changed (increased or decreased) by at most 1. Also note, that you are not allowed to delete the whole array.

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 na and b (1 ≤ n ≤ 1 000 000, 0 ≤ a, b ≤ 109) — 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 ai (2 ≤ ai ≤ 109) — 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 test(s)
input
3 1 4
4 2 3
output
1
input
5 3 2
5 17 13 5 6
output
8
input
8 3 4
3 7 5 4 3 12 9 4
output
13
Note

In the first sample the optimal way is to remove number 3 and pay 1 coin for it.

In the second sample you need to remove a segment [17, 13] and then decrease number 6. The cost of these changes is equal to2·3 + 2 = 8 coins.

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=1e6+7;
 4 int a[maxn];
 5 long long A,B;
 6 int n;
 7 long long ans=1e16;
 8 vector<int>p;
 9 long long g1[maxn];
10 long long g2[maxn];
11 long long g3[maxn];
12 void f(int x)
13 {
14     for(int i=2;i*i<=x;i++)
15     {
16          if(x%i==0)
17          {
18              p.push_back(i);
19              while(x%i==0)
20                 x/=i;
21          }
22     }
23     if(x!=1) p.push_back(x);
24 }
25 void solve (int x)
26 {
27     memset(g1,0,sizeof(g1));
28     memset(g2,0,sizeof(g2));
29     memset(g3,0,sizeof(g3));
30     for(int i=1;i<=n;i++)
31     {
32         if(a[i]%x==0) g1[i]=g1[i-1];
33         else if((a[i]+1)%x==0||(a[i]-1)%x==0) g1[i]=g1[i-1]+B;
34         else g1[i]=1e16;
35 
36     }
37     for(int i=n;i>=1;i--)
38     {
39         if(a[i]%x==0)g2[i]=g2[i+1];
40         else if((a[i]-1)%x==0||(a[i]+1)%x==0) g2[i]=g2[i+1]+B;
41         else g2[i]=1e16;
42     }
43     g3[0]=1e16;
44     for(int i=1;i<=n;i++)
45     {
46         g3[i]=g1[i]-(i+1)*A;
47         g3[i]=min(g3[i],g3[i-1]);
48     }
49     for(int i=1;i<=n;i++)
50     {
51         ans=min(ans,g2[i]+(i-1)*A);
52         ans=min(ans,g1[i]+(n-i)*A);
53     }
54     for(int i=1;i<=n+1;i++)
55     {
56         ans=min(ans,g3[i-1]+g2[i]+A*i);
57     }
58 }
59 int main()
60     {
61         scanf("%d%ld%ld",&n,&A,&B);
62         for(int i=1;i<=n;i++)
63             scanf("%d",&a[i]);
64         for(int i=-1;i<=1;i++)
65             f(a[1]+i),f(a[n]+i);
66         sort(p.begin(),p.end());
67         p.erase(unique(p.begin(),p.end()),p.end());
68         for(int i=0;i<p.size();i++)
69             solve(p[i]);
70         printf("%I64d\n",ans);
71         return 0;
72     }

 

posted @ 2016-02-14 11:26  超级学渣渣  阅读(440)  评论(0编辑  收藏  举报