def v(s): if len(s) < 2: return 0 return int(s[-1]) - int(s[0]) def va2(l, i): if len(l) <= 1: return 0 v1 = l[i-1] - l[0] v2 = l[-1] - l[i] return v1 + v2 def groupSplit(group): mini = 1 minv = va2(group, 1) for i in range(2, len(group)): tminv = va2(group, i) if tminv < minv: minv = tminv mini = i return mini, minv def solve(l, k, d): if l == k: return 0 d.sort(key=int) groups = [tuple([int(k) for k in d])] for _ in range(0, k - 1): mini = 0 min_split_index, value = groupSplit(groups[mini]) maxdiff = value - v(list(groups[mini])) for i, g in enumerate(groups[1:], start=1): if v(g) < maxdiff: continue split_index, value = groupSplit(list(g)) tmindiff = value - v(list(g)) if tmindiff < maxdiff: mini = i maxdiff = tmindiff min_split_index = split_index groups.append(tuple(sorted(groups[mini][:min_split_index]))) groups.append(tuple(sorted(groups[mini][min_split_index:]))) del groups[mini] return sum([v(s) for s in groups]) if __name__ == '__main__': l, k = map(int, input().split()) print(solve(l, k, input().split()))