def v(s): if len(s) < 2: return 0 return int(s[-1]) - int(s[0]) def va2(l, i): 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 group[:mini], v(group[:mini]), \ group[mini:], v(group[mini:]), 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 group1, v1, group2, v2 = groupSplit(groups[mini]) maxdiff = (v1 + v2) - v(list(groups[mini])) for i, g in enumerate(groups[1:], start=1): if v(g) < maxdiff: continue tgroup1, tv1, tgroup2, tv2 = groupSplit(list(g)) tmindiff = (tv1 + tv2) - v(list(g)) if tmindiff < maxdiff: mini = i group1 = tgroup1 group2 = tgroup2 v1, v2 = tv1, tv2 maxdiff = tmindiff del groups[mini] groups.append(tuple(sorted(group1))) groups.append(tuple(sorted(group2))) return sum([v(s) for s in groups]) if __name__ == '__main__': l, k = map(int, input().split()) print(solve(l, k, input().split()))