I found an interesting problem on https://codeforces.com/problemset/problem/1328/F:
You are given the array $a$ consisting of $n$ elements and the integer $k \le n$.

You want to obtain at least $k$ equal elements in the array $a$. In one move, you can make one of the following two operations:

  Take one of the minimum elements of the array and increase its value by one (more formally, if the minimum value of $a$ is $mn$ then you choose such index $i$ that $a_i = mn$ and set $a_i := a_i + 1$);  take one of the maximum elements of the array and decrease its value by one (more formally, if the maximum value of $a$ is $mx$ then you choose such index $i$ that $a_i = mx$ and set $a_i := a_i - 1$). 

Your task is to calculate the minimum number of moves required to obtain at least $k$ equal elements in the array.


-----Input-----

The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of elements in $a$ and the required number of equal elements.

The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the $i$-th element of $a$.


-----Output-----

Print one integer — the minimum number of moves required to obtain at least $k$ equal elements in the array.


-----Examples-----
Input
6 5
1 2 2 4 2 3

Output
3

Input
7 5
3 3 2 1 1 1 3

Output
4

I tried it in Python, but could not do it. Can you solve it?
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,k = LI()
    a = LI()
    a.sort()
    d = defaultdict(lambda : 0)
    c = defaultdict(lambda : 0)
    s = [0]
    for i in a:
        d[i] += i
        c[i] += 1
        s.append(s[-1]+i)
    ans = float("inf")
    p = -1
    for i in a:
        if i == p:
            continue
        if k <= c[i]:
            ans = 0
            break
        l,r = bisect.bisect_left(a,i),bisect.bisect_right(a,i)
        m = r
        if m >= k:
            ns = l*(i-1)-s[l]
            su = ns+k-c[i]
            if su < ans:
                ans = su
        m = n-l
        if m >= k:
            ns = s[n]-s[r]-(n-r)*(i+1)
            su = ns+k-c[i]
            if su < ans:
                ans = su
        ns = s[n]-s[r]-(n-r)*(i+1)+l*(i-1)-s[l]
        su = ns+k-c[i]
        if su < ans:
            ans = su
        p = i
    print(ans)
    return

#Solve
def __starting_point():
    solve()

__starting_point()