#include <bits/stdc++.h>
using namespace std;
long long a[300010];
long long sum[300010];
long long dp[5010][5010];
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]);
  sort(a + 1, a + n + 1);
  for (int i = 2; i <= n; i++) sum[i] = sum[i - 1] + a[i] - a[i - 1];
  if (n % k == 0) {
    int t = n / k;
    long long ans = 0;
    for (int i = 1; i <= n; i += t) ans += sum[i + t - 1] - sum[i];
    printf("%I64d\n", ans);
    return 0;
  }
  int x1 = n % k;
  int y1 = n / k + 1;
  int x2 = k - x1;
  int y2 = n / k;
  dp[0][0] = 0;
  for (int i = 1; i <= x1; i++)
    dp[i][0] = dp[i - 1][0] + sum[i * y1] - sum[(i - 1) * y1 + 1];
  for (int j = 1; j <= x2; j++)
    dp[0][j] = dp[0][j - 1] + sum[j * y2] - sum[(j - 1) * y2 + 1];
  for (int i = 1; i <= x1; i++)
    for (int j = 1; j <= x2; j++)
      dp[i][j] = min(
          dp[i - 1][j] + sum[i * y1 + j * y2] - sum[(i - 1) * y1 + j * y2 + 1],
          dp[i][j - 1] + sum[i * y1 + j * y2] - sum[i * y1 + (j - 1) * y2 + 1]);
  printf("%I64d\n", dp[x1][x2]);
  return 0;
}
