您的位置:首页 > 移动开发

CF_462B_ApplemanAndCardGame

2015-08-17 14:56 190 查看
B. Appleman and Card Game

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

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

题意要拿k张卡片

每张卡片可以得到同种卡片总张数的钱

问这个收益的最大值

那么这个问题只需要简单的贪心就可以了

当卡片数目都小于k时优先拿卡片数量最多的即可

注意下数据范围

#include <iostream>
#include <stdio.h>
using namespace std;

const int M=1e5+5;
char s[M];
int ns[26];

int main()
{
int n,k;
long long mo=0;
scanf("%d%d%s",&n,&k,s);
for(int i=0;i<n;i++)
ns[s[i]-'A']++;
while(k)
{
int maxn=0;int p;
for(int i=0;i<26;i++)
{
if(ns[i]>k)
{
//cout<<k;
mo+=(long long)k*k;k=0;
break;
}
if(ns[i]>maxn)
{
p=i;
maxn=ns[i];
}
}
if(k)
{
k-=maxn;
ns[p]=0;
mo+=(long long)maxn*maxn;
}
}
printf("%I64d\n",mo);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: