#include <bits/stdc++.h>
using namespace std;
const int MAX = 3e6 + 5;
const long long INF = 1e17;
int n, k, a[MAX], c[MAX];
long long dp[5500][5500];
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  sort(a + 1, a + n + 1);
  int mn = INT_MAX, mx = INT_MIN;
  for (int i = 1; i <= k; i++) {
    int x = i, sz = 0;
    do {
      sz++;
      x += k;
    } while (x <= n);
    mn = min(mn, sz);
    mx = max(mx, sz);
    c[sz]++;
  }
  for (int i = 0; i <= k + 3; i++)
    for (int j = 0; j <= k + 3; j++) dp[i][j] = INF;
  dp[c[mn]][c[mx]] = 0;
  for (int c1 = c[mn]; c1 >= 0; c1--) {
    for (int c2 = c[mx]; c2 >= 0; c2--) {
      int i = c1 * mn + c2 * mx + 1;
      int j = i + mn - 1;
      dp[c1][c2] = min(dp[c1][c2], dp[c1 + 1][c2] + a[j] - a[i]);
      j = i + mx - 1;
      dp[c1][c2] = min(dp[c1][c2], dp[c1][c2 + 1] + a[j] - a[i]);
    }
  }
  printf("%lld\n", dp[0][0]);
  return 0;
}
