#include <bits/stdc++.h>
using namespace std;
long long int t, i, j, k, l, n, m, a[300010], d[5010][5010];
int main() {
  cin >> n >> m;
  t = n / m;
  l = n % m;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  for (i = 0; i <= m; i++) {
    for (j = 0; j <= l; j++) {
      d[i][j] = 9e18;
    }
  }
  d[0][0] = 0;
  for (i = 1; i <= m; i++) {
    for (j = 0; j <= l; j++) {
      d[i][j] = d[i - 1][j] + abs(a[t * i + j - 1] - a[t * i + j - t]);
      if (j) {
        d[i][j] = min(d[i][j], d[i - 1][j - 1] + abs(a[t * i + j - 1] -
                                                     a[t * i + j - t - 1]));
      }
    }
  }
  cout << d[m][n % m] << endl;
}
