#include <bits/stdc++.h>
using namespace std;
long long n, k;
long long a[300010], dp[5010][5010];
int main() {
  for (long long i = 0; i <= 5009; i++)
    for (long long j = 0; j <= 5009; j++)
      dp[i][j] = (long long)1000000000000009;
  dp[0][0] = (long long)0;
  cin >> n >> k;
  for (long long i = 1; i <= n; i++) scanf("%I64d", &a[i]);
  sort(a + 1, a + n + 1);
  long long val = n / k;
  long long ans = n - val * k;
  for (long long i = 0; i <= k - ans; i++)
    for (long long j = 0; j <= ans; j++) {
      long long res = (i + j) * val + j;
      if (i != (long long)0)
        dp[i][j] = min(dp[i][j], dp[i - 1][j] + a[res] - a[res - val + 1]);
      if (j != (long long)0)
        dp[i][j] = min(dp[i][j], dp[i][j - 1] + a[res] - a[res - val]);
    }
  cout << dp[k - ans][ans] << endl;
  return 0;
}
