#D8691. Librarian's Work
Librarian's Work
Librarian's Work
Japanese Animal Girl Library (JAG Library) is famous for a long bookshelf. It contains books numbered from to from left to right. The weight of the -th book is .
One day, naughty Fox Jiro shuffled the order of the books on the shelf! The order has become a permutation from left to right. Fox Hanako, a librarian of JAG Library, must restore the original order. She can rearrange a permutation of books by performing either operation A or operation B described below, with arbitrary two integers and such that holds.
Operation A:
- A-1. Remove from the shelf.
- A-2. Shift books between and to .
- A-3. Insert into the of .
Operation B:
- B-1. Remove from the shelf.
- B-2. Shift books between and to .
- B-3. Insert into the of .
This picture illustrates the orders of the books before and after applying operation A and B for .
Since the books are heavy, operation A needs $\sum_{i=l+1}^r w_{p_i} + C \times (r-l) \times w_{p_l}$ units of labor and operation B needs $\sum_{i=l}^{r-1} w_{p_i} + C \times (r-l) \times w_{p_r}$ units of labor, where is a given constant positive integer.
Hanako must restore the initial order from by performing these operations repeatedly. Find the minimum sum of labor to achieve it.
Input
The input consists of a single test case formatted as follows.
:
The first line conists of two integers and (). The ()-th line consists of two integers and (). The sequence () is a permutation of ().
Output
Print the minimum sum of labor in one line.
Examples
Input
3 2 2 3 3 4 1 2
Output
15
Input
3 2 1 2 2 3 3 3
Output
0
Input
10 5 8 3 10 6 5 8 2 7 7 6 1 9 9 3 6 2 4 5 3 5
Output
824
inputFormat
Input
The input consists of a single test case formatted as follows.
:
The first line conists of two integers and (). The ()-th line consists of two integers and (). The sequence () is a permutation of ().
outputFormat
Output
Print the minimum sum of labor in one line.
Examples
Input
3 2 2 3 3 4 1 2
Output
15
Input
3 2 1 2 2 3 3 3
Output
0
Input
10 5 8 3 10 6 5 8 2 7 7 6 1 9 9 3 6 2 4 5 3 5
Output
824
样例
3 2
2 3
3 4
1 2
15