#include <bits/stdc++.h>
using namespace std;
int dp[5001][5001], n, k, p, q, pp, qq;
int main() {
  cin >> n >> k;
  p = (n - 1) / k + 1;
  q = p - 1, pp = n - q * k, qq = k - pp;
  int a[n], sum[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  for (int i = 0; i < n; i++) {
    sum[i] = 0;
    if (i) sum[i] = (sum[i - 1] + a[i] - a[i - 1]);
  }
  for (int i = 0; i <= qq; i++) {
    for (int j = 0; j <= pp; j++) {
      if (!i and !j) continue;
      if (!i)
        dp[i][j] = sum[j * p - 1] - sum[(j - 1) * p] + dp[i][j - 1];
      else if (!j)
        dp[i][j] = sum[i * q - 1] - sum[(i - 1) * q] + dp[i - 1][j];
      else
        dp[i][j] = min(
            dp[i][j - 1] + sum[i * q + j * p - 1] - sum[i * q + (j - 1) * p],
            dp[i - 1][j] + sum[i * q + j * p - 1] - sum[(i - 1) * q + j * p]);
    }
  }
  cout << dp[qq][pp];
}
