#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k, p;
  priority_queue<int> pq;
  scanf("%d%d", &n, &k);
  for (int i = 0; i < k; i++) {
    scanf("%d", &p);
    pq.push(-p);
  }
  for (int i = k; i < n; i++) {
    scanf("%d", &p);
    int v = pq.top();
    if (-v > p) {
      pq.pop();
      pq.push(-p);
    }
  }
  int s = 0;
  while (!pq.empty()) {
    s += pq.top();
    pq.pop();
  }
  printf("%d", s);
  return 0;
}
