#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
const int INF = 1e9;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  vector<long long> a(n);
  for (int i = (0); i < (n); ++i) cin >> a[i];
  sort((a).begin(), (a).end());
  int NL = n % k;
  int NS = k - NL;
  int SL = n / k + 1;
  int SS = n / k;
  vector<vector<long long>> dp(NL + 1, vector<long long>(NS + 1, LLONG_MAX));
  dp[0][0] = 0;
  for (int i = (0); i < (NL + 1); ++i) {
    for (int j = (0); j < (NS + 1); ++j) {
      if (i > 0) {
        int low = (i - 1) * SL + j * SS;
        int high = low + SL - 1;
        long long diff = a[high] - a[low];
        dp[i][j] = min(dp[i][j], dp[i - 1][j] + diff);
      }
      if (j > 0) {
        int low = i * SL + (j - 1) * SS;
        int high = low + SS - 1;
        long long diff = a[high] - a[low];
        dp[i][j] = min(dp[i][j], dp[i][j - 1] + diff);
      }
    }
  }
  cout << dp[NL][NS] << endl;
  return 0;
}
