PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GL0000374. Ước chung lớn nhất sau khi chỉnh sửa mảng (Array GCD)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Ước chung lớn nhất sau khi chỉnh sửa mảng (Array GCD)

Array GCD

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an array of nn integers a1,…,ana_1,\ldots,a_n. You may use each of the following operation types at most once:

  1. Remove one contiguous segment of length kk, where 0≤k<n0\le k<n, at a cost of k⋅Ak\cdot A. Removing nothing (k=0k=0) is allowed. You must not remove the whole array.
  2. Modify any subset of the remaining elements: each selected element may be increased by exactly 11 or decreased by exactly 11, at a cost of BB for that element. Each element may be modified at most once. Modifying nothing is allowed.

Find the minimum total cost such that the GCD of the remaining array is greater than 11.

Removed indices must form a single contiguous segment, not several disjoint segments. The segment may be a prefix, an internal segment, or a suffix.

Input

The first line contains n,A,Bn,A,B. The next line contains the nn integers a1,…,ana_1,\ldots,a_n.

Output

Print one 64-bit integer: the minimum total cost to make the remaining array GCD greater than 11.

Subtasks

  • Subtask 1 (20 points): 1≤n≤301\le n\le30; 0≤A,B≤1000\le A,B\le100; 2≤ai≤1 0002\le a_i\le1\,000.
  • Subtask 2 (30 points): 1≤n≤100 0001\le n\le100\,000; 0≤A,B≤1060\le A,B\le10^6; 2≤ai≤1062\le a_i\le10^6.
  • Subtask 3 (50 points): 1≤n≤1061\le n\le10^6; 0≤A,B≤1090\le A,B\le10^9; 2≤ai≤1092\le a_i\le10^9.

Examples

Example 1

Input

3 1 4
4 2 3

Output

1

Explanation

Remove the last element 33 for A=1A=1. The remaining array is [4,2][4,2] with GCD 2>12>1, so the cost is 11. No element needs modification.

Example 2

Input

5 3 2
5 17 13 5 6

Output

8

Explanation

Remove the segment [17,13][17,13] of length 22 for 66 coins; decrease 66 to 55 for 22 more coins. The remaining array becomes [5,5,5][5,5,5], whose GCD is 55. Total cost is 88.

Example 3

Input

8 3 4
3 7 5 4 3 12 9 4

Output

13

Explanation

Delete positions 22 through 44, the segment [7,5,4][7,5,4], at cost 3⋅3=93\cdot3=9. The remaining array is [3,3,12,9,4][3,3,12,9,4]. Decrease its last value from 44 to 33, paying another 44. The final array [3,3,12,9,3][3,3,12,9,3] has GCD 33, and the total cost is 9+4=139+4=13.