#include <bits/stdc++.h>
using namespace std;
const int N = 6 * 1e5 + 5;
int a[N];
long long dp[5005][5005];
int main() {
  std::ios::sync_with_stdio(false);
  memset(dp, 120, sizeof(dp));
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; ++i) {
    scanf("%d", a + i);
  }
  sort(a, a + n);
  int x = n % k;
  int y = k - x;
  int z = n / k;
  dp[0][0] = 0;
  for (int i = 0; i <= x; ++i) {
    for (int j = 0; j <= y; ++j) {
      int k = z * (i + j) + i;
      dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + abs(a[k + z - 1] - a[k]));
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + abs(a[k + z] - a[k]));
    }
  }
  cout << dp[x][y] << endl;
  return 0;
}
