B. Appleman and Card GameTime limit per test 1 secondmemory limit per test megabytes
Input
Standard Input
Output
Standard Output
Appleman has n cards. Each card has a 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 should calculate how much Toastman ' s cards has the letter equal to Letter-on i-th, then sum-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 the 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 Test (s)Input
15 10
Dzfdfzdfddddddf
Output
82
Input
6 4
Yjsnpi
Output
4
Note
In the first Test example Toastman can choose nine cards with letter D and one additional card with any letter . For each card with D He'll get 9 coins and for the additional card he'll get 1 coin.
Problem solving: greed.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6#include <climits>7#include <vector>8#include <queue>9#include <cstdlib>Ten#include <string> One#include <Set> A#include <stack> - #defineLL Long Long - #definePII pair<int,int> the #defineINF 0x3f3f3f3f - using namespacestd; - intletter[ -]; - Charstr[100100]; + BOOLcmpConst int&x,Const int&y) { - returnX >y; + } A intMain () { at intn,k; - LL ans; - while(~SCANF ("%d%d",&n,&k)) { -memset (Letter,0,sizeof(letter)); -scanf"%s", str); - for(inti =0; Str[i]; i++) letter[str[i]-'A']++; inSort (letter,letter+ -, CMP); - for(inti = ans =0; K && I < -; i++){ toAns + = 1ll*min (K,letter[i]) *min (k,letter[i]); +K-=min (k,letter[i]); - } thecout<<ans<<Endl; * } $ return 0;Panax Notoginseng}
View Code
Codeforces 263B. Appleman and Card Game