Under construction

Still being built. Things may change

Bubble Sort

Time limit1sMemory limit128 MB

Summary
Swap exactly one pair of elements in the array, then find the minimum number of swaps the given bubble sort performs on the result.
Level

Hard8 of 10

Topics
Sorting, Prefix sum, Greedy
Solved
No attempts yet

Problem

Bubble sort is one of the simplest sorting algorithms, described by the pseudocode below. Whenever two adjacent elements are out of order and get swapped, the swap counter increases by one.

void bubble_sort(int *a, int n) {
    int i, j;
    for (i = 0; i < n - 1; ++i) {
        for (j = 0; j < n - 1; ++j) {
            if (a[j] > a[j + 1]) {
                /* The pair is out of order, so swap the two elements. */
                /* This increases the swap count by one. */
                int x = a[j];
                a[j] = a[j + 1];
                a[j + 1] = x;
            }
        }
    }
}

Given an array AA of length nn, define an array A∗A^* as follows. Choose two indices ii and jj with 1≤i<j≤n1 \le i < j \le n, and swap the ii-th and jj-th elements of AA exactly once; the resulting array is A∗A^*.

Among all possible arrays A∗A^*, output the smallest number of swaps that the bubble sort above would perform.

Input

The first line contains an integer NN. Each of the next NN lines contains one element of the array AA, given in the order A1,A2,…,ANA_1, A_2, \ldots, A_N (one per line).

Output

Print the minimum bubble-sort swap count over all arrays A∗A^*.

Constraints

  • 1≤N≤100,0001 \le N \le 100{,}000
  • 1≤Ai≤1,000,000,0001 \le A_i \le 1{,}000{,}000{,}000

Examples3

  1. Example 1

    Input
    5
    10
    3
    6
    8
    1
    
    Expected output
    0
    
  2. Example 2

    Input
    5
    3
    1
    7
    9
    5
    
    Expected output
    2
    
  3. Example 3

    Input
    3
    1
    2
    3
    
    Expected output
    1