Feng Shui

시간 제한1초메모리 제한2048 MB

요약
순열이 주어질 때, 한 지점을 기준으로 앞은 감소하고 뒤는 증가하도록 만드는 최소 인접 교환 횟수를 구한다.
난이도

보통10점 중 7점

유형
정렬, 그리디, 배열, 누적 합
정답자
아직 제출이 없습니다

문제

You own NN flower pots (numbered from 11 to NN) displayed in order from west to east. Each flower pot has a different height; flower pot ii is the A_iA\_ith shortest flower pot by height. In other words, the array \[A_1,A_2,…,A_N]\[A\_1, A\_2, \dots , A\_N ] is a permutation of \[1,2,…,N]\[1, 2, \dots , N].

After learning about Feng Shui (a practice of arranging pieces in living spaces to create balance), your house is healthier if you arrange the flower pots as follows. There should exist an integer kk such that 1≤k≤N1 ≤ k ≤ N, A_i>A_i+1A\_i > A\_{i+1} for all 1≤i<k1 ≤ i < k, and A_i<A_i+1A\_i < A\_{i+1} for all k≤i<Nk ≤ i < N. You are allowed to swap adjacent flower pots zero or more times.

As the flower pots are fragile, you want to minimize the number of swaps. Determine the minimum number of swaps such that the flower pots follow the Feng Shui rule.

입력

The first line consists of an integer NN (1≤N≤300,0001 ≤ N ≤ 300\\, 000).

The second line consists of NN integers A_iA\_i (1≤A_i≤N1 ≤ A\_i ≤ N). The array AA is a permutation of \[1,2,…,N]\[1, 2, \dots , N].

출력

Output a single integer representing the minimum number of swaps such that the flower pots follow the Feng Shui rule.

예제3

  1. 예제 1

    입력
    6
    2 6 5 3 1 4
    
    예상 출력
    3
    
  2. 예제 2

    입력
    5
    1 2 3 4 5
    
    예상 출력
    0
    
  3. 예제 3

    입력
    5
    5 4 3 2 1
    
    예상 출력
    0