#include <bits/stdc++.h>
using namespace std;
const long long N = 5e3 + 5, mod = 1e9 + 7, mod1 = 998244353, mod2 = 1e9 + 9,
                inf = 1e18 + 7;
const long long infll = 1e18 + 7;
long long n, k, X, Y, Z;
long long a[(long long)3e5 + 5];
long long dp[N][N];
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  Y = n % k;
  X = k - Y;
  Z = n / k;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
  }
  sort(a + 1, a + 1 + n);
  for (long long i = 0; i <= X; i++) {
    for (long long j = 0; j <= Y; j++) {
      if (i == 0 && j == 0) {
        dp[i][j] = 0;
        continue;
      }
      dp[i][j] = inf;
      long long t = i * Z + j * (Z + 1);
      if (i) {
        dp[i][j] = min(dp[i][j], dp[i - 1][j] + a[t] - a[t - Z + 1]);
      }
      if (j) {
        dp[i][j] = min(dp[i][j], dp[i][j - 1] + a[t] - a[t - Z]);
      }
    }
  }
  cout << dp[X][Y];
}
