#include <bits/stdc++.h>
using namespace std;
int arr[300001];
long long int dp[(int)(25e6 + 2)];
int n;
int smallSz;
int largeSz;
int small;
int large;
int dpp(int s, int l) {
  if (dp[s * (large + 1) + l] != ((int)1e18)) return dp[s * (large + 1) + l];
  if (s * smallSz + l * largeSz == n) return 0;
  int i = s * smallSz + l * largeSz;
  int ids = (s + 1) * smallSz + l * largeSz - 1;
  int idl = (s)*smallSz + (l + 1) * largeSz - 1;
  if (ids < n && s < small)
    dp[s * (large + 1) + l] =
        ((dp[s * (large + 1) + l] > dpp(s + 1, l) + abs(arr[i] - arr[ids]))
             ? dpp(s + 1, l) + abs(arr[i] - arr[ids])
             : dp[s * (large + 1) + l]);
  if (idl < n && l < large)
    dp[s * (large + 1) + l] =
        ((dp[s * (large + 1) + l] > dpp(s, l + 1) + abs(arr[i] - arr[idl]))
             ? dpp(s, l + 1) + abs(arr[i] - arr[idl])
             : dp[s * (large + 1) + l]);
  return dp[s * (large + 1) + l];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int k;
  cin >> n >> k;
  smallSz = (n - n % k) / k;
  largeSz = smallSz + 1;
  large = n % k;
  small = (n - largeSz * large) / smallSz;
  fill(dp, dp + (small + 1) * (large + 1), ((int)1e18));
  for (int i = 0; i < n; i++) cin >> arr[i];
  sort(arr, arr + n);
  cout << dpp(0, 0);
  return 0;
}
