#include <bits/stdc++.h>
using namespace std;
const long long inf = 1100000000000000000;
void solve() {
  long long n, k;
  cin >> n >> k;
  long long a[n];
  for (long long i = 0; i < n; i++) cin >> a[i];
  sort(a, a + n);
  long long dp[k + 5][k + 5];
  memset(dp, 0, sizeof(dp));
  long long x = n % k, c = (n + k - 1) / k, s = n / k;
  ;
  long long y = k - x;
  for (long long i = 0; i <= x; i++)
    for (long long j = 0; j <= y; j++) dp[i][j] = inf;
  dp[0][0] = 0;
  for (long long i = 0; i <= x; i++) {
    if (i)
      for (long long j = 0; j <= y; j++) {
        long long idx = (i - 1) * c + s * j;
        dp[i][j] = min(dp[i][j], abs(a[idx + c - 1] - a[idx]) + dp[i - 1][j]);
      }
    for (long long j = 1; j <= y; j++) {
      long long idx = i * c + s * (j - 1);
      dp[i][j] = min(dp[i][j], abs(a[idx + s - 1] - a[idx]) + dp[i][j - 1]);
    }
  }
  cout << dp[x][y];
}
signed main() {
  ios_base::sync_with_stdio(false), cin.tie(nullptr);
  long long T = 1;
  while (T--) {
    solve();
    cout << '\n';
  }
  return 0;
}
