#d11406. 双端队列

双端队列

Deque

Problem Description

You are given a sequence a1,a2,…,ana_1, a_2, \dots, a_n of length nn and an empty deque.

You must insert the numbers of the sequence into the deque one by one, in order. For each number you may choose to push it to the front or to the back of the deque.

Minimize the number of inversions in the final sequence obtained from the deque.

Input Format

The first line contains an integer nn, the length of the sequence (1≤n≤2×1051 \le n \le 2 \times 10^5).

The second line contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n, the given sequence (−109≤ai≤109-10^9 \le a_i \le 10^9).

Output Format

Output one integer on a single line: the minimum possible number of inversions.

Sample Input

4
3 7 5 5

Sample Output

2