/* -*- coding: utf-8 -*- * * 2039.cc: No.2039 Copy and Avoid - yukicoder */ #include #include using namespace std; /* constant */ const int MAX_M = 5000; const int MAX_K = 1344; const long long LINF = 1LL << 62; /* typedef */ typedef long long ll; /* global variables */ int cs[MAX_M], ds[MAX_K]; ll dp[MAX_K]; /* subroutines */ inline void setmin(ll &a, ll b) { if (a > b) a = b; } /* main */ int main() { int n, m, a, b; scanf("%d%d%d%d", &n, &m, &a, &b); for (int i = 0; i < m; i++) scanf("%d", cs + i); sort(cs, cs + m); int k = 0; for (int p = 1; p * p <= n; p++) if (n % p == 0) { ds[k++] = p; int q = n / p; if (q != p) ds[k++] = q; } sort(ds, ds + k); //for (int i = 0; i < k; i++) printf("%d ", ds[i]); putchar('\n'); fill(dp, dp + k, LINF); dp[0] = 0; for (int i = 0; i < k; i++) { if (dp[i] >= LINF) continue; int di = ds[i]; int x = lower_bound(cs, cs + m, di) - cs; for (; x < m && cs[x] % di != 0; x++); int maxj = (x >= m) ? k : lower_bound(ds, ds + k, cs[x]) - ds; //printf("ds[%d]=%d, maxj=%d\n", i, di, maxj); for (int j = i + 1; j < maxj; j++) setmin(dp[j], dp[i] + ((ll)a * (ds[j] / di - 1) + b)); } printf("%lld\n", (dp[k - 1] < LINF) ? dp[k - 1] - b : -1LL); return 0; }