#include <bits/stdc++.h>
using namespace std;
long long a[300005], dp[5005][5005];
long long min(long long a, long long b) { return a < b ? a : b; }
int main() {
  int n, k, q, r;
  long long sum = 0LL;
  ios::sync_with_stdio(0);
  cin >> n >> k;
  for (int i = 0; i < n; i++) cin >> a[i];
  sort(a, a + n);
  q = n / k;
  r = n % k;
  if (r) {
    memset(dp, 0x33, sizeof dp);
    dp[0][0] = 0LL;
    for (int i = 1; i <= k; i++)
      for (int j = 0; j <= r; j++) {
        if (j > i) break;
        long long pos = q * i + j;
        long long add = a[pos - 1] - a[pos - q];
        dp[i][j] = min(dp[i][j], dp[i - 1][j] + add);
        if (j) {
          add = a[pos - 1] - a[pos - q - 1];
          dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + add);
        }
      }
    sum = dp[k][r];
  } else {
    for (int i = 0, s = 0; i < k; i++, s += q) sum += a[s + q - 1] - a[s];
  }
  cout << sum << endl;
  return 0;
}
