#include <bits/stdc++.h>
using namespace std;
long long dp[5005][5005];
long long arr[300005];
long long dif_p[300005];
int main() {
  int N, K;
  cin >> N >> K;
  int chains = N / K;
  int r = N % K;
  for (int i = 0; i < N; i++) {
    cin >> arr[i];
  }
  sort(arr, arr + N);
  for (int i = 1; i < N; i++) {
    dif_p[i] = dif_p[i - 1] + abs(arr[i] - arr[i - 1]);
  }
  for (int j = 0; j <= r; ++j) {
    dp[0][j] = 4294967295;
  }
  dp[0][0] = 0;
  for (int i = 1; i <= K; ++i) {
    for (int j = 0; j <= r; ++j) {
      if (j > i) {
        dp[i][j] = 4294967295;
        continue;
      }
      int tmp = chains * i + j;
      int tmp2 = chains * (i - 1) + j;
      if (j == 0) {
        dp[i][j] = dp[i - 1][j] + dif_p[tmp - 1] - dif_p[tmp2];
      } else {
        dp[i][j] = min(dp[i - 1][j] + dif_p[tmp - 1] - dif_p[tmp2],
                       dp[i - 1][j - 1] + dif_p[tmp - 1] - dif_p[tmp2 - 1]);
      }
    }
  }
  cout << dp[K][r] << endl;
}
