#include <bits/stdc++.h>
using namespace std;
const int N = 3E5 + 5;
const int M = 5005;
int a[N], n, k, u, v, x, y, w;
long long f[M][M];
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= k; i++) {
    w = (n - i) / k + 1;
    if (!x) x = w;
    if (x != w && !y) y = w;
    if (x == w)
      u++;
    else if (y == w)
      v++;
  }
  for (int i = 0; i <= u; i++)
    for (int j = 0; j <= v; j++) f[i][j] = 1E18;
  f[0][0] = 0;
  for (int i = 0; i <= u; i++)
    for (int j = 0; j <= v; j++) {
      w = i * x + j * y + 1;
      if (i < u) f[i + 1][j] = min(f[i + 1][j], f[i][j] + a[w + x - 1] - a[w]);
      if (j < v) f[i][j + 1] = min(f[i][j + 1], f[i][j] + a[w + y - 1] - a[w]);
    }
  cout << f[u][v];
}
