#include <bits/stdc++.h>
using namespace std;
const long double eps = 1e-9;
const int inf = (1 << 30) - 1;
const long long inf64 = ((long long)1 << 62) - 1;
const long double pi = acos(-1);
template <class T>
T sqr(T x) {
  return x * x;
}
template <class T>
T abs(T x) {
  return x < 0 ? -x : x;
}
const int MAXN = 320 * 1000;
const int MAXK = 5500;
int a[MAXN];
int ans[MAXK][MAXK];
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }
  sort(a, a + n);
  ans[0][0] = 0;
  int s1 = n / k;
  int n1 = k - (n % k);
  int s2 = (n + k - 1) / k;
  int n2 = n % k;
  for (int i = 0; i <= n1; ++i) {
    for (int j = 0; j <= n2; ++j) {
      if (i == 0 && j == 0) {
        continue;
      }
      ans[i][j] = 2 * inf;
      int tmp = i * s1 + j * s2;
      if (i > 0) {
        ans[i][j] = min(ans[i][j], (ans[i - 1][j] - a[tmp - s1]) + a[tmp - 1]);
      }
      if (j > 0) {
        ans[i][j] = min(ans[i][j], (ans[i][j - 1] - a[tmp - s2]) + a[tmp - 1]);
      }
    }
  }
  printf("%d\n", ans[n1][n2]);
  return 0;
}
