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

#define sync ios_base::sync_with_stdio(0);cin.tie(0);
#define all(x) x.begin(),x.end()
#define unq(a) sort(all(a));a.resize(unique(all(a)) - a.begin())
#define ll long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define mem(a , x) memset(a, x, sizeof a)
#define endl '\n'
const ld pi = atan(1) * 4.0;

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


int main(){

    #ifndef ONLINE_JUDGE 
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    sync

    int t = 1;
    //cin >> t;
    while(t--){
       int n, k; cin >> n >> k;
       vector<int> a(n);
       for (int& x : a) cin >> x;
       vector<ll> f(*max_element(all(a)) + 2);
       for (int& x: a) f[x]++;
       ll cnt = 0, lst = -1, mx = *max_element(all(a)), ok = k;
       for (int i = 0;i <= mx + 1;i++){
        if (f[i]){
            int mn = min(ok , f[i]);
            cnt += ((ok - mn) * 1ll * i);
            ok = mn;
        }
        else{
            cnt += (i * 1ll * ok);
            break;
        }
       }
       cout << cnt << endl;
    }

    cerr << "processor time: " << clock() / (double) CLOCKS_PER_SEC << "s    ";
    
    return 0;
}
#include<bits/stdc++.h>
#include <iostream>

using namespace std;

int main()
{
    #ifdef LOCAL_EXEC
    #else
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #endif // LOCAL_EXEC
    long long int n,i,j,k,x=0,y,z,sum=0;
    cin>>n>>k;
    long long int cnt[n+1]={0};
    for(i=1;i<=n;i++){
        cin>>x;
        cnt[x]+=1;
    }
    y=n;
    for(i=1;i<=k;i++){
        for(j=0;j<=n;j++){
            if(cnt[j]==0||y==0)
                break;
            cnt[j]-=1;
            y-=1;
        }
        sum+=j;
        if(y==0)
            break;
    }
    cout<<sum;
    return 0;
}
