#include <bits/stdc++.h>
using namespace std;
const int N = 600001;
int n, k;
int a[N];
int dp[5001][5001];
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  sort(a, a + n);
  int sz = n / k;
  int B = n % k;
  int A = k - B;
  for (int i = 0; i <= A; i++)
    for (int j = 0; j <= B; j++) dp[i][j] = numeric_limits<int>::max();
  dp[0][0] = 0;
  for (int i = 0; i <= A; i++)
    for (int j = 0; j <= B; j++) {
      int t = dp[i][j];
      int x = (i + j) * sz + j;
      dp[i + 1][j] = min(dp[i + 1][j], t + a[x + sz - 1] - a[x]);
      dp[i][j + 1] = min(dp[i][j + 1], t + a[x + sz] - a[x]);
    }
  cout << dp[A][B];
}
