#P11908. Minimum Adjacent Swaps to Reorder Top Artworks

    ID: 14012 Type: Default 1000ms 256MiB

Minimum Adjacent Swaps to Reorder Top Artworks

Minimum Adjacent Swaps to Reorder Top Artworks

In country \(H\), there is a museum with a corridor where \(n\) artworks are arranged in a straight line. The artwork at the \(i\)-th position (from left to right) has a value \(c_i\). Today, an important VIP guest is visiting the museum, but due to a tight schedule, the guest can only view the first \(k\) artworks from the entrance (i.e. the leftmost \(k\) pieces). In order to improve the museum's image, the curator has decided to move the \(k\) most valuable artworks to these \(k\) front positions.

Since the artworks are extremely precious, each move can only swap two adjacent pieces. To minimize the risk of damaging the artworks, the curator requires that the rearrangement be completed in the minimum number of moves possible.

Given the current values of the artworks, output the minimum number of adjacent swaps needed to complete the rearrangement.

inputFormat

The first line contains two integers \(n\) and \(k\) (\(1 \le k \le n\)). The second line contains \(n\) integers \(c_0, c_1, \ldots, c_{n-1}\) representing the value of each artwork in order from left to right.

It is guaranteed that the values are distinct.

outputFormat

Output a single integer: the minimum number of adjacent swaps required to bring the \(k\) highest valued artworks to the front (first \(k\) positions), while keeping their relative order the same as in the original sequence.

sample

5 2
3 1 5 2 4
5