#include <bits/stdc++.h>
using namespace std;
const double pi =
    3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342;
int A[300010];
long long dp[2][5010][2];
int main() {
  long long res = 0;
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 0; i < (n); ++i) {
    scanf("%d", &A[i]);
    res += abs(A[i]);
  }
  sort(A, A + n);
  int dl = n / k;
  if (!(n % k)) {
    res = 0;
    int p = 0;
    for (int i = 0; i < (k); ++i) {
      res += A[p + dl - 1] - A[p];
      p += dl;
    }
    printf("%lld\n", res);
    return 0;
  }
  for (int i = 0; i < (2); ++i)
    for (int j = 0; j < (5010); ++j)
      for (int z = 0; z < (2); ++z) dp[i][j][z] = res;
  int ile = n % k;
  dp[0][0][0] = A[dl - 1] - A[0];
  dp[0][0][1] = A[dl] - A[0];
  for (int i = (1); i < (k); ++i) {
    int wsk = i & 1;
    int p = i * dl;
    int v = A[p + dl - 1] - A[p];
    dp[wsk][0][0] = v + dp[!wsk][0][0];
    v = A[p + dl] - A[p];
    dp[wsk][0][1] = v + dp[!wsk][0][0];
    for (int j = (1); j < (min(ile, i + 1)); ++j) {
      p = j * (dl + 1) + (i - j) * dl;
      v = A[p + dl - 1] - A[p];
      dp[wsk][j][0] = v + min(dp[!wsk][j][0], dp[!wsk][j - 1][1]);
      v = A[p + dl] - A[p];
      dp[wsk][j][1] = v + min(dp[!wsk][j][0], dp[!wsk][j - 1][1]);
    }
    if (i >= ile) {
      p = ile * (dl + 1) + (i - ile) * dl;
      v = A[p + dl - 1] - A[p];
      dp[wsk][ile][0] = v + min(dp[!wsk][ile][0], dp[!wsk][ile - 1][1]);
    }
  }
  int wsk = (k - 1) & 1;
  printf("%lld\n", min(dp[wsk][ile][0], dp[wsk][ile - 1][1]));
  return 0;
}
