#include <bits/stdc++.h>
int n, k, a[300011];
long long s[300001];
int num1, num2, len1, len2;
long long f[5001][5001];
long long calc(int l, int r) { return s[r - 1] - s[l - 1]; }
int get(int len) { return (len / k) + (len % k > 0); }
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  std::sort(a + 1, a + n + 1);
  for (int i = 1; i < n; i++) s[i] = s[i - 1] + a[i + 1] - a[i];
  num1 = 1;
  num2 = 0;
  len1 = get(n);
  for (int st = 2; st <= k; st++) {
    int lennow = get(n - st + 1);
    if (lennow != len1) {
      len2 = lennow;
      num2++;
    } else
      num1++;
  }
  if (num2 == 0) {
    long long ans = 0;
    for (int i = 1; i <= k; i++) ans += calc((i - 1) * len1 + 1, i * len1);
    std::cout << ans << std::endl;
  } else {
    f[0][0] = 0;
    int i, j, totallen;
    long long tmp1, tmp2;
    for (i = 0; i <= num1; i++)
      for (j = 0; j <= num2; j++) {
        if (i == 0 && j == 0) continue;
        totallen = i * len1 + j * len2;
        if (i >= 1)
          tmp1 = f[i - 1][j] + calc(totallen - len1 + 1, totallen);
        else
          tmp1 = -1;
        if (j >= 1)
          tmp2 = f[i][j - 1] + calc(totallen - len2 + 1, totallen);
        else
          tmp2 = -1;
        if (tmp1 == -1)
          f[i][j] = tmp2;
        else if (tmp2 == -1)
          f[i][j] = tmp1;
        else {
          if (tmp1 < tmp2)
            f[i][j] = tmp1;
          else
            f[i][j] = tmp2;
        }
      }
    std::cout << f[num1][num2] << std::endl;
  }
}
