#include <bits/stdc++.h>
using namespace std;
int n, arr[300005], wct, uct, wlen, ulen, k;
long long dp[5005][5005];
int main() {
  scanf("%d%d", &n, &k);
  int xb, x2;
  for (xb = 0; xb < n; xb++) scanf("%d", arr + xb);
  wct = n % k;
  sort(arr, arr + n);
  if (!wct) wct = k;
  uct = k - wct;
  wlen = (n + k - 1) / k;
  ulen = wlen - 1;
  for (xb = 0; xb < 5005; xb++)
    fill(dp[xb], dp[xb] + 5005, 1000000000000000000LL);
  dp[0][0] = 0;
  for (xb = 1; xb <= k; xb++) {
    for (x2 = 0; x2 <= wct && x2 <= xb; x2++) {
      if (x2) {
        dp[xb][x2] = (long long)dp[xb - 1][x2 - 1] +
                     (long long)arr[xb * ulen + x2 - 1] -
                     (long long)arr[xb * ulen + x2 - wlen];
      }
      if (x2 < xb) {
        dp[xb][x2] =
            (long long)(((dp[xb][x2]) < ((long long)dp[xb - 1][x2] +
                                         (long long)arr[xb * ulen + x2 - 1] -
                                         (long long)arr[xb * ulen + x2 - ulen]))
                            ? (dp[xb][x2])
                            : ((long long)dp[xb - 1][x2] +
                               (long long)arr[xb * ulen + x2 - 1] -
                               (long long)arr[xb * ulen + x2 - ulen]));
      }
    }
  }
  printf("%I64d\n", dp[k][wct]);
  return 0;
}
