#include<bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long n , k , a [300005] , num [300005] , ans;
void go ( int x )
{
    num [x] --;
    if ( num [ x + 1 ] == 0 )
    {
        ans += x + 1;
        return;
    }
    else go ( x + 1 );
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    cin >> n >> k;
    for ( int i = 0 ; i < n ; i ++ )
    {
        cin >> a [i];
        num [ a [i] ] ++;
    }
    while ( k )
    {
        if ( num [0] == 0 ) break;
        go ( 0 );
        k --;
    }
    cout << ans;
}

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for(int i = (a); i < (b); ++i)
#define per(i, a, b) for(int i = (b)-1; i >= (a); --i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define pb push_back
#define eb emplace_back
#define mp make_pair

template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<pii> vii;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);

  int n, k;
  cin >> n >> k;

  vi cnt(n+1);
  rep(i,0,n) {
    int x;
    cin >> x;
    ++cnt[x];
  }

  int tot = min(cnt[0], k);
  ll ans = 0;
  rep(i,1,n+1) {
    if (tot == 0) break;
    // how many i do we have
    int nxt = min(tot, cnt[i]);
    int rm = tot-nxt;
    ans += ll(i) * ll(rm);
    tot = nxt;
  }
  cout << ans << '\n';
}