#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-10;
const double PI = acos(-1.0);
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int N, K;
  cin >> N >> K;
  vector<int> xs(N);
  for (int i = (0); i < (N); ++i) cin >> xs[i];
  sort((xs).begin(), (xs).end());
  int n1 = N % K, n2 = K - n1, l2 = N / K;
  vector<vector<long long>> dp(n1 + 1, vector<long long>(n2 + 1, 0));
  for (int i = 0; i <= n1; ++i) {
    for (int j = 0; j <= n2; ++j) {
      int idx = i * (l2 + 1) + j * l2;
      if (i < n1)
        dp[i + 1][j] =
            max(dp[i + 1][j],
                dp[i][j] +
                    (idx + 1 + l2 >= N ? 0 : xs[idx + 1 + l2] - xs[idx + l2]));
      if (j < n2)
        dp[i][j + 1] = max(
            dp[i][j + 1],
            dp[i][j] + (idx + l2 >= N ? 0 : xs[idx + l2] - xs[idx + l2 - 1]));
    }
  }
  long long ans = 0;
  for (int i = (0); i < (N - 1); ++i) ans += xs[i + 1] - xs[i];
  cout << ans - dp[n1][n2] << endl;
  return 0;
}
