#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e9 + 7, maxn = 1e6 + 100, mod = 998244353;
long long pref[maxn];
void solve() {
  long long n, k;
  cin >> n >> k;
  vector<long long> a(n);
  for (long long i = 0; i < n; ++i) cin >> a[i];
  sort(a.begin(), a.end());
  pref[0] = 0;
  for (long long i = 1; i <= n; ++i) pref[i] = pref[i - 1] + a[i - 1];
  long long x = n % k, y = k - (n % k), l = n / k;
  vector<vector<long long> > dp(x + 1, vector<long long>(y + 1, 2 * inf));
  dp[0][0] = 0;
  for (long long i = 0; i <= x; ++i) {
    for (long long j = 0; j <= y; ++j) {
      if (i > 0)
        dp[i][j] = min(dp[i][j], dp[i - 1][j] + a[i * (l + 1) + j * l - 1] -
                                     a[(i - 1) * (l + 1) + j * l]);
      if (j > 0)
        dp[i][j] = min(dp[i][j], dp[i][j - 1] + a[i * (l + 1) + j * l - 1] -
                                     a[i * (l + 1) + (j - 1) * l]);
    }
  }
  cout << dp[x][y];
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  srand(100);
  cout << fixed << setprecision(8);
  long long t = 1;
  while (t--) {
    solve();
    cout << "\n";
  }
}
