atcoder#AGC032D. [AGC032D] Rotation Sort
[AGC032D] Rotation Sort
Score : points
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.
3 20 30
3 1 2
20
Shifting to the left by one results in .
4 20 30
4 2 3 1
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 .
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