#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 100;
int solve(int n, int m, int len, int num, const vector<int>& v) {
  if (m >= n) return 0;
  map<pair<int, int>, int> f[N];
  f[0][pair<int, int>(num, m - num)] = 0;
  for (int i = 0; i < (n + 1); ++i) {
    for (auto top : f[i]) {
      int num0 = top.first.first, num1 = top.first.second;
      if (num0 && i + len <= n) {
        int j = i + len;
        pair<int, int> k = pair<int, int>(num0 - 1, num1);
        if (!f[j].count(k)) f[j][k] = 2e9;
        f[j][k] = min(f[j][k], top.second + v[j - 1] - v[i]);
      }
      if (num1 && i + len - 1 <= n) {
        int j = i + len - 1;
        pair<int, int> k = pair<int, int>(num0, num1 - 1);
        if (!f[j].count(k)) f[j][k] = 2e9;
        f[j][k] = min(f[j][k], top.second + v[j - 1] - v[i]);
      }
    }
  }
  return f[n][pair<int, int>(0, 0)];
}
int main() {
  int n, m;
  while (cin >> n >> m) {
    vector<int> vec(n);
    for (int i = 0; i < (n); ++i) scanf("%d", &vec[i]);
    sort(vec.begin(), vec.end());
    vec.push_back(vec.back());
    int len = (n - 1) / m + 1, num = (n - 1) % m + 1;
    int ret = solve(n, m, len, num, vec);
    cout << ret << endl;
  }
  return 0;
}
