#include <bits/stdc++.h>
using namespace std;
int n, k;
int m = 0;
int arr[300009];
long long dp[5009][5009];
long long par[300009];
long long calc(int a, int b) {
  long long &ans = dp[a][b];
  if (ans != -1) return ans;
  ans = INT_MAX;
  if (a == 0 && b == 0) ans = 0;
  if (a != 0) {
    int l = (a - 1) * (m + 1) + b * m;
    int r = l + (m + 1);
    ans = min(ans, par[r - 1] - par[l] + calc(a - 1, b));
  }
  if (b != 0) {
    int l = (a) * (m + 1) + (b - 1) * m;
    int r = l + m;
    ans = min(ans, par[r - 1] - par[l] + calc(a, b - 1));
  }
  return ans;
}
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) cin >> arr[i];
  sort(arr + 1, arr + n + 1);
  memset(dp, -1, sizeof dp);
  for (int i = 1; i <= n; i++) par[i] = abs(arr[i + 1] - arr[i]) + par[i - 1];
  m = n / k;
  cout << calc(n % k, k - (n % k)) << endl;
  return 0;
}
