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.

解题:贪心。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
int letter[];
char str[];
bool cmp(const int &x,const int &y){
return x > y;
}
int main() {
int n,k;
LL ans;
while(~scanf("%d %d",&n,&k)){
memset(letter,,sizeof(letter));
scanf("%s",str);
for(int i = ; str[i]; i++) letter[str[i]-'A']++;
sort(letter,letter+,cmp);
for(int i = ans = ; k && i < ; i++){
ans += 1LL*min(k,letter[i])*min(k,letter[i]);
k -= min(k,letter[i]);
}
cout<<ans<<endl;
}
return ;
}

最新文章

  1. Excel Sheet Column Title
  2. Spring(三)AOP面向切面编程
  3. 最详细的hadoop2.2.0集群的HA高可靠的最简单配置
  4. hdwiki 学习笔记 01
  5. Android SDK生成时,自定义文件名称,而非系统第一分配的app-release.apk
  6. &quot;Your local changes to the following files would be overwritten by merge&quot; on git
  7. Can brain stimulation aid memory and brain health?
  8. Golang学习 - fmt 包
  9. 9、JPA_映射双向一对一的关联关系
  10. 12天学好C语言——记录我的C语言学习之路(Day 8)
  11. PHP 表单防止刷新提交的方法
  12. html5重力感应事件
  13. EasyMvc--让MVC区域开发更Easy(提供源码下载)
  14. Linux的资源管理器
  15. linux18.04+jdk11.0.2+hadoop3.1.2部署伪分布式
  16. Druid数据库连接池
  17. .net core 中的 DependencyInjection - IOC
  18. 【读书笔记】iOS-OCUnit-单元测试
  19. 2019-03-05-day004-列表操作
  20. LINUX block I/O --systemtap

热门文章

  1. E20180124-hm
  2. 【WIP_S3】链表
  3. c#自定义ORM框架---(泛型&amp;反射&amp;实体类扩展属性&lt;附带通用增、删、查、改&gt;)
  4. 1.2打印ASCII码
  5. JavaScript编程艺术-第8章-8.6.1-显示“缩略词语表”
  6. .net面试题 2016
  7. java 利用Xstream注解生成和解析xml
  8. 创建对象——单例(Singleton)模式
  9. switch-case用法
  10. 校网助手APP lua源码