#include <bits/stdc++.h>
using namespace std;
int n, k;
long long dp[5001][5001];
int extra = 0;
int mx;
vector<int> a;
long long sol(int i, int ex) {
  if (i == k) {
    if (ex == extra)
      return 0;
    else
      return 1000000000000ll;
  }
  if (dp[i][ex] != -1) return dp[i][ex];
  int in = i * (mx) + ex;
  long long mn = 10000000000000ll;
  if (ex < extra) {
    mn = a[in + mx] - a[in] + sol(i + 1, ex + 1);
  }
  mn = min(mn, a[in + mx - 1] - a[in] + sol(i + 1, ex));
  return dp[i][ex] = mn;
}
int main() {
  scanf("%d %d", &n, &k);
  a = vector<int>(n);
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  sort(a.begin(), a.end());
  mx = a.size() - 1;
  mx /= k;
  for (int i = 0; i < k; i++) {
    int len = a.size() - 1 - i;
    len /= k;
    if (len == mx) extra++;
  }
  for (int i = 0; i < 5001; i++)
    for (int j = 0; j < 5001; j++) dp[i][j] = -1;
  cout << sol(0, 0) << endl;
}
