#include <bits/stdc++.h>
using namespace std;
const int N = 5010, INF = 2000 * 1000 * 1000 + 10;
int d[N][N], a[300100];
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  int x = n % k, y = k - x, l = n / k;
  for (int i = 0; i <= y; i++) {
    for (int j = 0; j <= x; j++) {
      if (i || j) {
        d[i][j] = INF;
      }
      if (j) {
        d[i][j] = min(d[i][j], d[i][j - 1] + (a[l * i + (l + 1) * j - 1] -
                                              a[l * i + (l + 1) * (j - 1)]));
      }
      if (i) {
        d[i][j] = min(d[i][j], d[i - 1][j] + (a[l * i + (l + 1) * j - 1] -
                                              a[l * (i - 1) + (l + 1) * j]));
      }
    }
  }
  cout << d[y][x] << endl;
  return 0;
}
