#include <bits/stdc++.h>
using namespace std;
const int N = 3 * 100 * 1000 + 1000;
const int N1 = 5100;
long long A[N], n, k;
unsigned long long dp[N1][N1];
void read() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) cin >> A[i];
  sort(A, A + n);
  for (int i = 0; i < N1; i++)
    for (int j = 0; j < N1; j++) dp[i][j] = (1 << 31);
}
int cal(int p, int q) { return p * ((n + k - 1) / k) + q * (n / k) - 1; }
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  read();
  int p = n % k, q = k - n % k;
  dp[0][0] = 0;
  for (int i = 0; i <= p; i++)
    for (int j = 0; j <= q; j++) {
      int last = cal(i, j);
      if (i - 1 >= 0)
        dp[i][j] = min(dp[i][j],
                       dp[i - 1][j] + A[last] - A[last - (n + k - 1) / k + 1]);
      if (j - 1 >= 0)
        dp[i][j] =
            min(dp[i][j], dp[i][j - 1] + A[last] - A[last - (n / k) + 1]);
    }
  cout << dp[p][q] << endl;
  return 0;
}
