#include <bits/stdc++.h>
using namespace std;
const int maxn = 300009;
map<short, int> f[maxn];
int a[maxn], n, K, p, q, d;
int sum[maxn], i, j, k;
int main() {
  scanf("%d%d", &n, &K);
  d = n / K;
  for (i = 1; i <= n; i++) scanf("%d", &a[i]);
  sort(a + 1, a + n + 1);
  for (i = 2; i <= n; i++) sum[i] = sum[i - 1] + a[i] - a[i - 1];
  f[0][0] = 0;
  for (i = 1; i <= n; i++) {
    for (k = 0; k <= 1; k++) {
      if (i >= d)
        for (map<short, int>::iterator j = f[i - d].begin();
             j != f[i - d].end(); j++)
          if (j->first + k <= n % K) {
            if (!f[i].count(j->first + k))
              f[i][j->first + k] = j->second + sum[i] - sum[i - d + 1];
            else
              f[i][j->first + k] =
                  min(f[i][j->first + k], j->second + sum[i] - sum[i - d + 1]);
          }
      d++;
    }
    if (i >= d) f[i - d].clear();
    d -= 2;
  }
  printf("%d", f[n][n % K]);
  return 0;
}
