#include <bits/stdc++.h>
using namespace std;
const int MN = 3e5 + 10;
const int MK = 5e3 + 10;
const long long INF = 1e18 + 10;
int n, k;
int arr[MN];
long long dp[MK][MK];
int L, S;
int l, s;
void input() {
  ios_base ::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n; ++i) cin >> arr[i];
  sort(arr, arr + n);
  L = n % k;
  S = k - L;
  l = n / k + 1;
  s = n / k;
}
void solve() {
  for (int i = 0; i <= L; ++i)
    for (int j = 0; j <= S; ++j) dp[i][j] = INF;
  dp[0][0] = 0;
  for (int i = 0; i <= L; ++i)
    for (int j = 0; j <= S; ++j) {
      if (i == 0 && j == 0) continue;
      int sz = i * l + j * s - 1;
      if (i) {
        dp[i][j] = min(dp[i][j], dp[i - 1][j] + arr[sz] - arr[sz - l + 1]);
      }
      if (j) {
        dp[i][j] = min(dp[i][j], dp[i][j - 1] + arr[sz] - arr[sz - s + 1]);
      }
    }
}
int main() {
  ios_base ::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  input();
  if (n % k) {
    solve();
    cout << dp[L][S] << endl;
  } else {
    int ans = 0;
    for (int i = 0; i < n; i += n / k) ans += arr[i + n / k - 1] - arr[i];
    cout << ans << endl;
  }
  return 0;
}
