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

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define range(a) a.begin(), a.end()
#define endl "\n"
#define Yes() cout << "Yes" << endl
#define No() cout << "No" << endl
#define MP make_pair
const unsigned long long mod = 1e9 + 7;
const long long INF = 1LL<<60;
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};
void chmin(long long &a, long long b) { if (a > b) a = b; }
void chmax(long long &a, long long b) { if (a < b) a = b; }

int main(void){
	ios::sync_with_stdio(0);
	cin.tie(0);
	ll N, K;
	cin >> N >> K;
	ll A[300010] = {};
	FOR(i,0,N){
		ll b;
		cin >> b;
		if(A[b]<K){
			A[b]++;
		}
	}

	ll now = K;
	ll sum = 0;

	FOR(i,0,N){
		ll c = A[i];
		if(c == 0){
			sum += (now - c) * i;
			break;
		}
		if(now<=c){
			continue;
		}
		sum += (now - c) * i;
		now = min(now, c);
	}

	cout << sum << endl;

	return 0;
}



#include <vector>
#include <stdio.h>
using namespace std;
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0;i<(n);++i)
using ll = long long;
using p = pair<int,int>;

int n;
vector<vector<int>> to;

vector<int> used,col;
vector<int> vs;

void dfs(int v)//引数は今見ている頂点
{
	if(used[v])//頂点に訪れていたならreturn
	{
		return;
	}
	used[v] = 1;//これがないと無限ループになる
	vs.push_back(v);
	for(int u : to[v])//vに隣接する頂点に対して
	{
		dfs(u);
	}
}

ll now;

void dfs2(int i)
{
	if(i == (int)vs.size())
	{
		now++;
		return;
	}
	int v = vs[i];//今見ている頂点
	rep(c,3)//色を0,1,2で表す
	{
		col[v] = c;//vをc色に塗る
		bool ok = true;
		for(int u : to[v])
		{
			if(col[u] == c)
			{
				ok = false;
			}
		}
		if(!ok)
		{
			continue;
		}
		dfs2(i+1);
	}
	col[v] = -1;
}

int main() {
	int m;
	cin >> n >> m;
	to = vector<vector<int>>(n);
	rep(i,m)
	{
		int a,b;
		cin >> a >> b;
		--a;
		--b;
		to[a].push_back(b);
		to[b].push_back(a);
	}
	ll ans = 1;
	used = vector<int>(n); /*dfsの判定に使う*/
	col = vector<int>(n,-1);/*塗り方を試すために使う*/

	rep(i,n)
	{
		if(used[i])
		{
			continue;
		}
		ans *= 3;
		vs = vector<int>();//dfsの順番で頂点を入れていく
		dfs(i);
		col[vs[0]] = 0;
		now = 0;
		dfs2(1);//色塗りを探索
		ans *= now;
	}
	cout << ans << endl;
return 0;
}


