///****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";

}

/* B */ 
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

const int N = 3e5 + 10;
int n, m, a[N], s[N];

ll solve(void)
{
	int num = m;
	ll res = 0;
	for (int k = 1; k <= n; ++k) s[a[k]]++;
	for (int k = 0; k <= n; ++k) {
		if (s[k] >= num) continue;
		res += (ll)(num - s[k]) * k;
		num = s[k];
	}
	return res;
}

signed main(int argc, char** argv)
{
	#ifndef ONLINE_JUDGE
		freopen("input.txt", "r", stdin);
	//	freopen("output.txt", "w", stdout);
	#endif
	while (cin >> n >> m) {
		for (int k = 1; k <= n; ++k) cin >> a[k];
		sort(a + 1, a + n + 1);
		cout << solve() << endl;
	}
	return 0;	
} 