#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e17;
const int N = 3e5 + 1;
long long dp[5555][5555], A[N];
int main() {
  int n, k, i, j, t;
  scanf("%d %d", &n, &k);
  t = n / k;
  for (i = 1; i <= n; i++) scanf("%I64d", A + i);
  sort(A + 1, A + 1 + n);
  for (i = 0; i <= k; i++)
    for (j = 0; j <= k; j++) dp[i][j] = INF;
  dp[0][0] = 0;
  for (i = 1; i <= k; i++) {
    for (j = 0; j <= i; j++) {
      dp[i][j] = dp[i - 1][j] + abs(A[t * i + j] - A[t * i + j - t + 1]);
      if (j)
        dp[i][j] = min(dp[i][j],
                       dp[i - 1][j - 1] + abs(A[t * i + j] - A[t * i + j - t]));
    }
  }
  cout << dp[k][n % k];
  return 0;
}
