#include <bits/stdc++.h>
using namespace std;
const int maxn = 3 * 100000 + 5;
const int maxk = 5005;
const long long INF = 1LL << 60;
int dp[maxk][maxk];
int a, b;
long long g[maxn][2];
vector<int> v;
long long f(int n, int x, int y) {
  if (x == 0 and y == 0) return 0;
  int i = n - (a * x + b * y);
  if (dp[x][y] == -1) {
    long long ret = INF;
    if (x) ret = min(ret, g[i][0] + f(n, x - 1, y));
    if (y) ret = min(ret, g[i][1] + f(n, x, y - 1));
    dp[x][y] = ret;
  }
  return dp[x][y];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  int n, k;
  cin >> n >> k;
  v.resize(n);
  for (int i = 0; i < n; ++i) cin >> v[i];
  sort(v.begin(), v.end());
  int x, y;
  a = 0, b = 0, x = 0, y = 0;
  vector<int> mark(n);
  for (int i = 0; i < n; ++i) {
    if (mark[i] == 0) {
      int t = i;
      int cnt = 0;
      while (t < n) {
        cnt++;
        mark[t] = 1;
        t += k;
      }
      if (i == 0) {
        a = cnt;
        x++;
      } else {
        if (cnt == a) {
          x++;
        } else {
          y++;
          b = cnt;
        }
      }
    }
  }
  long long h[maxn];
  for (int i = n - 2; i >= 0; --i) {
    h[i] = h[i + 1] + abs(v[i] - v[i + 1]);
  }
  for (int i = 0; i < n; ++i) {
    g[i][0] = g[i][1] = INF;
    if (i + a <= n) {
      g[i][0] = h[i] - h[i + a - 1];
    }
    if (i + b <= n) {
      g[i][1] = h[i] - h[i + b - 1];
    }
  }
  memset(dp, -1, sizeof dp);
  long long ans = f(a * x + b * y, x, y);
  cout << ans << '\n';
  return 0;
}
