#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define chmin(x,y) x = min((x),(y));
#define chmax(x,y) x = max((x),(y));
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
const int INF = 1e9;
const long long LINF = 1e17;
const int MOD = 1000000007;
//const int MOD = 998244353;
const double PI = 3.14159265358979323846;

int main(){
    ll n,k;
    cin >> n >> k;
    vector<ll> cnt(n,0);
    rep(i,n){
        ll a;
        cin >> a;
        cnt[a] ++;
    }
    ll p = cnt[0];
    for(ll i=1;i<n;i++){
        cnt[i] = min(cnt[i],p);
        p = cnt[i];
    }
    vector<ll> ans(k,0);
    rep(i,n){
        rep(j,cnt[i]){
            if(j>=k) break;
            ans[j] = i+1;
        }
    }
    ll res = accumulate(ans.begin(),ans.end(),0);
    cout << res << endl;
    return 0;
}
#include<bits/stdc++.h>
#define maxn 300010
#define ll long long
using namespace std;
ll n,m,now,ans,sum;
ll a[maxn],nm[maxn];
struct nod{
	ll pos,num;
};
bool operator < (nod aa,nod bb){
	return aa.num==bb.num?aa.pos>  bb.pos:aa.num>bb.num;
}
priority_queue <nod> q;
int main(){
	scanf("%lld%lld",&n,&m);
	for(ll i=1;i<=n;++i) scanf("%lld",&a[i]),nm[a[i]]++;
	sort(a+1,a+n+1);
	for(ll i=0;i<=n;++i) 
		q.push((nod){i,nm[i]});
	if(a[1]==0) {
	now=1;
	for(ll i=2;i<=n;++i){
	if(a[i]==a[i-1]||a[i]==a[i-1]+1) now=a[i]+1;else break;
	}
}
	while(m&&!q.empty()){
		nod v=q.top();
		q.pop();
		if(v.num==0) continue;
		if(v.pos>now) continue;
		ans+=now*min(m,v.num-sum);
		m-=min(m,v.num-sum);
		sum=v.num;
		now=v.pos;
	}
	cout<<ans;
	return 0;
} 
/*
12 4
0 1 2 3 4 0 1 2 0 1 1 0
*/