#include <bits/stdc++.h>
using namespace std;
const int max_n = 300333, max_k = 5005;
const long long inf = 1111111111111111111LL;
int n, k, l1, l2, a1, a2, a[max_n];
long long dp[max_k][max_k];
long long get_sum(int l, int r) { return a[r] - a[l]; }
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }
  sort(a, a + n);
  l1 = 1 + (n - 1) / k;
  for (int i = 0; i < k; ++i) {
    int c = (n - i - 1) / k + 1;
    if (c == l1) {
      ++a1;
    } else {
      ++a2;
      l2 = c;
    }
  }
  dp[0][0] = 0;
  for (int i = 0; i <= a1; ++i) {
    for (int j = 0; j <= a2; ++j) {
      if (i == 0 && j == 0) {
        continue;
      }
      int poz = i * l1 + j * l2 - 1;
      dp[i][j] = inf;
      if (i) {
        dp[i][j] = min(dp[i][j], dp[i - 1][j] + get_sum(poz - l1 + 1, poz));
      }
      if (j) {
        dp[i][j] = min(dp[i][j], dp[i][j - 1] + get_sum(poz - l2 + 1, poz));
      }
    }
  }
  cout << dp[a1][a2] << endl;
  return 0;
}
