#include <bits/stdc++.h>
using namespace std;
long long a[1111111];
long long f[5005][5005];
long long st, ed;
long long temp1, temp2;
int n, k, m, r;
int i, j;
int main() {
  scanf("%d%d", &n, &k);
  for (i = 1; i <= n; i++) scanf("%I64d", &a[i]);
  sort(a + 1, a + n + 1);
  m = n / k;
  r = n - k * m;
  for (i = 0; i < 5005; i++)
    for (j = 0; j < 5005; j++) f[i][j] = 1000000000000000000ll;
  f[0][0] = 0;
  for (i = 1; i <= k; i++)
    for (j = 0; j <= min(i, r); j++) {
      ed = (i - j) * m + j * (m + 1);
      st = ed - m + 1;
      temp1 = f[i - 1][j] + (a[ed] - a[st]);
      temp2 = 1000000000000000000ll;
      if (j > 0) {
        st = ed - m;
        temp2 = f[i - 1][j - 1] + (a[ed] - a[st]);
      }
      f[i][j] = min(temp1, temp2);
    }
  printf("%I64d\n", f[k][r]);
}
