#include <bits/stdc++.h>
using namespace std;
int n, k, s, l;
long long a[300300];
long long ans;
long long f[5100][5100];
long long F(int x, int y) {
  if (x < 0 || y < 0) return 0x43000000;
  return f[x][y];
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) cin >> a[i];
  sort(a, a + n);
  memset(f, 0x3f, sizeof(f));
  s = n % k;
  l = k - s;
  f[0][0] = 0;
  for (int i = 1; i <= s; i++) {
    f[i][0] = f[i - 1][0] + a[i * (n / k + 1) - 1] - a[(i - 1) * (n / k + 1)];
  }
  for (int i = 1; i <= l; i++) {
    f[0][i] = f[0][i - 1] + a[i * (n / k) - 1] - a[(i - 1) * (n / k)];
  }
  for (int i = 1; i <= s; i++) {
    for (int j = 1; j <= l; j++) {
      f[i][j] = min(F(i - 1, j) - a[(i - 1) * (n / k + 1) + j * (n / k)],
                    F(i, j - 1) - a[i * (n / k + 1) + (j - 1) * (n / k)]) +
                a[i * (n / k + 1) + j * (n / k) - 1];
    }
  }
  cout << f[s][l] << endl;
  return 0;
}
