#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int N, K, p, i = 0, ans = 0;
  cin >> N;
  cin >> K;
  std::shared_ptr<int> ptr(new int[N], std::default_delete<int[]>());
  std::shared_ptr<int> min(new int[K], std::default_delete<int[]>());
  for (i = 0; i < K; i++) {
    min.get()[i] = 0;
  }
  for (i = 0; i < N; i++) {
    cin >> p;
    ptr.get()[i] = p;
    for (int j = 0; j < K; j++) {
      if (min.get()[j] == 0) {
        min.get()[j] = p;
        break;
      } else if (min.get()[j] > p) {
        int tmp = min.get()[j];
        min.get()[j] = p;
        if (j < N - 2) {
          for (int n = j + 1; n < K - 1; n++) {
            min.get()[n + 1] = min.get()[n];
          }
        }
        min.get()[j + 1] = tmp;
        break;
      }
    }
  }
  for (i = 0; i < K; i++) {
    ans += min.get()[i];
  }
  cout << ans;
  return 0;
}
