#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5100, INF = 1000000007;
long long f[MAXN][MAXN], a[300101];
int n, m;
bool v[MAXN][MAXN];
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]);
  sort(a + 1, a + n + 1);
  int p, q;
  p = n / m;
  q = n % m;
  v[0][0] = 1;
  f[0][0] = 0;
  for (int i = 1; i <= m; i++)
    for (int j = 0; j <= q && j <= m; j++)
      if (v[i - 1][j]) {
        long long k;
        k = f[i - 1][j] + a[i * p + j] - a[(i - 1) * p + j + 1];
        if (k < f[i][j] || v[i][j] == 0) {
          f[i][j] = k;
          v[i][j] = 1;
        }
        k = f[i - 1][j] + a[i * p + j + 1] - a[(i - 1) * p + j + 1];
        if (j + 1 <= q && (k < f[i][j + 1] || v[i][j + 1] == 0)) {
          f[i][j + 1] = k;
          v[i][j + 1] = 1;
        }
      }
  cout << f[m][q] << endl;
  return 0;
}
