#agc032d. [agc032_d]Rotation Sort
[agc032_d]Rotation Sort
Problem Statement
You are given a permutation of . You can perform the following two kinds of operations repeatedly in any order:
- Pay a cost . Choose integers and (), and shift to the left by one. That is, replace with , respectively.
- Pay a cost . Choose integers and (), and shift to the right by one. That is, replace with , respectively.
Find the minimum total cost required to sort in ascending order.
Constraints
- All values in input are integers.
- is a permutation of .
Input
Input is given from Standard Input in the following format:
Output
Print the minimum total cost required to sort in ascending order.
Sample Input 1
3 20 30
3 1 2
Sample Output 1
20
Shifting to the left by one results in .
Sample Input 2
4 20 30
4 2 3 1
Sample Output 2
50
One possible sequence of operations is as follows:
- Shift to the left by one. Now we have .
- Shift to the right by one. Now we have .
Here, the total cost is .
Sample Input 3
1 10 10
1
Sample Output 3
0
Sample Input 4
4 1000000000 1000000000
4 3 2 1
Sample Output 4
3000000000
Sample Input 5
9 40 50
5 3 4 7 6 1 2 9 8
Sample Output 5
220