#include <bits/stdc++.h>
using namespace std;
int a[1000000];
unsigned s[6000][6000];
unsigned solve(const int a[], const int g, int i, int j) {
  if (i < 0 || j < 0)
    return 0x7FFFFFFF;
  else if (i == 0 && j == 0)
    return 0;
  else if (s[i][j] == 0xFFFFFFFF)
    s[i][j] = min((a[g] - a[0]) + solve(a + g + 1, g, i - 1, j),
                  (a[g - 1] - a[0]) + solve(a + g, g, i, j - 1));
  return s[i][j];
}
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  memset(a, 0x00, sizeof(a));
  memset(s, 0xFF, sizeof(s));
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  sort(a, a + n);
  printf("%u\n", solve(a, n / k, n % k, k - n % k));
}
