#include <bits/stdc++.h>
using namespace std;
int i, j, k, l, n, m, s, an, K, X;
const int N = 5100;
int a[1100000], g[1100000];
long long f[N][N];
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 v1 = n / K + 1;
  int v2 = n / K;
  if (n % K == 0) v1--;
  int ss = n % K;
  if (!ss) ss = K;
  ss = K - ss;
  memset(f, 120, sizeof(f));
  f[0][0] = 0;
  for (int i = 0; i <= K; i++)
    for (int j = 0; j <= ss; j++) {
      int now = j * v2 + (i - j) * v1;
      f[i + 1][j] = min(f[i + 1][j], f[i][j] + a[now + v1] - a[now + 1]);
      f[i + 1][j + 1] =
          min(f[i + 1][j + 1], f[i][j] + a[now + v2] - a[now + 1]);
    }
  printf("%I64d\n", f[K][ss]);
  return 0;
}
