#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300010;
const int MAXK = 5010;
const long long INF = 1000ll * 1000 * 1000 * 1000 * 1000 * 1000;
int a[MAXN];
long long dp[2][MAXK];
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) cin >> a[i];
  sort(a, a + n);
  int len = n / k;
  for (int i = 1; i <= k; i++)
    for (int j = 0; j <= i; j++) {
      int cur = i * len + j;
      if (cur > n) continue;
      dp[i % 2][j] = INF;
      if (j <= i - 1)
        dp[i % 2][j] =
            min(dp[i % 2][j], dp[(i - 1) % 2][j] + a[cur - 1] - a[cur - len]);
      if (j > 0)
        dp[i % 2][j] = min(dp[i % 2][j], dp[(i - 1) % 2][j - 1] + a[cur - 1] -
                                             a[cur - len - 1]);
    }
  cout << dp[k % 2][n % k] << endl;
}
