#include <bits/stdc++.h>
int main() {
  int N, K, p[1000], t, to, no;
  int i;
  for (i = 0; i < 1000; i++) {
    p[i] = 0;
  }
  scanf("%d %d\n", &N, &K);
  for (i = 0; i < N - 1; i++) {
    scanf("%d ", &t);
    p[t - 1]++;
  }
  scanf("%d/n", &t);
  p[t - 1]++;
  for (i = 0, to = 0, no = 0; i < 1000; i++) {
    if (p[i] > 0) {
      if (no + p[i] <= K) {
        no = no + p[i];
        to = to + p[i] * (i + 1);
      } else {
        while (no + 1 < K) {
          no = no + 1;
          to = to + (i + 1);
        }
      }
    }
  }
  printf("%d\n", to);
  return 0;
}
