#include <bits/stdc++.h>
using namespace std;
template <typename T>
void print(T t) {
  cout << t << endl;
}
template <typename T, typename... Args>
void print(T t, Args... args) {
  cout << t << " ";
  print(args...);
}
long long n, k;
long long arr[300005];
long long cache[5005][5005];
long long dp(long long tk, long long rem) {
  if (tk == k) {
    if (rem == n % k) return 0;
    return 1e18;
  }
  if (rem > n % k) return 1e18;
  long long &ans = cache[tk][rem];
  if (ans != -1) return ans;
  long long currind = tk * (n / k) + rem;
  long long ahead = currind + n / k - 1;
  ans = min(arr[ahead] - arr[currind] + dp(tk + 1, rem),
            arr[ahead + 1] - arr[currind] + dp(tk + 1, rem + 1));
  return ans;
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n >> k;
  for (long long i = 0; i < n; i++) cin >> arr[i];
  sort(arr, arr + n);
  memset(cache, -1, sizeof(cache));
  cout << dp(0, 0);
}
