#include <bits/stdc++.h>
using namespace std;
const int K = 5010;
const int N = (int)5e5;
const long long INF = (long long)1e18;
int n, k, a[N];
long long dp[K][K];
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  sort(a, a + n);
  int s0 = n / k;
  int s1 = s0 + 1;
  int q1 = n % k;
  int q0 = k - q1;
  for (int i = 0; i <= q0; i++) {
    for (int j = 0; j <= q1; j++) {
      dp[i][j] = INF;
    }
  }
  dp[0][0] = 0;
  for (int i = 0; i <= q0; i++) {
    for (int j = 0; j <= q1; j++) {
      int u = i * s0 + j * s1;
      if (i < q0) {
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + a[u + s0 - 1] - a[u]);
      }
      if (j < q1) {
        dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + a[u + s1 - 1] - a[u]);
      }
    }
  }
  printf("%I64d\n", dp[q0][q1]);
  return 0;
}
