Appleman and Card Game CodeForces

来源:互联网 发布:火车票买票软件 编辑:程序博客网 时间:2024/05/01 12:51

Appleman and Card Game CodeForces - 462B

题目描述
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 ≤ 10^5). 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.

Example

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 will get 9 coins and for the additional card he will get 1 coin.

大致题意
让你从n个写有一个大写字母的卡片里选出k个卡片,你所选的每个卡片的值为你挑选出的k张卡片中写有相同大写字母的卡片的个数,比如你选了6张AAABBC则每张卡片A的值为3,B为2,C为1。然后问你所能选出的最多卡片总和值为多少。

思路:贪心,先统计每个字母对应的卡片数量,然后从最多的那个开始取。
注意,记得开 long long。
代码如下

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>#include<vector>#include<cmath>#include<string>#define ll long long  #define maxn 100005using namespace std;ll a[26];bool cmp(ll x,ll y){    return x>y;}int main(){    ll s=0;   ll n,k;   char c;   cin>>n>>k;   memset(a,0,sizeof(a));   for(ll i=1;i<=n;i++)   {     cin>>c;     a[c-'A']++;   }   sort(a,a+26,cmp);   for(ll i=0;i<26;i++){   if(a[i]<=k)   {      s+=a[i]*a[i];      k-=a[i];   }   else    {    s+=k*k;    break;   }    }    cout<<s;   return 0;}
0 0