PPaste!

My code - you only need to check those 2 functions.

Home - All the pastes - Authored by Thooms

Raw version

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from itertools import combinations as cmb

def mus(array):
    return sum([abs(i[0]-i[1]) for i in cmb(array,2)]) 

def LetMeDoIt(n, k, arr1):
    res = 1000000
    arr = sorted(arr1)
    
    for x in range(n-k):
        res = min(res, mus(arr[x:x+k]))

    return res

def MainApp():
    n = int(input())
    k = int(input())

    arr1 = list(int(input()) for _ in range(n))

    result = LetMeDoIt(n, k, arr1)

    print(result)    

if __name__ == '__main__':
    MainApp()