#include <bits/stdc++.h>
using namespace std;
const long double sqr5 = sqrt(5), p_ = (1 + sqr5) / 2;
const long double PI = ((long double)2 * asin(1));
const long long INF = ((long long)1e12);
const long long MOD = ((long long)1e9 + 7);
const long long BIG_HSH = ((long long)9999999900000001);
const long long SML_HSH = ((long long)100030001);
inline long long tavan(long long a, long long b) {
  return (b) ? (tavan((a) * (a), (b) >> 1) * ((b)&1 ? (a) : 1)) : 1;
}
inline long long tavan(long long a, long long n, long long mod) {
  long long p = 1;
  while (n > 0) {
    if (n % 2) {
      p = p * a;
      p %= mod;
    }
    n >>= 1;
    a *= a;
    a %= mod;
  }
  return p % mod;
}
inline long double fib(long long x) {
  return (tavan(p_, x) - tavan(1 - p_, x)) / (sqr5);
}
inline long double fib(long long x, long long mod) {
  return (tavan(p_, x, mod) - tavan(1 - p_, x, mod)) / (sqr5);
}
inline long long gcd(long long a, long long b) {
  while (b ^= a ^= b ^= a = a % b)
    ;
  return a;
}
template <class T>
inline void smx(T &x, T y) {
  x = max((x), (y));
}
template <class T>
inline void smn(T &x, T y) {
  x = min((x), (y));
}
ifstream fin("input.txt");
ofstream fout("output.txt");
long long n, k, a[301 * 1000], dp[(5500)][(5500)];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  for (long long i = 0; i < n; i++) cin >> a[i];
  for (long long i = 0; i < (5500); i++)
    for (long long j = 0; j < (5500); j++) dp[i][j] = INF;
  dp[0][0] = 0;
  sort(a, a + n);
  long long t = n / k, q = n % k;
  for (long long i = 1; i <= k; i++)
    for (long long j = 0; j <= min(q, i); j++) {
      long long x = i * t + j - 1;
      if (x >= t - 1) smn(dp[i][j], dp[i - 1][j] + a[x] - a[x - t + 1]);
      if (x >= t) smn(dp[i][j], dp[i - 1][j - 1] + a[x] - a[x - t]);
    }
  cout << dp[k][q];
  return 0;
}
