#include <bits/stdc++.h>
using namespace std;
int n, k;
int sobra;
int tam;
long long int v[300005];
long long int t[5001][5001];
int le, ln;
int ve, vn;
long long int solve(int normal, int especial) {
  if (normal == ln && especial == le) return 0;
  if (normal > ln || especial > le) return 0x3f3f3f3f3f3f3f3f;
  long long int &ret = t[normal][especial];
  if (ret != -1) return ret;
  long long int subn, sube;
  subn = 0x3f3f3f3f3f3f3f3f;
  sube = 0x3f3f3f3f3f3f3f3f;
  int atual = normal * vn + especial * ve;
  ret = 0x3f3f3f3f3f3f3f3f;
  if (normal < ln) {
    subn = v[atual + vn - 1] - v[atual];
    if (atual + vn - 1 >= n) subn = 0;
    ret = min(ret, solve(normal + 1, especial) + subn);
  }
  if (especial < le) {
    sube = v[atual + ve - 1] - v[atual];
    if (atual + ve - 1 >= n) sube = 0;
    ret = min(ret, solve(normal, especial + 1) + sube);
  }
  return ret;
}
int main() {
  memset(t, -1, sizeof(t));
  cin >> n >> k;
  vn = n / k;
  ve = vn;
  if (n % k) ve++;
  le = n % k;
  ln = k - le;
  if (ln * vn + le * ve != n) {
    cout << 123123123123123123 << endl;
    return 0;
  }
  for (int i = 0; i < n; i++) cin >> v[i];
  sort(v, v + n);
  cout << solve(0, 0) << endl;
}
