from itertools import pairwise from math import inf def main(): N, K = map(int, input().split()) a = sorted(map(int, input().split())) a_diff = [0] for a_elm1, a_elm2 in pairwise(a): a_diff.append(a_elm2 - a_elm1) dp_table = [[0 for _ in range(K+1)] for _ in range(N+1)] dp_table[0][0] = 0 for idx in range(1, N+1): dp_table[idx][0] = inf for idx in range(1, K+1): dp_table[0][idx] = inf for k in range(1, K+1): for n in range(1, N+1): dp_table[n][k] = min( dp_table[n-1][k-1], dp_table[n-1][k]+a_diff[n-1] ) print(dp_table[N][K]) if __name__ == "__main__": main()