#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline void wrote(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) wrote(x / 10);
  putchar(x % 10 + '0');
}
inline void writeln(long long x) {
  wrote(x);
  puts("");
}
const long long N = 300010;
long long n, k, a[N], pre[N], f[5010][5010];
int main() {
  n = read();
  k = read();
  for (long long i = 1; i <= n; ++i) a[i] = read();
  sort(a + 1, a + n + 1);
  long long sum_more = n % k, sum_less = k - sum_more;
  memset(f, 60, sizeof f);
  f[0][0] = 0;
  for (long long i = 0; i <= sum_more; ++i)
    for (long long j = 0; j <= sum_less; ++j) {
      long long now = i * (n / k + 1) + j * (n / k);
      if (i) f[i][j] = min(f[i][j], f[i - 1][j] + a[now] - a[now - n / k]);
      if (j) f[i][j] = min(f[i][j], f[i][j - 1] + a[now] - a[now - n / k + 1]);
    }
  writeln(f[sum_more][sum_less]);
}
