#include <bits/stdc++.h>
using namespace std;
const int INF = 2e9;
const int MAX_n = 3e5 + 10, MAX_k = 5010;
int n, k;
int a[MAX_n];
int dp[MAX_k][MAX_k];
int abs(int x) { return max(x, -x); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n; ++i) cin >> a[i];
  sort(a, a + n);
  int div = n / k, mod = n % k;
  for (int i = 0; i <= mod; ++i) {
    for (int j = 0; j <= k - mod; ++j) {
      if (i + j == 0) continue;
      int l = i * (div + 1) + j * (div);
      dp[i][j] = INF;
      if (i) {
        dp[i][j] = min(dp[i][j], dp[i - 1][j] + a[l - 1] - a[l - div - 1]);
      }
      if (j) {
        dp[i][j] = min(dp[i][j], dp[i][j - 1] + a[l - 1] - a[l - n / k]);
      }
    }
  }
  cout << dp[n % k][k - n % k] << endl;
  return 0;
}
