#include <bits/stdc++.h>
using namespace std;
const int max_n = 300 * 1000, max_k = 5000;
int a[max_n];
long long sum[max_n];
long long dp[max_k + 1][max_k + 1];
const long long inf = 1e16;
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);
  sum[0] = 0;
  for (int i = (1); i < (n); i++) sum[i] = abs(a[i] - a[i - 1]) + sum[i - 1];
  const int x = n / k;
  const int a = n % k, b = k - n % k;
  if (x == 0) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 0; i < (a + 1); i++)
    for (int j = 0; j < (b + 1); j++) dp[i][j] = inf;
  dp[0][0] = 0;
  for (int i = 0; i < (a + 1); i++)
    for (int j = 0; j < (b + 1); j++)
      if (dp[i][j] < inf) {
        int cur = i * (x + 1) + j * x;
        if (i < a)
          dp[i + 1][j] = min(sum[cur + x] - sum[cur] + dp[i][j], dp[i + 1][j]);
        if (j < b)
          dp[i][j + 1] =
              min(sum[cur + x - 1] - sum[cur] + dp[i][j], dp[i][j + 1]);
      }
  cout << dp[a][b] << endl;
  return 0;
}
