
#include<bits/stdc++.h>
using namespace std;
int gcd(int a, int b)
{
    if (a < b) {
        a ^= b;
        b ^= a;
        a ^= b;
    }

    return b ? gcd(b, a % b) : a;
}

int lcm(int a, int b)
{
    return a * b / gcd(a, b);
}
int main(){

long long n,k;
cin>>n>>k;
long long a[n];
long long count[n+1]={0};
for(long long i=0;i<n;i++){
  cin>>a[i];
  count[a[i]]++;
}
long long ans=0;
long long count2[n+1]={0};
count2[0]=min(k,count[0]);
for(long long i=1;i<n;i++){
count2[i]=min(count2[i-1],count[i]);
count2[i]=min(k,count2[i]);
}
long long count3=0;
long long count4=0;
for(long long i=n-1;i>=0;i--){

  if(count3<=k){
    if(count3<count2[i]){
    if(count3+count2[i]-count3<=k){

    ans+=(i+1)*(count2[i]-count3);
    count3+=count2[i]-count3;
  }
  else{
    ans+=(k-count3)*(i+1);
    break;
  }
}
}
else{
  break;
}
}
cout<<ans<<endl;
  return 0;
}
///****In the name of ALLAH, most Gracious, most Compassionate****//

#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

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

typedef long long ll;
typedef pair <int, int> pii;

#define ALL(a) a.begin(), a.end()
#define FastIO ios::sync_with_stdio(false); cin.tie(0);cout.tie(0)
#define IN freopen("input.txt","r+",stdin)
#define OUT freopen("output.txt","w+",stdout)

#define DBG(a) cerr<< "line "<<__LINE__ <<" : "<< #a <<" --> "<<(a)<<endl
#define NL cout<<endl

template < class T1,class T2>
ostream &operator <<(ostream &os,const pair < T1,T2 > &p)
{
    os<<"{"<<p.first<<","<<p.second<<"}";
    return os;
}

const int N=3e5+5;
const int oo=1e9+7;
int a[N];
int32_t main()
{
    FastIO;
    int n,k;
    cin>>n>>k;

    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort(a,a+n);
    priority_queue < pair < int , int > > pq;
    for(int i=0;i<k;i++)
        pq.push({0,i});

    ll ans = 0;
    for(int i=0;i<n and ! pq.empty();i++)
    {
        while(!pq.empty())
        {
            auto x = pq.top();
            pq.pop();

            int now = - x.first ;

//            DBG(now);
//            DBG(a[i]);
//            NL;

            if(a[i] < now)
            {
                pq.push(x);
                break;
            }
            else if(a[i] == now)
            {
                pq.push({ - (now +1) , x.second});
                break;
            }
            else
            {
                ans+=now;
                continue ;
            }
        }
    }
    while(!pq.empty())
    {
        ans += -pq.top().first;
        pq.pop();
    }
    cout<<ans<<"\n";

}
