Problem
A gym teacher asks his students to line up in descending order of height. However, they line up randomly, and the teacher must arrange them in order by himself. Each time, the teacher chooses a student and moves them from position to position . If , then the positions of the students between and increase by . Conversely, if , then the positions of the students between and decrease by . The teacher continues to make these moves until each student is in the correct position. Each time the teacher makes a move of a student from position to position , the teacher is forced to take steps.
Write a program to find the minimum total number of steps the teacher must take to place all the students in the correct order.
Input
The input will consist of two lines. The first line will contain a natural number , the number of students. The second line will contain integers , , , separated in pairs by a single space: the heights of the students initially at positions , , , respectively. Assume that there are no two students with the same height.
Output
The output must consist of a single line containing only one natural number: the minimum total number of steps the teacher must take.
Constraints
Execution time limit: 2 sec.
Memory limit: 64 MB.
Examples of Input - Output
1st
STDIN (gymnast.in)
5
20 30 5 15 10
STDOUT (gymnast.out)
11
Explanation of the first example: The teacher can move student from position to position (cost ) and then move student from position to position (cost ). Total cost is . This cost is also the minimum possible.
2nd
STDIN (gymnast.in)
10
9 6 2 10 8 7 4 5 1 3
STDOUT (gymnast.out)
52
Comments