#include <bits/stdc++.h>
using namespace std;
const int N = 5001;
int n, k, a[N * 60];
long long dp[N][N];
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; i++) scanf("%d", a + i);
  sort(a, a + n);
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) dp[i][j] = 5e18;
  dp[k - n % k][n % k] = 0;
  for (int i = k - n % k; i + 1; i--)
    for (int j = n % k; j + 1; j--) {
      if (i) {
        int last = n / k * (k - n % k - i) + (n / k + 1) * (n % k - j);
        dp[i - 1][j] =
            min(dp[i - 1][j], dp[i][j] + a[last + n / k - 1] - a[last]);
      }
      if (j) {
        int last = n / k * (k - n % k - i) + (n / k + 1) * (n % k - j);
        dp[i][j - 1] = min(dp[i][j - 1], dp[i][j] + a[last + n / k] - a[last]);
      }
    }
  printf("%lld\n", dp[0][0]);
}
