#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000005;
const int MOD = 1e9 + 9;
int mod_sum(int a, int b) { return ((a + b) % MOD + MOD) % MOD; }
int mod_sub(int a, int b) { return ((a - b) % MOD + MOD) % MOD; }
int mod_mul(int a, int b) { return 1ll * a * b % MOD; }
bool ld_equal(long double a, long double b) { return abs(a - b) < 1e-18; }
bool double_equal(double a, double b) { return abs(a - b) < 1e-6; }
void solve() {
  int n, k;
  scanf("%d %d", &n, &k);
  vector<int> v(n);
  for (int i = 0; i < n; ++i) scanf("%d", &v[i]);
  sort(v.begin(), v.end());
  vector<int> dp(n + 1, -1);
  dp[0] = 0;
  set<pair<int, int>> seta1;
  set<pair<int, int>> group1;
  set<int> seta2;
  for (int i = k; i < n + 1; ++i) {
    if (dp[i - k] != -1) {
      seta1.insert(make_pair(dp[i - k], i - k));
      group1.insert(make_pair(dp[i - k] + v[i - k], i - k));
    }
    while (group1.size() > 0 && v[i - 1] >= group1.begin()->first) {
      seta2.insert(-v[group1.begin()->second]);
      seta1.erase(
          make_pair(dp[group1.begin()->second], group1.begin()->second));
      group1.erase(group1.begin());
    }
    if (seta1.size() > 0) dp[i] = seta1.begin()->first;
    if (dp[i] == -1 || v[i - 1] + *seta2.begin() < dp[i])
      dp[i] = v[i - 1] + *seta2.begin();
  }
  printf("%d\n", dp[n]);
}
int main() {
  solve();
  return 0;
}
