CodeForces462B
Description
Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman's card i you should calculate how much Toastman's cards have the letter equal to letter on ith, then sum up all these quantities, such a number of coins Appleman should give to Toastman.
Given the description of Appleman's cards. What is the maximum number of coins Toastman can get?
Input
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n uppercase letters without spaces — the i-th letter describes the i-th card of the Appleman.
Output
Print a single integer – the answer to the problem.
Sample Input
Input
15 10
DZFDFZDFDDDDDDF
Output
82
Input
6 4
YJSNPI
Output
4
1 //2016.8.2 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 6 using namespace std; 7 8 int ch[30]; 9 10 bool cmp(int a, int b) 11 { 12 return a>b; 13 } 14 15 int min(int a, int b) 16 { 17 return a > b ? b : a; 18 } 19 20 int main() 21 { 22 int n, k; 23 long long ans, x; 24 char c; 25 while(cin >> n >> k) 26 { 27 memset(ch, 0, sizeof(ch)); 28 getchar(); 29 ans = 0; 30 for(int i = 0; i < n; i++) 31 { 32 c = getchar(); 33 ch[c-'A']++; 34 } 35 sort(ch, ch+26, cmp); 36 37 for(int i = 0; k > 0;i++) 38 { 39 x = min(k, ch[i]); 40 ans += x*x; 41 k -= x; 42 } 43 cout << ans << endl; 44 } 45 return 0; 46 }