CodeForces462B
Appleman and Card Game
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
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
15 10
DZFDFZDFDDDDDDF
82
6 4
YJSNPI
4
//2016.8.2
#include<iostream>
#include<algorithm>
#include<cstring> using namespace std; int ch[]; bool cmp(int a, int b)
{
return a>b;
} int min(int a, int b)
{
return a > b ? b : a;
} int main()
{
int n, k;
long long ans, x;
char c;
while(cin >> n >> k)
{
memset(ch, , sizeof(ch));
getchar();
ans = ;
for(int i = ; i < n; i++)
{
c = getchar();
ch[c-'A']++;
}
sort(ch, ch+, cmp); for(int i = ; k > ;i++)
{
x = min(k, ch[i]);
ans += x*x;
k -= x;
}
cout << ans << endl;
}
return ;
}
CodeForces462B的更多相关文章
随机推荐
- Swift中自定义打印方法
// 1.获取打印所在的文件 let file = ( #file as NSString).lastPathComponent // 2.获取打印所在的方法 let funcName = #func ...
- hibernate---一对一单向外键关联--annotation (重要!!!)
1. 生成wife.java: package com.bjsxt.hibernate; import javax.persistence.Entity; import javax.persisten ...
- Zabbix日志错误总结(持续更新)
no active checks on server [*.*.*.*:10051]: host [*] not found failed to accept an incoming connecti ...
- LPC2478的SPI使用
LPC2478的spi使用 LPC2748具有一个SPI控制器,可以当做SPI主机或者从机使用,有以下特性 其使用起来很方便,并且支持中断,使用的寄存器如下 基本上,使用起来就是设置控制为,CPOL ...
- NSDateFormatter调整时间格式的代码
NSDateFormatter调整时间格式的代码 在开发iOS程序时,有时候需要将时间格式调整成自己希望的格式,这个时候我们可以用NSDateFormatter类来处理.例如://实例化一个NSDat ...
- CentOS6.x升级MySQL版本5.1到5.6
CentOS6.x升级MySQL版本5.1到5.6 分类: Web MySQL 2014-08-04 11:22 2813人阅读 评论(1) 收藏 举报 mysql云服务器升级centos6 有一些虚 ...
- leetcode-004 insertion sort list
package leetcode; class ListNode { int val; ListNode next; ListNode(int x) { val = x; next = null; } ...
- 微信小程序之----navigator页面跳转
navigator navigator跳转页面样式分为两种一种是左上角带返回按钮跳转到新的页面,另一种不带即在本页跳转,通过控制redirect属性 .js <view> <navi ...
- 更改Windows Live Writer默认日志与草稿保存路径
目的:把保存Windows Live Writer的日志与草稿文件夹My Weblog Posts移动到E:\Blog\路径下 用mklink命令,创建E:\Blog\路径下的My Weblog Po ...
- 深度学习caffe:Ubuntu16.04安装指南(1)
caffe [CPU ONLY] 2017-01-15 最简单的安装配置方式: 不用GPU加速,使用OPENCV2.4图像库, 这是根据官方教程(链接如下)简化而得到. Ubuntu 16.04 or ...