def v(data, v = None): s = list(data) if len(s) < 2: return 0 return int(s[-1]) - int(s[0]) def groupSplit(group): g1, g2 = group[:1], group[1:] v1, v2 = v(g1), v(g2) minv = v1 + v2 for i in range(2, len(group)): tg1, tg2 = group[:i], group[i:] tv1, tv2 = v(tg1), v(tg2) if tv1 + tv2 < minv: v1 = tv1 v2 = tv2 g1 = tg1 g2 = tg2 minv = tv1 + tv2 return g1, v1, g2, v2 def solve(_, k, d): d.sort(key=int) groups = {tuple([int(k) for k in d]) : v(d)} print(groups) for _ in range(0, k - 1): mingroup = sorted(groups, key=lambda x: x[0])[0] del groups[tuple(mingroup)] group1, v1, group2, v2 = groupSplit(mingroup) groups[group1] = v1 groups[group2] = v2 print(groups) return sum([s for s in groups.values()]) if __name__ == '__main__': l, k = map(int, input().split()) print(solve(l, k, input().split()))