#include <bits/stdc++.h>
using namespace std;
const long long MXN = 5e3 + 10;
const long long MXM = 3e5 + 10;
long long n, k, t, r;
long long A[MXM], dp[MXN][MXN];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  memset(dp, 63, sizeof dp);
  cin >> n >> k;
  r = n % k;
  for (int i = 1; i <= n; i++) cin >> A[i];
  sort(A + 1, A + n + 1);
  dp[0][0] = 0;
  for (int i = 0; i <= k - r; i++) {
    for (int j = 0; j <= r; j++) {
      if (i + j == 0) continue;
      long long now = (i + j) * (n / k) + j, a = 1e17, b = 1e17;
      if (i) a = dp[i - 1][j] + A[now] - A[now - n / k + 1];
      if (j) b = dp[i][j - 1] + A[now] - A[now - n / k];
      dp[i][j] = min(a, b);
    }
  }
  cout << dp[k - r][r];
  return 0;
}
