#include <bits/stdc++.h>
using namespace std;
int n, k, A[300000];
long long done[5001][5001];
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) scanf("%d", &A[i]);
  sort(A, A + n);
  int x = n / k, y = n % k;
  for (int i = 1; i < k + 1; i++)
    for (int j = 0; j < min(y, i) + 1; j++) {
      if (j == 0)
        done[i][0] = done[i - 1][0] + A[x * i - 1] - A[x * (i - 1)];
      else if (i == j)
        done[i][j] =
            done[i - 1][j - 1] + A[x * i + j - 1] - A[x * (i - 1) + j - 1];
      else {
        done[i][j] =
            done[i - 1][j - 1] + A[x * i + j - 1] - A[x * (i - 1) + j - 1];
        done[i][j] = min(
            done[i][j], done[i - 1][j] + A[x * i + j - 1] - A[x * (i - 1) + j]);
      }
    }
  cout << done[k][y];
  return 0;
}
