Problem Link :http://codeforces.com/problemset/problem/462/B
#include<iostream>
#include<algorithm>
using namespace std;
char s[111111];
__int64 p,n,k,i,m[27];
int main()
{
int flag;
while(scanf("%d%d",&n,&k)!=EOF)
{
scanf("%s",s);
memset(m,0,sizeof(m));
for(i=0;i<n;i++)
m[s[i]-'A']++;
sort(m,m+26);
flag=1;
p=0;
while(flag)
{
for(i=25;i>=0;i--)
{
k-=m[i];
if(k<=0)
{
p+=((k+m[i])*(k+m[i]));
flag=0;
break;
}
else
{
p+=(m[i]*m[i]);
}
}
}
printf("%I64d\n",p);
}
return 0;
}