Capba dreams of one day being a virtuoso trombone soloist… but for now, he must practice his boring exercises, which involve playing notes, one after another. A trombone has seven slide positions, numbered closest to furthest from to . The -th note can be played in different slide positions.
Since he wants to save his energy for more interesting things, he wants to get through the exercise with as little effort as possible. Most importantly, this means minimizing the total distance that the slide moves. If two consecutive notes are played in positions and , then the slide moves a distance of .
However, if there are multiple ways to play the exercise which minimize the movement of the slide, Capba wants the one which will minimize the number of times the slide must change direction – another important aspect which can reduce the amount of effort. Between two consecutive notes, with the first played in slide position and the second in position , the slide is either moving in , stationary , or moving out .
Given the above criteria, your job is to find a way to play all of the notes in order that minimizes the effort required, and output the total distance travelled by the slide as well as the number of times it changes direction.
Input Specification
Line : .
Next lines: , followed by integers, denoting the
slide positions in which note can be played.
Output Specification
Output two space-separated integers: the total distance travelled by the slide, followed by the number of direction changes, for the best solution.
Sample Input
8
2 1 6
2 3 6
2 1 4
2 3 6
2 1 5
2 2 6
1 4
2 6 1
Sample Output
10 2
Explanation for Sample Output
Twinkle, twinkle, little star (with repeated notes omitted).
Note can be played in either position or , note in position or , etc.
Notice that the slide positions can be given in any order.
The best option is to play the notes in positions . The total distance travelled is , and there are changes of direction after notes and .
Note that other solutions exist which yield a distance of , such as always using the furthest slide position – however, they require more changes of direction.
Comments