시간 제한 메모리 제한 제출 정답 맞은 사람 정답 비율
1 초 512 MB 8 5 5 83.333%

문제

At the end of a training camp, a group photo is taken with the N participants.The participants are numbered from 1 to N, in order of height. The height of the participant h is h (1 ≤ h ≤ N).

The participants stand on the stairs for the photo. There are N steps in the stairs. The steps are numbered from 1 to N, from a lower place to a higher place.

The step i + 1 is higher than the step i by 2 (1 ≤ i ≤ N − 1). Since the steps of the stairs are narrow, only one participant will stand on each step. A group photo will be taken when the participants are lined up behind each other.

A group photo will be taken soon. One participant stands on each step. Now, the participant Hi stands on the step i (1 ≤ i ≤ N). However, since the difference of the heights of the participants are too large, if a photo is taken with the current order of the participants, some participants might be hidden behind other participants. Hence, you want to change the order of the participants so that at least the head of every participant shows on the photo. In other words, the following condition should be satisfied.

Let ai be the height of the participant on the step i (1 ≤ i ≤ N). Then, the inequality ai < ai+1 + 2 is satisfied for every i (1 ≤ i ≤ N − 1).

You can only swap two consecutive participants. In other words, by an operation, you choose a step i (1 ≤ i ≤ N − 1) arbitrarily, and you swap the participant on the step i and the participant on the step i + 1.

You want to minimize the number of operations so that the above condition is satisfied.

Write a program which, given the order of the participants, calculates the minimum number of operations.

입력

Read the following data from the standard input. Given values are all integers.

N
H1 · · · HN

출력

Write one line to the standard output. The output should contain the minimum number of operations.

제한

  • 3 ≤ N ≤ 5 000.
  • 1 ≤ Hi ≤ N (1 ≤ i ≤ N).
  • Hi ≠ Hj (1 ≤ i < j ≤ N).

예제 입력 1

5
3 5 2 4 1

예제 출력 1

3

The condition is satisfied if you perform three operations as follows.

  • First, you swap the participants on the steps 2 and 3. The heights of the participants become 3, 2, 5, 4, 1 from a lower place to a higher place.
  • Second, you swap the participants on the steps 4 and 5. The heights of the participants become 3, 2, 5, 1, 4 from a lower place to a higher place.
  • Finally, you swap the participants on the steps 3 and 4. The heights of the participants become 3, 2, 1, 5, 4 from a lower place to a higher place. Then the condition is satisfied.

Since the condition is not satisfied if you perform operations less than three times, output 3.

예제 입력 2

5
3 2 1 5 4

예제 출력 2

0

The condition is already satisfied. You do not need to do any operation.

예제 입력 3

9
6 1 3 4 9 5 7 8 2

예제 출력 3

9