#include <bits/stdc++.h>
using namespace std;
int solve(int s, int b, vector<int> &mas, int k, int q, int **known) {
  if (known[s][b] != 100000009) {
    return known[s][b];
  }
  int res1, res2;
  if (s > 0) {
    if (known[s - 1][b] != 100000009) {
      res1 = known[s - 1][b];
    } else {
      res1 = solve(s - 1, b, mas, k, q, known);
      known[s - 1][b] = res1;
    }
  } else {
    res2 = solve(s, b - 1, mas, k, q, known);
    int ind2 = s * q + (b - 1) * (q + 1);
    return (res2 + mas[ind2 + (q)] - mas[ind2]);
  }
  if (b > 0) {
    if (known[s][b - 1] != 100000009) {
      res2 = known[s][b - 1];
    } else {
      res2 = solve(s, b - 1, mas, k, q, known);
      known[s][b - 1] = res2;
    }
  } else {
    res1 = solve(s - 1, b, mas, k, q, known);
    int ind1 = (s - 1) * q + b * (q + 1);
    return (res1 + mas[ind1 + (q - 1)] - mas[ind1]);
  }
  int ind1 = (s - 1) * q + b * (q + 1);
  int ind2 = s * q + (b - 1) * (q + 1);
  if ((res1 + mas[ind1 + (q - 1)] - mas[ind1]) <=
      (res2 + mas[ind2 + (q)] - mas[ind2])) {
    return (res1 + mas[ind1 + (q - 1)] - mas[ind1]);
  } else {
    return (res2 + mas[ind2 + (q)] - mas[ind2]);
  }
}
vector<int> abc(int n) {
  vector<int> a(n, 1);
  return a;
}
void printing(vector<int> a) {
  for (int i = 0; i < a.size(); i++) cout << a[i] << " ";
}
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> mas(n);
  for (int i = 0; i < n; i++) {
    cin >> mas[i];
  }
  sort(mas.begin(), mas.end());
  int q = n / k;
  int razn = 0;
  int **known = new int *[k - (n % k) + 1];
  for (int i = 0; i < k - (n % k) + 1; i++) {
    known[i] = new int[(n % k) + 1];
    for (int j = 0; j < (n % k) + 1; j++) {
      known[i][j] = 100000009;
    }
  }
  known[0][0] = 0;
  razn = solve(k - (n % k), (n % k), mas, k, q, known);
  cout << razn;
  for (int i = 0; i < k - (n % k) + 1; i++) {
    free(known[i]);
  }
  return 0;
}
