#include<bits/stdc++.h>
#include<chrono>
using namespace std;
using namespace chrono;
typedef long long ll;
typedef long long int lli;

#define arrsize 100001
#define dpsize 1001
#define vpp vector<PP>
#define vll vector<ll>
#define vcc vector<char>
#define endl "\n"
#define vbb vector<bool>
#define w(t) while(t--)
#define PP pair<ll,ll>
#define test(x) ll t; cin>>t; w(t) x()
#define __lb lower_bound
#define __ub upper_bound
#define szs(x) x.length()
#define szv(x) x.size()
#define INF 1999999996000000010
#define takeINP(arr,n) for(long long i=0;i<n;i++) cin>>arr[i];
#define f(i,s,e) for(long long i=s;i<e;i++)
#define ef(i,s,e) for(long long i=s;i<=e;i++)
#define rf(i,e,s) for(long long i=e-1;i>=s;i--)
#define mem(arr) memset(arr,-1,sizeof(arr));
#define rsz(x,n) x.resize(n)
#define rsr(x,n) x.reserve(n) 
#define float long long double
#define pb push_back
#define print(arr,s,e) f(i,s,e) cout<<arr[i]<<" "; cout<<endl;
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define vll vector<ll>
#define triplet pair<ll,pair<ll,ll> >
#define MITR(a,b) map<a,b>::reverse_iterator 

lli M=1000000007;
lli mod(lli a) { return ((a%M+M)%M);} 
lli add(lli a,lli b) { return mod(mod(a)+mod(b));}
lli multiply(lli a,lli b) { return mod(mod(a)*mod(b));}
lli min(lli a,lli b) { if(a<b) return a; return b;}
lli max(lli a,lli b) { return (a>b)?a:b;}
lli modexpo(lli a,lli b){
    lli res = 1;
    while(b>0){
    	if(b&1) res = (res*a)%M;
    	a = (a*a)%M;
    	b/=2;
    }
    return res;
}
lli mulmod(lli a, lli b, lli mod) 
{ 
    lli res = 0; // Initialize result 
    a = a % mod; 
    while (b > 0) 
    { 
        // If b is odd, add 'a' to result 
        if (b % 2 == 1) 
            res = (res + a) % mod; 
  
        // Multiply 'a' with 2 
        a = (a * 2) % mod; 
  
        // Divide b by 2 
        b /= 2; 
    } 
  
    // Return result 
    return res % mod; 
} 

lli binPower(lli a , lli n , lli mod)
{
	lli res = 1;
 
	while(n)
	{
		if(n & 1)
		res = mulmod(res , a , mod);
 
		n >>= 1;
		a = mulmod(a , a , mod);
	}
 
	return res % mod;
}
class Line{
  public:
  double slope;
  double c;
  Line(double slope=0,double c=0){
    this->slope=slope;
    this->c=c;
  }
};
int divisor[1000005];

void solve(){
    ll n,k;
    cin>>n>>k;
    vll arr(n);
    map<ll,ll> map;
    ll maxe=0;
    f(i,0,n) cin>>arr[i],map[arr[i]]++,maxe=max(maxe,arr[i]);
    sort(all(arr));
    ll ans=0;
    ll ls=0;
    for(ll i=0;i<maxe+1;i++){
        // cout<<i<<":"<<map[i]<<" ";
        if(map[i]<k){
            ans+=(k-map[i])*i;
            // cout<<e.first<<" ";
            k=map[i];
        }
        ls=i+1;
    }
    if(k>0){
        ans+=k*ls;
    }
    cout<<ans<<"\n";
    
}
int main(){
    // freopen("input.txt","r",stdin);
    // freopen("output.txt","w",stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    solve();
    // sieve();
    // int t;
    // cin>>t;
    // while(t--) solve();
    return 0;
}
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>

#include <queue>

using namespace std;

typedef pair<int,int> ii;

int fr[300010];

int main(){
	
	cin.tie(0); ios_base::sync_with_stdio(0);
	int n,k;
	
	cin>>n>>k;
	
	long long res=0;
	int a;
	for(int i=0;i<n;i++){
		cin>>a;
		fr[a]++;
	}
	
	int it=0;
	while(k){
		k = min(k,fr[it]);
		it++;
		res+=k;
	}
	cout<<res<<endl;
	
	return 0;
}