#include <bits/stdc++.h>
using namespace std;
const long long N = 3e5 + 10, K = 5000 + 10;
long long n, k, a[N];
long long dp[K][K];
long long solve(long long la, long long lb, long long ra, long long rb) {
  long long l = lb * (n / k + 1) + (la - lb) * (n / k) + 1;
  long long r = rb * (n / k + 1) + (ra - rb) * (n / k);
  return dp[la][lb] + a[r] - a[l];
}
int main() {
  scanf("%lld%lld", &n, &k);
  for (long long i = 1; i <= n; i++) scanf("%lld", &a[i]);
  sort(a + 1, a + n + 1);
  memset(dp, 0x3f, sizeof(dp));
  dp[0][0] = 0;
  for (long long i = 1; i <= k; i++) {
    long long tmp = min(i, n % k);
    for (long long j = 0; j <= tmp; j++) {
      if (j == 0)
        dp[i][j] = solve(i - 1, j, i, j);
      else
        dp[i][j] = min(solve(i - 1, j - 1, i, j), solve(i - 1, j, i, j));
    }
  }
  printf("%lld", dp[k][n % k]);
  return 0;
}
