#include <bits/stdc++.h>
using namespace std;
const long double PI = 3.14159265359;
const long long MOD = (long long)998244353ll;
const long long MAXN = (long long)3e5 + 10;
const long long INF = (long long)1e17;
const long double EPS = (long double)1e-8;
long long a[MAXN], dp[5100][5100];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  for (int i = 0; i < 5100; i++) fill(dp[i], dp[i] + 5100, INF);
  long long n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) cin >> a[i];
  sort(a, a + n);
  long long re = n % k;
  long long si = n / k;
  dp[0][0] = 0;
  for (int i = 1; i <= k; i++) {
    dp[i][0] = dp[i - 1][0] + a[i * si - 1] - a[(i - 1) * si];
    for (int j = 1; j <= re; j++) {
      dp[i][j] =
          min(dp[i - 1][j] + a[i * si - 1 + j] - a[(i - 1) * si + j],
              dp[i - 1][j - 1] + a[i * si - 1 + j] - a[(i - 1) * si + j - 1]);
    }
  }
  cout << dp[k][re] << endl;
  return 0;
}
