#include <bits/stdc++.h>
using namespace std;
int i, i0, n, k, a[300005];
long long dp[5005][5005];
int main() {
  scanf("%d %d", &n, &k);
  for (i = 1; i <= n; i++) scanf("%d", &a[i]);
  sort(a + 1, a + 1 + n);
  int cnt = k - n % k, cnt0 = n % k, len = n / k, len0 = n / k + 1;
  for (i = 1; i <= cnt; i++) {
    for (i0 = 0; i0 <= 0; i0++) {
      dp[i][i0] = dp[i - 1][i0] - a[i * len - len + i0 * len0 + 1] +
                  a[i * len + i0 * len0];
    }
  }
  for (i = 0; i <= 0; i++) {
    for (i0 = 1; i0 <= cnt0; i0++) {
      dp[i][i0] = dp[i][i0 - 1] - a[i * len + i0 * len0 - len0 + 1] +
                  a[i * len + i0 * len0];
    }
  }
  for (i = 1; i <= cnt; i++) {
    for (i0 = 1; i0 <= cnt0; i0++) {
      dp[i][i0] = min(dp[i - 1][i0] - a[i * len - len + i0 * len0 + 1],
                      dp[i][i0 - 1] - a[i * len + i0 * len0 - len0 + 1]) +
                  a[i * len + i0 * len0];
    }
  }
  printf("%lld\n", dp[cnt][cnt0]);
  return 0;
}
