#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
const long long INF = 1e18;
const int MOD = 1e9 + 7;
const double eps = 1e-4;
int n, k;
long long dp[5005][5005], a[N];
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]);
  sort(a + 1, a + 1 + 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 - n % k); i++)
    for (int j = 0; j <= n % k; j++)
      if (i || j) {
        int num = i * (n / k) + j * (n / k + 1);
        if (i)
          dp[i][j] = min(dp[i][j], dp[i - 1][j] + a[num] - a[num - n / k + 1]);
        if (j) dp[i][j] = min(dp[i][j], dp[i][j - 1] + a[num] - a[num - n / k]);
      }
  cout << dp[k - (n % k)][n % k];
  return 0;
}
