#include <bits/stdc++.h>
using namespace std;
void FastIO() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
}
long long modpow(long long a, long long p, long long mod) {
  long long ret = 1;
  while (p) {
    if (p & 1) ret = (ret * a) % mod;
    a = (a * a) % mod;
    p /= 2;
  }
  return ret;
}
long long power(long long a, long long p) {
  long long ret = 1;
  while (p) {
    if (p & 1) ret = (ret * a);
    a = (a * a);
    p /= 2;
  }
  return ret;
}
int a[300010];
long long dp[5010][5010];
int main() {
  FastIO();
  long long n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) cin >> a[i];
  sort(a, a + n);
  long long x = k - n % k, y = n % k, sz = n / k;
  dp[0][0] = 0;
  for (int i = 0; i < x; i++)
    dp[i + 1][0] = dp[i][0] + a[(i + 1) * sz - 1] - a[i * sz];
  for (int i = 0; i < y; i++)
    dp[0][i + 1] = dp[0][i] + a[(i + 1) * (sz + 1) - 1] - a[i * (sz + 1)];
  for (int i = 0; i < x; i++)
    for (int j = 0; j < y; j++) {
      long long add = a[(i + 1) * sz + (j + 1) * (sz + 1) - 1],
                subs = a[i * sz + (j + 1) * (sz + 1)],
                subl = a[(i + 1) * sz + j * (sz + 1)];
      dp[i + 1][j + 1] =
          min(dp[i][j + 1] + add - subs, dp[i + 1][j] + add - subl);
    }
  cout << dp[x][y] << '\n';
  return 0;
}
