cswork / OAOpen practice workspace

AmazonOA #219

Get Minimum Number of Moves (A.K.A. Weight Lifting Equipments:)

Estimated EasyJudge-ready

Topics (AI-labelled)In-place Arrays

Blocks of distinct weights are arranged in an array, and each operation swaps two adjacent blocks. The lightest block must end up first and the heaviest last; the middle does not need to be sorted. Find the minimum number of swaps.

Input

The first line contains n; the second line contains the distinct weights. 2≤n≤100000, 1≤weight≤10^9.

Output

Output a single integer, the answer.

Example 1

Input:

5
2 4 3 1 6

Output:

3

Example 2

Input:

3
3 2 1

Output:

3

Example 3

Input:

3
1 3 2

Output:

1

Example notes

The answers are 3, 3 and 1. In the second example the two extremes cross, and that step must not be counted twice.

Editorial

Loading solution…