#include <bits/stdc++.h>
using namespace std;
const int N = 300300, M = 5050;
const long long oo = 1000000000000000000ll;
int i, j, k, n, m, ch, ff, t, l;
int a[N];
long long f[M][M];
void R(int &x) {
  x = ff = 0;
  ch = getchar();
  while (ch < '0' || '9' < ch) {
    if (ch == '-') ff = 1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  if (ff) x = -x;
}
void W(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) W(x / 10);
  putchar(x % 10 + '0');
}
int min(const int &x, const int &y) {
  if (x < y) return x;
  return y;
}
long long min(const long long &x, const long long &y) {
  if (x < y) return x;
  return y;
}
int main() {
  R(n);
  R(m);
  memset(f, 60, sizeof f);
  for (i = 1; i <= n; i++) R(a[i]);
  sort(a + 1, a + n + 1);
  t = n % m;
  l = n / m;
  f[0][0] = 0;
  for (i = 0; i < m; i++)
    for (j = min(t, i); j >= 0; j--)
      if (f[i][j] < oo) {
        f[i + 1][j] =
            min(f[i + 1][j], f[i][j] + a[(i + 1) * l + j] - a[i * l + j + 1]);
        if (j + 1 <= t)
          f[i + 1][j + 1] =
              min(f[i + 1][j + 1],
                  f[i][j] + a[(i + 1) * l + j + 1] - a[i * l + j + 1]);
      }
  W(f[m][t]);
  puts("");
}
