#include <bits/stdc++.h>
using namespace std;
vector<long long> arr;
long long memo[5123][5123];
int n, k, large, small, total_large, total_small;
const long long INF = (int)1e18;
long long dp(int num_large, int num_small) {
  if (memo[num_large][num_small] != -1) return memo[num_large][num_small];
  if (num_large == total_large && num_small == total_small) return 0;
  int id = (num_large * large) + (num_small * small);
  long long take_large = INF;
  if (num_large < total_large)
    take_large = arr[id + large - 1] - arr[id] + dp(num_large + 1, num_small);
  long long take_small = INF;
  if (num_small < total_small)
    take_small = arr[id + small - 1] - arr[id] + dp(num_large, num_small + 1);
  return memo[num_large][num_small] = min(take_large, take_small);
}
int main() {
  cin >> n >> k;
  arr.resize(n);
  for (int i = 0; i < n; i++) cin >> arr[i];
  sort(arr.begin(), arr.end());
  memset(memo, -1, sizeof memo);
  total_large = n % k;
  total_small = k - total_large;
  large = n / k + 1;
  small = n / k;
  cout << dp(0, 0) << endl;
  return 0;
}
