William has recently been working on select algorithms! In case you’re not already familiar with them, these algorithms receive an array of length and an index in input. Then, through steps they rearrange the array contents, so that the -th element is in position of the array, with all smaller elements sitting on its left side, and all larger elements on its right side. No relative order, however, can be assumed between elements within a same side.
The select algorithm is ready, however, the problem William has to tackle requires a bit more work. He needs to process a very large array of length , so that at the end given different indexes , , are all partitioning the array.
Since William is lazy, he doesn’t want to write a new algorithm from scratch! Instead, he plans to re-use the select algorithm multiple times to achieve the desired effect. For example, assume that , and the indexes are , , , as in Figure . One possible way to achieve the effect could be:
- first, run select on the whole vector with (as in Figure ), spending algorithm steps;
- then, run select on the sub-vector [] with , which requires algorithm steps;
- finally, run select on the sub-vector [] with , which requires algorithm steps.
Overall, the procedure will require + + = algorithm steps. Notice that this is not the only possible way to obtain the result! Another could be:
- run select on the whole vector with , spending steps;
- run select on the sub-vector [] with , spending steps;
- run select on the sub-vector [] with , spending steps.
With this other strategy, the procedure will require + + = steps not so good! Help William perform his task, minimising the number of algorithm steps required.
Input data
The first line contains the two integers and . The second line contains integers .
Output data
You need to write a single line with an integer: the minimum total number of algorithm steps required to multi-select the given indexes from an array of length .
Constraints and clarifications
- ;
- ;
- ;
# | Score | Constraints |
---|---|---|
1 | 0 | Examples |
2 | 19 | |
3 | 25 | |
4 | 40 | |
5 | 16 | No additional limitations. |
Example 1
stdin
3 11
2 4 10
stdout
21
Explanation
The first sample case is the one described in the problem statement.
Example 2
stdin
3 101
49 50 51
stdout
152
Explanation
In the second sample case, one way of achieving the lowest amount of steps is to first select = in steps, then = in further steps, getting = in place for free.