#include <bits/stdc++.h>
using namespace std;
const long long OO = 0x0101010101010101;
class debug_t {
 public:
  template <typename T>
  debug_t& operator<<(const T& o) {
    return *this;
  }
  void end() {
    *this << "\nExecution Time = " << clock() * 1.0 / CLOCKS_PER_SEC << '\n';
    exit(0);
  }
} dout;
int dr[] = {0, 0, -1, 1};
int dc[] = {-1, 1, 0, 0};
long long num1, num2;
long long len1, len2;
long long n, k;
long long pre[300005];
long long dp[5005][5005];
long long solve(long long i, long long cnt1, long long cnt2) {
  if (i >= n) return 0;
  if (cnt1 > num1) return 1e15;
  if (cnt2 > num2) return 1e15;
  long long& ret = dp[cnt1][cnt2];
  if (ret + 1) return ret;
  ret = 0;
  long long solve1 = 1e15, solve2 = 1e15;
  if (i + len1 < n)
    solve1 = pre[i + len1] - pre[i] + solve(i + len1 + 1, cnt1 + 1, cnt2);
  if (i + len2 < n)
    solve2 = pre[i + len2] - pre[i] + solve(i + len2 + 1, cnt1, cnt2 + 1);
  return ret = min(solve1, solve2);
}
int main() {
  ios_base::sync_with_stdio(false);
  memset(dp, -1, sizeof(dp));
  cin >> n >> k;
  for (long long i = 0; i < n; i++) cin >> pre[i];
  sort(pre, pre + n);
  num1 = k - (n % k), num2 = n % k;
  len1 = n / k - 1, len2 = n / k;
  cout << solve(0, 0, 0) << "\n";
  return 0;
}
