#include <bits/stdc++.h>
using namespace std;
template <class T, class L>
bool smax(T &x, L y) {
  return x < y ? (x = y, 1) : 0;
}
template <class T, class L>
bool smin(T &x, L y) {
  return y < x ? (x = y, 1) : 0;
}
const int maxn = 3e5 + 17, maxk = 5e3 + 17;
int n, small, big, a[maxn], k;
long long save[maxk][maxk];
long long hamid(int s, int b) {
  if (~save[s][b]) return save[s][b];
  if (!s && !b) return 0;
  int i = s * small + b * big - 1;
  long long ans = 1e18;
  if (s) smin(ans, hamid(s - 1, b) + a[i] - a[i - small + 1]);
  if (b) smin(ans, hamid(s, b - 1) + a[i] - a[i - big + 1]);
  return save[s][b] = ans;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n; i++) cin >> a[i];
  memset(save, -1, sizeof save);
  sort(a, a + n);
  small = n / k, big = small + 1;
  cout << hamid(k - n % k, n % k) << '\n';
  return 0;
}
