#include <bits/stdc++.h>
using namespace std;
int n, k;
int a[300003];
unordered_map<int, long long> d[300003];
inline long long calc() {
  long long res = 0;
  for (int i = 0; i + k < n; ++i) res += abs(a[i] - a[i + k]);
  return res;
}
inline void update(int n, int k, long long val) { d[n][k] = max(d[n][k], val); }
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
  sort(a, a + n);
  long long sum = 0;
  for (int i = 1; i < n; ++i) sum += abs(a[i] - a[i - 1]);
  int len = n / k;
  int k1 = k - n % k;
  int k2 = n % k;
  d[0][0] = 0;
  for (int i = 0; i < n; ++i) {
    if (i + len > n) continue;
    for (auto& key : d[i]) {
      int j = key.first;
      int val = key.second;
      int t = ((i + 1) - j * len) / (len + 1);
      if (j + 1 <= k1)
        update(i + len, j + 1,
               val + (i + len < n ? abs(a[i + len] - a[i + len - 1]) : 0));
      if (t + 1 <= k2)
        update(i + len + 1, j,
               val + (i + len + 1 < n ? abs(a[i + len + 1] - a[i + len]) : 0));
    }
    d[i].clear();
  }
  cout << sum - d[n][k1] << endl;
  return 0;
}
