#include <bits/stdc++.h>
using namespace std;
const int N = 311111, M = 5001;
const long long base = 33ll;
const long long MOD = 1000000007ll;
int n, m, a[N];
long long d[M][M];
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; ++i) cin >> a[i];
  sort(a, a + n);
  for (int i = 0; i < M; ++i)
    for (int j = 0; j < M; ++j) d[i][j] = (long long)1e16;
  d[0][0] = 0;
  int A = n / m;
  for (int i = 1; i <= m; ++i)
    for (int j = 0; j <= i; ++j) {
      int left = i * A + j - 1;
      d[i][j] = d[i - 1][j] + a[left] - a[left - A + 1];
      if (j) d[i][j] = min(d[i][j], d[i - 1][j - 1] + a[left] - a[left - A]);
    }
  cout << d[m][n % m];
}
