#C1772. Minimum Sorting Time

    ID: 45014 Type: Default 1000ms 256MiB

Minimum Sorting Time

Minimum Sorting Time

You are given a list of n integers representing the weights of books. You need to determine the minimum time (in seconds) required to sort these books in non-decreasing order if you can only swap adjacent books. The time required to perform a swap is 1 second.

This problem is equivalent to counting the number of inversions in the sequence. An inversion is a pair of indices \(i, j\) such that \(i a[j]\). The minimal number of adjacent swaps required to sort the list is exactly the number of inversions in the list. Formally, if we denote the inversions by \(T\), then:

[ T = \sum_{i<j} \mathbf{1}_{{a[i] > a[j]}} ]

Your task is to compute \(T\) given the list of weights.

inputFormat

The input is read from standard input (stdin) and consists of two lines:

  1. The first line contains a single integer \(n\) (the number of books).
  2. The second line contains \(n\) space-separated integers representing the weights of the books.

outputFormat

Output a single integer on standard output (stdout), which is the minimum time (the number of adjacent swaps) needed to sort the books in non-decreasing order.

## sample
5
4 3 2 1 5
6