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)} for _ in range(0, k - 1): ming = list(groups.keys())[0] group1, v1, group2, v2 = groupSplit(ming) mindiff = (v1 + v2) - groups[ming] for g in list(groups.keys())[1:]: tgroup1, tv1, tgroup2, tv2 = groupSplit(g) tmindiff = (tv1 + tv2) - groups[g] if tmindiff < mindiff: ming = g group1 = tgroup1 group2 = tgroup2 v1, v2 = tv1, tv2 mindiff = tmindiff del groups[tuple(ming)] groups[group1] = v1 groups[group2] = v2 return sum([s for s in groups.values()]) if __name__ == '__main__': l, k = map(int, input().split()) print(solve(l, k, input().split()))