#include <bits/stdc++.h>
template <class T>
inline void rd(T &x) {
  char c = getchar();
  x = 0;
  while (!isdigit(c)) c = getchar();
  while (isdigit(c)) {
    x = x * 10 + c - '0';
    c = getchar();
  }
}
using namespace std;
const int N = 3e5 + 10;
const int M = 5010;
long long a[N], oo = 0x3f3f3f3f * 1000000LL;
long long f[2][M];
int k1, k2, len1, len2;
int g(int x, int y) { return x * len1 + y * len2; }
int main() {
  int n, m;
  scanf("%d", &n);
  scanf("%d", &m);
  for (int i = 0; i < n; i++) scanf("%I64d", &a[i]);
  sort(a, a + n);
  len1 = n / m, k2 = n % m;
  k1 = m - k2;
  len2 = len1 + 1;
  int tag = 0;
  for (int p = 0; p < k1 + k2 + 1; p++) {
    tag ^= 1;
    for (int i = 0; i < k1 + 1; i++) {
      int j = p - i;
      if (j >= 0 && j <= k2) {
        if (i || j)
          f[tag][i] =
              min(i ? f[!tag][i - 1] + a[g(i, j) - 1] - a[g(i - 1, j)] : oo,
                  j ? f[!tag][i] + a[g(i, j) - 1] - a[g(i, j - 1)] : oo);
      }
    }
  }
  cout << f[tag][k1] << endl;
  return 0;
}
