#P51433. [JOI 2021 Final] Group Photo

[JOI 2021 Final] Group Photo

Description

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

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

The step i+1i + 1 is higher than the step ii by 22 (1iN11 \leq i \leq 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 HiH_i stands on the step ii (1iN1 \leq i \leq 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 aia_i be the height of the participant on the step ii (1iN1 \leq i \leq N). Then, the inequality ai<ai+1+2a_i < a_{i+1} + 2 is satisfied for every ii (1iN11 \leq i \leq N − 1).

You can only swap two consecutive participants. In other words, by an operation, you choose a step ii (1iN11 \leq i \leq N − 1) arbitrarily, and you swap the participant on the step i and the participant on the step i+1i + 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.

Input

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

NN
H1HNH_1 \ldots H_N

Output

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

Sample 1

5
3 5 2 4 1
3

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

  • First, you swap the participants on the steps 22 and 33. The heights of the participants become 3,2,5,4,13, 2, 5, 4, 1 from a lower place to a higher place.
  • Second, you swap the participants on the steps 44 and 55. The heights of the participants become 3,2,5,1,43, 2, 5, 1, 4 from a lower place to a higher place.
  • Finally, you swap the participants on the steps 33 and 44. The heights of the participants become 3,2,1,5,43, 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 33.

5
3 2 1 5 4
0

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

9
6 1 3 4 9 5 7 8 2
9

Constraints

  • 3N50003 \leq N \leq 5000.
  • 1HiN1 \leq H_i \leq N (1iN1 \leq i \leq N).
  • HiHjH_i \neq H_j (1i<jN1 \leq i < j \leq N).

Subtasks

  1. (55 points) N9N \leq 9.
  2. (77 points) N20N \leq 20.
  3. (3232 points) N200N \leq 200.
  4. (2020 points) N800N \leq 800.
  5. (3636 points) No additional constraints.