#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<utility>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007; 

struct fast_io {
	fast_io(){
		std::cin.tie(nullptr);
		std::ios::sync_with_stdio(false);
	};
} fio;

signed main(){
	cout<<fixed<<setprecision(10);
	
	int N, K;
    int ans = 0;
    vector<int> a;
    vector<int> con(400000);
	
    cin>>N>>K;
    
    a.resize(N);
	
    for(int i = 0; i < N; i++){
        cin>>a[i];
        con[a[i]]++;
        con[a[i]] = min(con[a[i]], K);
    }
    
    // sort(a.begin(), a.end());
    
    for(int i = 0; i + 1 < con.size(); i++){
        con[i+1] = min(con[i], con[i+1]);
        ans += (con[i] - con[i+1]) * (i+1);
    }
    
    cout<<ans<<endl;
    
	return 0;
}
#include<bits/stdc++.h>
#define maxn 10005
using namespace std;
int n,m; string s[805];
int son[800*13][8],tot=1;
vector<int> has[800*13];
struct mat{
	int v[20][20];
	int score;
	bool vis[800];
}arr[maxn];

int vis[800*13],col;
int dx[4]={0,1},dy[4]={1,0};
void getscore(mat &tt)
{
	memset(tt.vis,0,sizeof(tt.vis));
	tt.score=0;
	col++;
	for (int x=0;x<n;x++)
		for (int y=0;y<n;y++)	
		for (int f=0;f<=1;f++)
		{
			int nx=x,ny=y,now=1,step=0;
			while (step<n)
			{
				if (!son[now][tt.v[nx][ny]]) break;
				now=son[now][tt.v[nx][ny]];
				step++; nx=(nx+dx[f]+n)%n; ny=(ny+dy[f]+n)%n; 
				if (vis[now]!=col)
				{
					vis[now]=col;
					int siz=has[now].size();
					for (int j=0;j<siz;j++)
						tt.vis[has[now][j]]=1;
				}
			}
		}
	for (int i=0;i<m;i++)
	if (tt.vis[i])
		tt.score++;
	return;
}

void ins(int id,string s)
{
	int now=1;
	for (int i=0;i<s.size();i++)
	{
		int tt=s[i]-'A';
		if (!son[now][tt]) son[now][tt]=++tot;
		now=son[now][tt];
	}
	has[now].push_back(id);
	return;
}

void out(mat now)
{
	for (int i=0;i<n;i++)
	{
		for (int j=0;j<n;j++)
			cout<<(char)(now.v[i][j]+'A');
		cout<<endl;
	}
//	cout<<now.score<<endl;
	return;
}

int father=10,sson=5,dai=300000,fir=10000;
int cmp(mat x,mat y) {return x.score>y.score;}
void init()
{
	int tot=0;
	for (int i=1;i<=fir;i++)
	{	
		tot++; arr[tot]=arr[i];
		for (int tt=1;tt<=100;tt++)
		{
			int j=rand()%(m-arr[tot].score)+1,x=rand()%n,y=rand()%n,now=0;
			while (true)
			{
				if (arr[tot].vis[now]==0) j--;
				if (j==0) break;
				now++;
			}
			//构造第now个字符串 
			int f=rand()%2;
			for (j=0;j<s[now].size();j++)
			{
				arr[tot].v[x][y]=s[now][j]-'A';
				x=(x+dx[f]+n)%n; y=(y+dy[f]+n)%n;
			}
		}
		getscore(arr[tot]);
	}
	sort(arr+1,arr+tot+1,cmp);
}

int main()
{
//	freopen("0055.txt","r",stdin); 
	int ti=clock();
	srand(time(0));
	cin>>n>>m;
	for (int i=0;i<m;i++) {cin>>s[i]; ins(i,s[i]);}
	int limit=CLOCKS_PER_SEC*2.9;
//	init();
	
	for (int tim=1;tim<=dai;tim++)
	{
		if (clock()-ti>=limit) break;
//		if (tim%50==0) cout<<tim<<" "<<arr[1].score<<endl;
//		cout<<tim<<" "<<arr[1].score<<endl;
		int tot=father;
		for (int i=1;i<=father;i++)
		{
			int q=sson; 
			q=sson+(100-i*40>=0)?30-i*10:0;
			for (int tt=1;tt<=q;tt++)
			{
				tot++; arr[tot]=arr[i];
				int j=rand()%(m-arr[tot].score)+1,x=rand()%n,y=rand()%n,now=0;
				while (true)
				{
					if (arr[tot].vis[now]==0) j--;
					if (j==0) break;
					now++;
				}
				//构造第now个字符串 
				int f=rand()%2;
				for (j=0;j<s[now].size();j++)
				{
					arr[tot].v[x][y]=s[now][j]-'A';
					x=(x+dx[f]+n)%n; y=(y+dy[f]+n)%n;
				}
				
				getscore(arr[tot]);
				if (arr[tot].score==m) {out(arr[tot]); return 0;}
			}
		}
		sort(arr+1,arr+tot+1,cmp);
	}
	sort(arr+1,arr+tot+1,cmp);
	out(arr[1]);
	return 0;
}