#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ld = long double;
const int mod = 1e9 + 7;
const ll inf = 1e9;
const ll INF = 2e18;
const int N = 2e6 + 66;
const int M = 5005;
ll a[N], dp[5005][5005];
void solve() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  sort(a, a + n);
  for (int i = 0; i <= k; ++i) {
    for (int j = 0; j <= k; ++j) {
      dp[i][j] = INF;
    }
  }
  dp[0][0] = 0;
  for (int i = 0; i <= k; ++i) {
    for (int j = 0; j <= n % k; ++j) {
      if (dp[i][j] == INF) continue;
      int pos = i * (n / k) + j;
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + a[pos + n / k - 1] - a[pos]);
      dp[i + 1][j + 1] =
          min(dp[i + 1][j + 1], dp[i][j] + a[pos + n / k] - a[pos]);
    }
  }
  cout << dp[k][n % k];
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  ;
  int tt = 1;
  while (tt--) {
    solve();
    cout << "\n";
  }
}
