#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T abs(T a) {
  return ((a < 0) ? -a : a);
}
template <typename T>
inline T sqr(T a) {
  return a * a;
}
const int N = 300009;
const int logN = 22;
const int sqrN = 450;
const int MOD = 1000000007;
const int INF = 1e9 + 100;
const long long INF64 = 2e18;
const long double PI = 3.1415926535897932384626433832795;
const long double eps = 1e-9;
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
long long a[N];
long long f[N];
long long dp[5009][5009];
long long get(int l, int r) { return f[r] - f[l]; }
int main() {
  srand(time(NULL));
  cout << setprecision(10) << fixed;
  cerr << setprecision(10) << fixed;
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < (int)(n); ++i) cin >> a[i];
  sort(a, a + n);
  for (int i = 1; i < n; ++i) f[i] = f[i - 1] + a[i] - a[i - 1];
  if (n % k == 0) {
    long long res = 0;
    long long d = n / k;
    for (int i = 0; i < (int)(k); ++i) {
      int l = i * d, r = i * d + d - 1;
      res += get(l, r);
    }
    cout << res;
    return 0;
  }
  map<int, int> m;
  for (int i = 1; i <= k; ++i) m[((n - i) / k)]++;
  vector<int> v;
  vector<int> cnt;
  for (map<int, int>::iterator it = m.begin(); it != m.end(); ++it) {
    v.push_back(it->first);
    cnt.push_back(it->second);
  }
  for (int i = 0; i < (int)(2); ++i) ++v[i];
  for (int i = 0; i < (int)(k + 3); ++i)
    for (int j = 0; j < (int)(k + 3); ++j) dp[i][j] = INF64;
  dp[0][0] = 0;
  for (int i = 0; i < (int)(k + 3); ++i)
    for (int j = 0; j < (int)(k + 3); ++j) {
      int cnt1 = j, cnt2 = i - j;
      int l = cnt1 * v[0] + cnt2 * v[1];
      int r = l + v[0] - 1;
      if (r < n) dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + get(l, r));
      r = l + v[1] - 1;
      if (r < n) dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + get(l, r));
    }
  long long ans = INF64;
  cout << dp[cnt[0] + cnt[1]][cnt[0]];
  return 0;
}
