#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
const int N = int(3e5) + 10;
const int INF = int(1e9);
int A[N], dp[N];
int main() {
  int n;
  scanf("%d", &n);
  int k;
  scanf("%d", &k);
  for (int i = (1); i < (n + 1); ++i) scanf("%d", &A[i]);
  sort(A + 1, A + n + 1);
  for (int i = (0); i < (k); ++i) dp[i] = INF;
  dp[0] = 0;
  set<pair<int, int> > DP, cost;
  DP.insert({INF, 0});
  cost.insert({A[k] - A[1], 0});
  int add = 0;
  for (int i = (k); i < (n + 1); ++i) {
    dp[i] = min(cost.begin()->first + add, DP.begin()->first);
    DP.insert({dp[i - k + 1], i - k + 1});
    add += A[i + 1] - A[i];
    while (true) {
      auto top = *DP.begin();
      if (top.first == INF) break;
      int idx = top.second, dpval = top.first;
      if (dpval > A[i + 1] - A[idx + 1]) {
        break;
      }
      DP.erase(DP.begin());
      cost.insert({A[i + 1] - A[idx + 1] - add, idx});
    }
  }
  printf("%d\n", dp[n]);
  return 0;
}
