atcoder#AGC032D. [AGC032D] Rotation Sort

[AGC032D] Rotation Sort

Score : 10001000 points

Problem Statement

You are given a permutation p=(p1,,pN)p = (p_1, \ldots, p_N) of {1,,N}\{ 1, \ldots, N \}. You can perform the following two kinds of operations repeatedly in any order:

  • Pay a cost AA. Choose integers ll and rr (1l<rN1 \leq l < r \leq N), and shift (pl,,pr)(p_l, \ldots, p_r) to the left by one. That is, replace pl,pl+1,,pr1,prp_l, p_{l + 1}, \ldots, p_{r - 1}, p_r with pl+1,pl+2,,pr,plp_{l + 1}, p_{l + 2}, \ldots, p_r, p_l, respectively.
  • Pay a cost BB. Choose integers ll and rr (1l<rN1 \leq l < r \leq N), and shift (pl,,pr)(p_l, \ldots, p_r) to the right by one. That is, replace pl,pl+1,,pr1,prp_l, p_{l + 1}, \ldots, p_{r - 1}, p_r with pr,pl,,pr2,pr1p_r, p_l, \ldots, p_{r - 2}, p_{r - 1}, respectively.

Find the minimum total cost required to sort pp in ascending order.

Constraints

  • All values in input are integers.
  • 1N50001 \leq N \leq 5000
  • 1A,B1091 \leq A, B \leq 10^9
  • (p1,pN)(p_1 \ldots, p_N) is a permutation of {1,,N}\{ 1, \ldots, N \}.

Input

Input is given from Standard Input in the following format:

NN AA BB

p1p_1 \cdots pNp_N

Output

Print the minimum total cost required to sort pp in ascending order.

3 20 30
3 1 2
20

Shifting (p1,p2,p3)(p_1, p_2, p_3) to the left by one results in p=(1,2,3)p = (1, 2, 3).

4 20 30
4 2 3 1
50

One possible sequence of operations is as follows:

  • Shift (p1,p2,p3,p4)(p_1, p_2, p_3, p_4) to the left by one. Now we have p=(2,3,1,4)p = (2, 3, 1, 4).
  • Shift (p1,p2,p3)(p_1, p_2, p_3) to the right by one. Now we have p=(1,2,3,4)p = (1, 2, 3, 4).

Here, the total cost is 20+30=5020 + 30 = 50.

1 10 10
1
0
4 1000000000 1000000000
4 3 2 1
3000000000
9 40 50
5 3 4 7 6 1 2 9 8
220