#K51232. Carrot Sorting: Minimum Swap Count

    ID: 29042 Type: Default 1000ms 256MiB

Carrot Sorting: Minimum Swap Count

Carrot Sorting: Minimum Swap Count

You are given a list of carrots represented by their lengths. Each carrot can be swapped with its adjacent neighbor at a cost of one carrot coin per swap. Your task is to compute the minimum number of swaps required to sort the list in non-decreasing order. Mathematically, given an array (a_1, a_2, \ldots, a_n), you need to determine the minimum number of adjacent swaps required so that (a_1 \le a_2 \le \ldots \le a_n).

inputFormat

The input consists of two lines:

  • The first line contains an integer (n) ((1 \le n \le 10^5)) representing the number of carrots.
  • The second line contains (n) space-separated integers, each denoting the length of a carrot.

outputFormat

Output a single integer representing the minimum number of swaps required to sort the list of carrots in non-decreasing order.## sample

5
4 3 2 5 1
7